AES CFB 256-bit encrypted output longer than input?

I want to ask something, what distinguishes the conclusion below that these two programs are vb.net 2010 and dynamic c? Why is the length of the encrypted data different from the results (vb.net - 16 bytes = 0x878e086ec00cbfeaafc9fc91edc8294c and dynamic C - 10 bytes = 0x878e086ec00cbfeaafc9), although using the key, the initial vector (IV) and the announcement text are the same. What is the correct result? THANKYOU

I am trying to Encrypt decryption using AES CFB 256 BIT used by vb.net 2010, the result is as follows:

Key: 0x603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4 (256 bits = 32 BYTE)

IV: 0x000102030405060708090A0B0C0D0E0F (16 bytes)

Plain text: 0123456789 (10 bytes)

Encryption: 0x878e086ec00cbfeaafc9fc91edc8294c (16 bytes) ????? longer than 6 bytes than Plaintext = fc91edc8294c

Decrypt: 0123456789 (10 bytes)

And Encrypt decryption using AES CFB 256 BIT uses Dynamic C, the result is as follows:

Key: 0x603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4 (256 bits = 32 BYTE)

IV: 0x000102030405060708090A0B0C0D0E0F (16 bytes)

Plain text: 0123456789 (10 bytes)

Encryption: 0x878e086ec00cbfeaafc9 (10 bytes) ????? equal length equal to Plaintext

Decrypt: 0123456789 (10 bytes)


' VB.NET Imports System Imports System.IO Imports System.Security.Cryptography Imports System.Text Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim original As String = "0123456789" Dim roundtrip As String Dim textConverter As New ASCIIEncoding() Dim myRijndael As New RijndaelManaged() Dim fromEncrypt() As Byte Dim encrypted() As Byte Dim toEncrypt() As Byte Dim key() As Byte Dim IV() As Byte 'Create a new key and initialization vector. 'myRijndael.GenerateKey() 'myRijndael.GenerateIV() Debug.Print("Original: " & original) myRijndael.Mode = CipherMode.CFB myRijndael.BlockSize = 128 myRijndael.KeySize = 256 myRijndael.Key = StringToByteArray("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4") myRijndael.IV = StringToByteArray("000102030405060708090A0B0C0D0E0F") 'Get the key and IV. key = myRijndael.Key IV = myRijndael.IV 'Get an encryptor. Dim encryptor As ICryptoTransform = myRijndael.CreateEncryptor(key, IV) 'Encrypt the data. Dim msEncrypt As New MemoryStream() Dim csEncrypt As New CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write) 'Convert the data to a byte array. toEncrypt = textConverter.GetBytes(original) 'Write all data to the crypto stream and flush it. csEncrypt.Write(toEncrypt, 0, toEncrypt.Length) csEncrypt.FlushFinalBlock() 'Get encrypted array of bytes. encrypted = msEncrypt.ToArray() Debug.Print("Encrypted: " & ByteArrayToString(encrypted)) 'This is where the message would be transmitted to a recipient ' who already knows your secret key. Optionally, you can ' also encrypt your secret key using a public key algorithm ' and pass it to the mesage recipient along with the RijnDael ' encrypted message. 'Get a decryptor that uses the same key and IV as the encryptor. Dim decryptor As ICryptoTransform = myRijndael.CreateDecryptor(key, IV) 'Now decrypt the previously encrypted message using the decryptor ' obtained in the above step. Dim msDecrypt As New MemoryStream(encrypted) Dim csDecrypt As New CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read) fromEncrypt = New Byte(encrypted.Length) {} 'Read the data out of the crypto stream. csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length) 'Convert the byte array back into a string. roundtrip = textConverter.GetString(fromEncrypt) 'Display the original data and the decrypted data. Debug.Print("Decrypt: " & roundtrip) Debug.Print("") End Sub Public Function ByteArrayToString(ByVal ba As Byte()) As String Dim hex As New StringBuilder(ba.Length * 2) For Each b As Byte In ba hex.AppendFormat("{0:x2}", b) Next Return hex.ToString() End Function Public Function StringToByteArray(ByVal hex As [String]) As Byte() Dim NumberChars As Integer = hex.Length Dim bytes As Byte() = New Byte(NumberChars \ 2 - 1) {} For i As Integer = 0 To NumberChars - 1 Step 2 bytes(i \ 2) = Convert.ToByte(hex.Substring(i, 2), 16) Next Return bytes End Function End Class 

 ////////////////////////////////////// //DYNAMIC C ////////////////////////////////////// #use AES_CRYPT.LIB #define AES_BLOCK_SIZE 16 #define BUFFER_SIZE 128 const char AES_KEY[AES_BLOCK_SIZE * 2] = { '\x60', '\x3d', '\xeb', '\x10', '\x15', '\xca', '\x71', '\xbe', '\x2b', '\x73', '\xae', '\xf0', '\x85', '\x7d', '\x77', '\x81', '\x1f', '\x35', '\x2c', '\x07', '\x3b', '\x61', '\x08', '\xd7', '\x2d', '\x98', '\x10', '\xa3', '\x09', '\x14', '\xdf', '\xf4', }; const char AES_IV[AES_BLOCK_SIZE] = { '\x00', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07', '\x08', '\x09', '\x0A', '\x0B', '\x0C', '\x0D', '\x0E', '\x0F', }; void ENC_AES_CFB_256(char data_in[], char data_out[]){ static char buffer[BUFFER_SIZE]; AESstreamState state; memset(buffer,'\0',sizeof(buffer)); AESinitStream256(&state, AES_KEY, AES_IV); memcpy(buffer, data_in, strlen(data_in)); AESencryptStream(&state, buffer, strlen(buffer)); strcpy(data_out, buffer); } void DEC_AES_CFB_256(char data_in[], char data_out[]){ static char buffer[BUFFER_SIZE]; AESstreamState state; memset(buffer,'\0',sizeof(buffer)); AESinitStream256(&state, AES_KEY, AES_IV); memcpy(buffer, data_in, strlen(data_in)); AESdecryptStream(&state, buffer, strlen(buffer)); strcpy(data_out, buffer); } int main(void) { static char ot[128], buf1[128]; int i; memset(ot,'\0',sizeof(ot)); memset(buf1,'\0',sizeof(buf1)); ENC_AES_CFB_256("0123456789", ot); for (i = 0; i < strlen(ot); i++) { printf("%02x", ot[i]); } printf("\n"); DEC_AES_CFB_256(ot, buf1); for (i = 0; i < strlen(buf1); i++) { printf(" %c ", buf1[i]); } printf("\n"); return 0; } 

