-
Notifications
You must be signed in to change notification settings - Fork 56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Encryption using the Java gave different result (solved) #4
Comments
PKCS7Padding but limited to 16bytes block. thank you for mentioning that i dont have the padding in the guide. i will keep in mind that the micro-controller con only handle 16bytes block , so On Tue, Sep 27, 2016 at 5:28 PM, robertgregor notifications@github.com
|
I see, OK, so the padding is clear. However, the first block has to be encrypted anyway in the same way, if the IV is the same. So why I am getting completely different results in java and with your library. I don't understand that, it should be the same... |
I have spent some time to look more closely and to match the result of your library with Java. I have found out, that the IV has to be calculated. However, I have modified my sketch now like that: #include <AES.h> AES aes; byte plain[] = "AddNodeA"; //real iv = iv x2 ex: 01234567 = 0123456701234567 void setup() { void prekey (int bits) void loop() { And this is the output, I have got: Encryption took: 414 And I can see, that the IV and CIPHER is the same. Why is that? Am I doing something wrong here? Robert |
no, the IV is part of the cipher (this comes from the Mechanism) |
@robertgregor did you solve your issue? I'm having the same problem. I'm ciphering in nodejs and java and they are giving the same result other than on Arduino that I'm having the same result with @spaniakos library or https://github.com/DavyLandman/AESLib library as well. padPlaintext besides "zero padding" to fill the rest of the block with zeros it also considers that you've entered a char array to encrypt that should end with 0x00 (end of string). So the buffers plain, cipher and check should be greater than the NBLOCK by one. But even overcoming this problem I'm still getting different results. Below I'm crypting in a different manner to avoid the end of string problem. (nodejs = java) != (@spaniakis = @DavyLandman)
Output:
This code below you can type on console running node:
Output:
|
Can you send me you ino file? Or just the plaintext/iv you want to use? The
expected results are above as i read.
…On Oct 31, 2017 8:50 PM, "rsegecin" ***@***.***> wrote:
@robertgregor <https://github.com/robertgregor> did you solve your issue?
I'm having the same problem. I'm ciphering in nodejs and java and they are
giving the same result other than on Arduino that I'm having the same
result with @spaniakos <https://github.com/spaniakos> library or
https://github.com/DavyLandman/AESLib library as well. *padPlaintext*
besides "zero padding" to fill the rest of the block with zeros it also
considers that you've entered a char array to encrypt that should end with
0x00 (end of string) and the plain, cipher and check buffers should be
greater than the NBLOCK by one. But even overcoming this problem I'm still
getting different results. Below I'm crypting in a different manner to
avoid the end of string problem.
(nodejs = java) != ***@***.*** = @DavyLandman
<https://github.com/davylandman>)
#define CBLOCK (1 * N_BLOCK)
void crypt() {
AES aes;
uint8_t key[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
uint8_t iv[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
byte tmp_iv[N_BLOCK];
byte data[CBLOCK];
byte ciphed[CBLOCK];
byte deciphed[CBLOCK];
char auxBuffer[129];
Serial.println("Encrypting");
memset(data, 0x00, CBLOCK);
memcpy(data, "rinaldi", 7);
memcpy(tmp_iv, iv, 16);
aes.set_key(key, 128);
byte test = aes.cbc_encrypt(data, ciphed, CBLOCK / N_BLOCK, tmp_iv);
if (test == SUCCESS)
{
Serial.println("Success");
ByteToHexString(auxBuffer, (unsigned char *)ciphed, sizeof(ciphed));
auxBuffer[32] = 0x00;
sprintf(lBuffer, "encrypted-cbc: %s CBLOCK: %i", auxBuffer, CBLOCK);
Serial.println(lBuffer);
ByteToHexString(auxBuffer, (unsigned char *)iv, sizeof(ciphed));
auxBuffer[32] = 0x00;
sprintf(lBuffer, "iv: %s", auxBuffer);
Serial.println(lBuffer);
memcpy(tmp_iv, iv, 16);
memset(deciphed, 0x00, 16);
test = aes.cbc_decrypt(ciphed, deciphed, CBLOCK / N_BLOCK, tmp_iv);
if (test == SUCCESS)
{
sprintf(lBuffer, "dencrypted-cbc: %s", deciphed);
Serial.println(lBuffer);
}
else
{
Serial.println("Failure");
}
}
else
{
Serial.println("Failure");
}
// Should give
// 583460271129deead261e621b676f1bf6
// But it's giving
// 2FD27523B3E1B5A122F397A5F976A680
}
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#4 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/ABnM6r3VpCm56yATsJtdRD7gBChXhKsRks5sx2vagaJpZM4KHvDG>
.
|
@spaniakos Thank you for the quick reply. I've altered the previous post with the all code and added some nodejs code to compare the output. |
Thank you
Will see it tomorrow.
…On Oct 31, 2017 9:29 PM, "rsegecin" ***@***.***> wrote:
@spaniakos <https://github.com/spaniakos> Thank you for the quick reply.
I've altered the previous post with the all code and added some nodejs code
to compare the output.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#4 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/ABnM6p_1Fa1KVTPFNCFuq7Muopw6MWo8ks5sx3UlgaJpZM4KHvDG>
.
|
Here it's the same variable values to produce the same output but using the method do_aes_encrypt, notice that I've to add 1 to CBLOCK.
|
I've just spent the all night trying to figure it out why it wasn't encrypting the same way and I found out that the encrypted data it's not padded PKCS#5. With the string that I passed in the example code I was able to obtain the right encryption padding manually. Updating the line in the example code as shown below:
As the block has 16 bytes and the string "rinaldi" has 7 so you've to pad 9 bytes with the byte 0x09 to the right in this block. I've altered the library to automate this process but then I notice that may be I should be padding with 8 bytes instead, I'll be trying this tomorrow. |
I will try to run the code in my framework
The weird thing is that i tested the library against standard and it works.
I dont know what time i will have the tests done as in my job it's a hell
week ( server migration and framework update ) haha
…On Nov 1, 2017 10:20, "rsegecin" ***@***.***> wrote:
I've just spent the all night trying to figure it out why it wasn't
encrypting with the same output and I found out that the encrypted data
it's not padded PKCS#5. With the string that I passed in the example code I
was able to obtain the right encryption padding manually. Updating the line
in the example code as shown below:
memset(data, 0x09, CBLOCK);
memcpy(data, (uint8_t*)"rinaldi", 7);
As the block has 16 bytes and the string "rinaldi" has 7 so you've to pad
9 bytes with the byte 0x09 to the right in this block. I've altered the
library to automate this process but then I notice that may be I should be
padding with 8 bytes instead, I'll be trying this tomorrow.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#4 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/ABnM6p_NKeP8Uk3I2IajuHw5-maalyPrks5syCnagaJpZM4KHvDG>
.
|
Hi @rsegecin, Can you please share the code to automate padding. Also want to know how can we handle long strings to encypt and decrypt. Also when I try to decrypt the crypted string printed in serial monitor from online it is not working, but when I try to decrypt the iv string printed in serial monitor i get the results as expected.
|
Hi @spaniakos,
|
@deepakchhapru the code that I made at the time was working only for a block of 16 bytes and I didn't put effort to understand the reason because I decided it was easier to change to Zero padding the rest of my system. If you follow the way I described it will work for the first block from there I think you can come up with a loop to work with all blocks of data. |
@deepakchhapru the padding is PKCS#7 When you transfer an encrypted string for decryption, the rule is: @rsegecin what was the problem for the code in your case ? |
@spaniakos I didn't know that the default padding for this library was different than those set in Java and NodeJs so I was getting different ciphering results. At the time I was researching it I couldn't find a way in your library or any other to set the padding to match for those languages. |
@spaniakos, But same code is not working when i give long plain text to encrypt and decrypt. So in @robertgregor code i just want to know how to handle long plain text encryption and decryption. |
@deepakchhapru I just placed an order online for an esp8622 and a nodemcu since i had many requests about and as it seems more and more coders are using these boards. I wont be able to compile against an esp8622 or nodemcu to see that results for 2-4 working days (hopefully Friday, with worst case scenario Monday) I will have my nodemcu and I will try to compile against it, and patch the bug. |
Hello, I am playing with your library and planning to use it in my IoT esp8266 communication with the server using the java. So on one side, Java is used and on other side, esp8266 chip is used. I have made some small changes, load it to the esp and created the sketch:
include <AES.h>
include <libb64/cencode.h>
AES aes;
byte key = (unsigned char)"0123456789010123";
byte plain[] = "AddNodeAddNodeAd";
//real iv = iv x2 ex: 01234567 = 0123456701234567
unsigned long long int my_iv = 36753562;
void setup() {
// put your setup code here, to run once:
Serial.begin (115200);
delay(500);
Serial.println("test");
}
void printArray(byte* data, int length) {
for (int i=0; i<length; i++) {
Serial.print((char)data[i]);
}
Serial.println();
}
void printBase64Array(byte* data, int inputLen) {
char encoded[base64_encode_expected_len(inputLen)];
base64_encode_chars((char*)data, inputLen,encoded);
for (int i=0; i<sizeof(encoded); i++) {
Serial.print((char)encoded[i]);
}
Serial.println();
}
void prekey (int bits)
{
aes.iv_inc();
byte iv [N_BLOCK] ;
byte plain_p[48];
byte cipher [48] ;
byte check [48] ;
unsigned long ms = micros ();
aes.set_IV(my_iv);
aes.get_IV(iv);
aes.do_aes_encrypt(plain,41,cipher,key,bits,iv);
Serial.print("Encryption took: ");
Serial.println(micros() - ms);
ms = micros ();
aes.set_IV(my_iv);
aes.get_IV(iv);
aes.do_aes_decrypt(cipher,48,check,key,bits,iv);
Serial.print("Decryption took: ");
Serial.println(micros() - ms);
Serial.println("\n\nPLAIN :");
printArray(plain, sizeof(plain));
Serial.println("\nCIPHER:");
printBase64Array(cipher, sizeof(cipher));
Serial.println("\nCHECK :");
//printArray(check,(bool)true);
Serial.println("\nIV :");
//printArray(iv,16);
Serial.println("\n============================================================\n");
}
void prekey_test ()
{
prekey (128) ;
}
void loop() {
prekey_test ();
delay(2000);
}
Then I wrote the corresponding java code and I expect to get the same result:
public class AES {
}
But the results are different. Because with your library I get (base64 encoded):
TfYfvi8/abVK7Ni5x4FpAD82mpq7HY2NAfXHIKX3FTCFMBjdkN4afwUDbDvjYuI0
and with Java I've got:
japwuqYfWgah2eNoWZtbQQ==
I think, the java output looks OK, thus I am encrypting only 128 bits (16 bytes) so should have only 16 bytes output. Why your library is getting 32? Are you padding the input with something?
Thanks.
The text was updated successfully, but these errors were encountered: