public byte [] Decrypt(byte [] cipher) { //int iters = cipher.Length / 8; int blockSize = 8; int rem = cipher.Length % blockSize; if (rem != 0) { throw new Exception("must be in 8 byte blocks"); } byte [] lastBlock = new byte[blockSize]; Array.Copy(cipher, cipher.Length - blockSize, lastBlock, 0, blockSize); TripleDESCryptoServiceProvider tempTdcsp = new TripleDESCryptoServiceProvider(); tempTdcsp.Key = (byte[])_tdcsp.Key.Clone(); tempTdcsp.IV = lastBlock; //TODO make this support other lengths 0x01 - 0x07070707070707 byte [] pkcs5 = new byte [] { 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08 }; byte [] padPlus = tempTdcsp.EncryptValue(pkcs5); byte [] paddedCipher = new byte [cipher.Length + blockSize]; Array.Copy(cipher, 0, paddedCipher, 0, cipher.Length); Array.Copy(padPlus, 0, paddedCipher, cipher.Length, blockSize); byte [] plain = _tdcsp.DecryptValue(paddedCipher); return(plain); }
public byte [] Encrypt(byte [] plain) { //int iters = plain.Length / 8; int rem = plain.Length % 8; if (rem != 0) { throw new Exception("must be in 8 byte blocks"); } //just encrypt and throw away the last 8 bytes byte [] cipher = _tdcsp.EncryptValue(plain); byte [] cipherNoPad = new byte[cipher.Length - _tdcsp.IV.Length]; Array.Copy(cipher, 0, cipherNoPad, 0, cipherNoPad.Length); /* * byte [] cipher = new byte[plain.Length]; * for(int i=0; i<iters; i++) * { * int offset = i * 8; * byte [] plainBlock = new byte[8]; * Array.Copy(plain, offset, plainBlock, 0, 8); * byte [] cipherBlock = _tdcsp.EncryptValue(plainBlock); * Array.Copy(cipherBlock, 0, cipher, offset, 8); * } * return cipher; */ return(cipherNoPad); }