c# - AES CBC Not Producing Correct Vectors -
i have 2 methods encryption , decryption in cbc mode. have ready verified ecb methods , function properly. issue when test encryption in cbc test vectors passes them. , when test cbc decryption same vectors make sure can go other way, passes tests well. when pass encryption decryption, don't same vector back. doesn't make sense me.
public byte[,] encrypt(byte[,] datatoencrypt, byte[] givenkey, byte[] initializationvector) { //xor data iv (int row = 0; row < 4; row++) { datatoencrypt[row,0] ^= initializationvector[4*row]; datatoencrypt[row, 1] ^= initializationvector[(4 * row) + 1]; datatoencrypt[row, 2] ^= initializationvector[(4 * row) + 2]; datatoencrypt[row, 3] ^= initializationvector[(4 * row) + 3]; } ecb encryptor = new ecb(); return encryptor.encrypt(datatoencrypt, givenkey); } public byte[,] decrypt(byte[,] datatodecrypt, byte[] givenkey, byte[] initializationvector) { ecb encryptor = new ecb(); byte[,] plaintext = encryptor.decrypt(datatodecrypt, givenkey); (int row = 0; row < 4; row++) { plaintext[row, 0] ^= initializationvector[4 * row]; plaintext[row, 1] ^= initializationvector[(4 * row) + 1]; plaintext[row, 2] ^= initializationvector[(4 * row) + 2]; plaintext[row, 3] ^= initializationvector[(4 * row) + 3]; } return plaintext; }
encryptor class aes in ecb form. these methods supposed take in xor's cbc. can tell me if seems wrong?
this single block of data testing, not large amounts of data.
i fixed issue. problem fact encryption there has reference object data encrypting rather equaling values. apologize posting question stupid error making. comments though. , here new encryption method, in case cares:
public byte[,] encrypt(byte[,] datatoencrypt, byte[] givenkey, byte[] initializationvector) { //setup ecb encryptor = new ecb(); byte[,] cyphertext = new byte[4,4]; //xor data iv (int row = 0; row < 4; row++) { cyphertext[row, 0] = (byte)(datatoencrypt[row, 0] ^ initializationvector[4 * row]); cyphertext[row, 1] = (byte)(datatoencrypt[row, 1] ^ initializationvector[(4 * row) + 1]); cyphertext[row, 2] = (byte)(datatoencrypt[row, 2] ^ initializationvector[(4 * row) + 2]); cyphertext[row, 3] = (byte)(datatoencrypt[row, 3] ^ initializationvector[(4 * row) + 3]); } return encryptor.encrypt(cyphertext, givenkey); }
Comments
Post a Comment