Arduino AES256 CBC Base64 And VB.Net AES256 CBC Base64 Not Matching
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Arduino AES256 CBC Base64 And VB.Net AES256 CBC Base64 Not Matching

  1. #1
    Join Date
    Aug 2008
    Posts
    94

    Question Arduino AES256 CBC Base64 And VB.Net AES256 CBC Base64 Not Matching

    Hey all I have a question here.

    I have code for using AES 256 CBC for Arduino (esp8266) and also for my VB.net app in order for either side to send/receive data and decode/encode it.

    Both sides work and produces a correct encode and decode on their own. However, when testing with the same data on both sides I get different base64 strings.

    Here is the VB.net code:
    Code:
        Dim key As String = "5TGB&YHN7UJM(IK<jwS48BVz94#7%Bk2"
        Dim iv As String = "!QAZ2WSX#EDC4RFV"
    
        Public Function Encrypt256(text As String) As String
            Dim aes As New AesCryptoServiceProvider()
            Dim src As Byte() = Nothing
    
            aes.BlockSize = 128
            aes.KeySize = 256
            aes.IV = Encoding.UTF8.GetBytes(AesIV256.Text)
            aes.Key = Encoding.UTF8.GetBytes(AesKey256.Text)
            aes.Mode = CipherMode.CBC
            aes.Padding = PaddingMode.PKCS7
            src = Encoding.Unicode.GetBytes(text)
    
            Using encrypt As ICryptoTransform = aes.CreateEncryptor()
                Dim dest As Byte() = encrypt.TransformFinalBlock(src, 0, src.Length)
    
                Return Convert.ToBase64String(dest)
            End Using
        End Function
    
        Public Function Decrypt256(text As String) As String
            Dim aes As New AesCryptoServiceProvider()
            Dim src As Byte() = Nothing
    
            aes.BlockSize = 128
            aes.KeySize = 256
            aes.IV = Encoding.UTF8.GetBytes(AesIV256.Text)
            aes.Key = Encoding.UTF8.GetBytes(AesKey256.Text)
            aes.Mode = CipherMode.CBC
            aes.Padding = PaddingMode.PKCS7
            src = System.Convert.FromBase64String(text)
    
            Using decrypt As ICryptoTransform = aes.CreateDecryptor()
                Dim dest As Byte() = decrypt.TransformFinalBlock(src, 0, src.Length)
    
                Return Encoding.Unicode.GetString(dest)
            End Using
        End Function
    And for the Arduino side (using This library via This page):
    Code:
        #include "AES.h"
        
        AES aes;
        
        void callAES() {
            char b64data[200];
            byte cipher[1000];
            byte iv [N_BLOCK] ;
            
            Serial.println("Let's encrypt:");
            // Our AES key. Note that is the same that is used on the Node-Js side but as hex bytes.
            //byte key[] = { 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C };
            byte *key = (unsigned char*)"5TGB&YHN7UJM(IK<jwS48BVz94#7%Bk2";
            
            // The unitialized Initialization vector
            byte *my_iv = (unsigned char*)"!QAZ2WSX#EDC4RFV";
            
            // Our message to encrypt. Static for this example.
            String msg = "hello";
            
            aes.set_key( key , sizeof(key));  // Get the globally defined key
            
            // Print the IV
            base64_encode( b64data, (char *)my_iv, N_BLOCK);
            Serial.println(" IV b64: " + String(b64data));
            Serial.println(" Message: " + msg );
         
            int b64len = base64_encode(b64data, (char *)msg.c_str(),msg.length());
            Serial.println (" Message in B64: " + String(b64data) );
            Serial.println (" The lenght is:  " + String(b64len) );
            
            // For sanity check purpose
            int decodedLen = base64_dec_len((char *)cipher, aes.get_size());
            char decoded[decodedLen];  
            base64_decode( decoded , b64data , b64len );
            Serial.println("Decoded: " + String(decoded));
            
            // Encrypt! With AES256, our key and IV, CBC and pkcs7 padding    
            aes.do_aes_encrypt((byte *)b64data, b64len , cipher, key, 256, my_iv);
            
            Serial.println("Encryption done!");    
            Serial.println("Cipher size: " + String(aes.get_size()));    
            base64_encode(b64data, (char *)cipher, aes.get_size() );
            Serial.println ("Encrypted data in base64: " + String(b64data) );
              
            Serial.println("Done...");
        }
    
        void setup() {
          callAES();
        }
    With the VB.net program I get: wGoswg6KeQ9sWhGttu3esw==

    With Arduino I get: 8cbCFENvP39Cp+7wNhH71Q==

    Maybe someone will notice something I currently dont in the code?

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,581

    Re: Arduino AES256 CBC Base64 And VB.Net AES256 CBC Base64 Not Matching

    Code:
     aes.set_key( key , sizeof(key));  // Get the globally defined key
    as key is of type byte* (ie a pointer), sizeof(key) will return the number of bytes used to store a pointer - not the number of bytes in the string! Either
    Code:
     aes.set_key( key , strlen(key));  // Get the globally defined key
    or define key as an array
    Code:
    byte key[] = "5TGB&YHN7UJM(IK<jwS48BVz94#7%Bk2";
    ...
    aes.set_key( key , sizeof(key));
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C, C++ Compiler: Microsoft VS2017.2

  3. #3
    Join Date
    Aug 2008
    Posts
    94

    Re: Arduino AES256 CBC Base64 And VB.Net AES256 CBC Base64 Not Matching

    Quote Originally Posted by 2kaud View Post
    Code:
     aes.set_key( key , sizeof(key));  // Get the globally defined key
    as key is of type byte* (ie a pointer), sizeof(key) will return the number of bytes used to store a pointer - not the number of bytes in the string! Either
    Code:
     aes.set_key( key , strlen(key));  // Get the globally defined key
    or define key as an array
    Code:
    byte key[] = "5TGB&YHN7UJM(IK<jwS48BVz94#7%Bk2";
    ...
    aes.set_key( key , sizeof(key));
    With changing the key to an array i seem to still get the same base64 encrypted data of 8cbCFENvP39Cp+7wNhH71Q==.
    Last edited by StealthRT; March 26th, 2017 at 09:29 PM.

  4. #4
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,581

    Re: Arduino AES256 CBC Base64 And VB.Net AES256 CBC Base64 Not Matching

    Do you know which is right - the Arduino or the vb.net? If the vb.net is right, I suggest you ask on the Arduino forum https://forum.arduino.cc/ otherwise I can move this thread to the vb.net forum so that the vb gurus might be able to advise on the vb side.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C, C++ Compiler: Microsoft VS2017.2

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)