【问题标题】:Decryption using AES使用 AES 解密
【发布时间】:2019-07-16 20:52:58
【问题描述】:

使用 AES 库,我正在尝试将加密数据从 arduino 端发送到 raspberry pi 端。在 arduino 串行监视器上打印的加密数据与在 raspberry 端打印的数据不同。 可能是解码问题。

此外,在树莓派端解密时,它会给出一个错误,说“输入文本的长度必须是 16 的倍数”,当我用零填充输入(温度数据)时,它仍然给出相同的错误消息。

我尝试使用“utf-8”和“iso-8859-1”进行解码,但它仍然没有显示相同的解密数据。

Python 代码:

 from Crypto.Cipher import AES
 ser=serial.Serial(' /dev/ttyS0',9600)
 st=ser.readline()
 st1=st.decode('utf-8')
 obj = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456')
 ciphertext = obj.encrypt(message)

 obj2 = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456')
 obj2.decrypt(ciphertext)

ARDUINO 代码:

void aesTest (int bits)
{
aes.iv_inc();

byte iv [N_BLOCK] ;
int plainPaddedLength = sizeof(chartemp) + (N_BLOCK - ((sizeof(chartemp)-1) % 16)); 
byte cipher [plainPaddedLength];
byte check [plainPaddedLength]; 


aes.set_IV(myIv);
aes.get_IV(iv);

aes.do_aes_encrypt(chartemp,sizeof(chartemp),cipher,key,bits,iv);


aes.set_IV(myIv);
aes.get_IV(iv);


aes.printArray(cipher,(bool)false); //print cipher with padding
String cipher1=String((char*)cipher);

myserial.println(cipher1);

}

HERE charttemp 是从 LM35 IC 转换为字符数组的温度。

我希望树莓派端的输出能够正确解密

【问题讨论】:

  • 我也尝试过使用base64,它没有显示预期的结果。请帮忙

标签: utf-8 aes raspberry-pi3


【解决方案1】:

加密数据是一个伪随机字节序列。它不是有效的 UTF-8 字符串。

这条线有点狡猾,但在技术上可能“有效”:

String cipher1=String((char*)cipher);

但是这行是不正确的:

st1=st.decode('utf-8')

您不能获取随机数据并将其解码为 utf-8。您要么只需要以字节串的形式发送和接收数据,要么将数据编码为字符串,例如使用 Base64。我怀疑你会更喜欢后者,所以看看 Java 中的 Base64 和 Python 中的 base64

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-12
    • 2013-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-22
    • 1970-01-01
    • 2021-02-11
    相关资源
    最近更新 更多