/////////////////////////////////////// DYNAMIC UPDATES C //////// //////////////////////////////

  #class auto #use AES_CRYPT.LIB #define AES_BLOCK_SIZE 16 // BYTE #define MD5_BLOCK_SIZE 16 // BYTE #define AES_BUFFER_SIZE 128 // BYTE const char AES_KEY[AES_BLOCK_SIZE * 2] = { '\x60', '\x3d', '\xeb', '\x10', '\x15', '\xca', '\x71', '\xbe', '\x2b', '\x73', '\xae', '\xf0', '\x85', '\x7d', '\x77', '\x81', '\x1f', '\x35', '\x2c', '\x07', '\x3b', '\x61', '\x08', '\xd7', '\x2d', '\x98', '\x10', '\xa3', '\x09', '\x14', '\xdf', '\xf4', }; const char AES_IV[AES_BLOCK_SIZE] = { '\x00', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07', '\x08', '\x09', '\x0A', '\x0B', '\x0C', '\x0D', '\x0E', '\x0F', }; /////////////////// AES CFB - KEY: 32 Byte (256 bit) and BLOCK: 16 byte (128 bit) /////////////////// void ENC_AES_CFB_32(char data_in[], char data_out[], int count) { AESstreamState enc_state; static char enc_buffer[AES_BUFFER_SIZE]; memset(enc_buffer,'\0', sizeof(enc_buffer)); memcpy(enc_buffer, data_in, count); enc_buffer[count]='\0'; AESinitStream256(&enc_state, AES_KEY, AES_IV); AESencryptStream(&enc_state, enc_buffer, sizeof(enc_buffer)); memcpy(data_out, enc_buffer, count); data_out[count]='\0'; } void DEC_AES_CFB_32(char data_in[], char data_out[], int count){ AESstreamState dec_state; static char dec_buffer[AES_BUFFER_SIZE]; memset(dec_buffer,'\0', sizeof(dec_buffer)); memcpy(dec_buffer, data_in, count); dec_buffer[count]='\0'; AESinitStream256(&dec_state, AES_KEY, AES_IV); AESdecryptStream(&dec_state, dec_buffer, sizeof(dec_buffer)); memcpy(data_out, dec_buffer, count); data_out[count]='\0'; } ////////////// MAIN PROGRAM //////////////////// int main(void) { static char in[AES_BUFFER_SIZE], ot[AES_BUFFER_SIZE], buf[AES_BUFFER_SIZE]; int i, cnt; ////////////// INITIALIZE INPUT CFB //////////////////// memset(in,'\0',sizeof(in)); // CLEAR INPUT BUFFER //// DATA INPUT (TEST 1) //// //cnt = 10; // DATA INPUT LENGTH //sprintf(in,"%s","0123496785"); // DATA INPUT //// DATA INPUT (TEST 2) //// cnt = 82; // DATA INPUT LENGTH sprintf(in,"%s"," 0123456789abcdefghijklmnopqrstuvwxyz!@ #$%^&*()_+{}|:L<>?ABCDEFGHIJKLMNOPQRSTUVWXYZ"); // DATA INPUT printf("PLAINT TEXT : %s", in); // PRINT INPUT printf("\n\n"); ////////////// TEST ENCRYPT WITH CFB //////////////////// memset(ot,'\0',sizeof(ot)); // CLEAR OUTPUT BUFFER ENC_AES_CFB_32(in, ot, cnt); // CALL ENCRYPT FUNCTION printf("ENCRYPT : "); // PRINT OUTPUT for (i = 0; i < cnt; i++){ printf("%02x", ot[i]); } printf("\n\n"); ////////////// TEST DECRYPT WITH CFB //////////////////// memset(buf,'\0',sizeof(buf)); // CLEAR OUTPUT BUFFER DEC_AES_CFB_32(ot, buf, cnt); // CALL DECRYPT FUNCTION printf("DECRYPT : "); // PRINT OUTPUT for (i = 0; i < cnt; i++) { printf("%c", buf[i]); } printf("\n\n"); return 0; } ///////////// OUTPUT TEST 1 //////////// // PLAINT TEXT : 0123496785 // ENCRYPT : 878e086ec000bfeaafc5 // DECRYPT : 0123496785 ///////////// OUTPUT TEST 2 //////////// // PLAINT TEXT : 0123456789abcdefghijklmnopqrstuvwxyz!@ #$%^&*()_+{}|:L<>?ABCDEFGHIJKLMNOPQRSTUVWXYZ // ENCRYPT : 878e086ec00cbfeaafc99bf588aa4a2cbefd574850365f17d882f1dd19fa601e80bbcf845a5f213800d72a5ab8b1aafc87df077ed8695c92e38ce8d3b54071d87274349f806b8afae15f6f3730dbaf8dc4c5 // DECRYPT : 0123456789abcdefghijklmnopqrstuvwxyz!@ #$%^&*()_+{}|:L<>?ABCDEFGHIJKLMNOPQRSTUVWXYZ 
+4
source share
2 answers

Using strlen and strcpy in the ciphertext of a C program can be part of the problem. Encrypted data can easily have zeros in it, so it would be impractical to use those C functions that expect strings with NULL termination. This may not be a problem in this particular situation, but it can ultimately cause problems and lead to an error.

+1
source

The problem is that your VB code uses the PKCS # 5 add-on (PKCS7 in .NET).

You should not use indentation with CFB; as a stream mode, there will never be a partial block that needs to be added ..NET, however, sets the PKCS7 add-on as the default. Explicitly override it as follows:

 … myRijndael.Mode = CipherMode.CFB myRijndael.Padding = PaddingMode.None 
+2
source

Source: https://habr.com/ru/post/1383757/


All Articles