5 Replies - 1406 Views - Last Post: 20 August 2019 - 09:49 PM Rate Topic: -----

#1 StealthRT   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 94
  • Joined: 29-September 08

Arduino AES256 CBC base64 and VB.net AES256 CBC base64 not matching

Posted 25 March 2017 - 10:56 PM

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:
    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):
    #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?

Is This A Good Question/Topic? 0
  • +

Replies To: Arduino AES256 CBC base64 and VB.net AES256 CBC base64 not matching

#2 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2369
  • View blog
  • Posts: 4,496
  • Joined: 30-May 10

Re: Arduino AES256 CBC base64 and VB.net AES256 CBC base64 not matching

Posted 25 March 2017 - 11:57 PM

> aes.set_key( key , sizeof(key)); // Get the globally defined key
sizeof() on a pointer gets you the size of the pointer, not the amount of memory it's pointing at.
Perhaps you need strlen()?
Was This Post Helpful? 0
  • +
  • -

#3 StealthRT   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 94
  • Joined: 29-September 08

Re: Arduino AES256 CBC base64 and VB.net AES256 CBC base64 not matching

Posted 26 March 2017 - 12:07 AM

View PostSalem_c, on 26 March 2017 - 02:57 AM, said:

> aes.set_key( key , sizeof(key)); // Get the globally defined key
sizeof() on a pointer gets you the size of the pointer, not the amount of memory it's pointing at.
Perhaps you need strlen()?

I get the following error when doing that:

Quote

error: invalid conversion from 'byte* {aka unsigned char*}' to 'const char*' [-fpermissive]

Was This Post Helpful? 0
  • +
  • -

#4 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2369
  • View blog
  • Posts: 4,496
  • Joined: 30-May 10

Re: Arduino AES256 CBC base64 and VB.net AES256 CBC base64 not matching

Posted 26 March 2017 - 05:49 AM

Well you managed to cast it one way, can't you cast it back again? :rolleyes:/>/>/>/>

34	    char decoded[decodedLen]; 
35	    base64_decode( decoded , b64data , b64len );
36	    Serial.println("Decoded: " + String(decoded));


Also, the default String constructor assumes that there is a \0 at the end, but your temporary buffer doesn't have room for one (if base64_decode fills the buffer).

Also, VLA's are not permitted in C++.

> int b64len = base64_encode(b64data, (char *)msg.c_str(),msg.length());
You might want to revise how many casts back and forth you have.
For sure, casting away the const-ness of c_str() will lead to bad things at some point.

This post has been edited by Salem_c: 26 March 2017 - 06:27 AM

Was This Post Helpful? 0
  • +
  • -

#5 StealthRT   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 94
  • Joined: 29-September 08

Re: Arduino AES256 CBC base64 and VB.net AES256 CBC base64 not matching

Posted 26 March 2017 - 07:16 PM

Mind being a little more detail and/or showing an example with the code I posted, Salem?
Was This Post Helpful? 0
  • +
  • -

#6 StealthRT   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 94
  • Joined: 29-September 08

Re: Arduino AES256 CBC base64 and VB.net AES256 CBC base64 not matching

Posted 20 August 2019 - 09:49 PM

Still looking for a solution to my issue.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1