问题描述
我正在尝试使用mbedTLS在运行FreeRTOS的微处理器上加密一些文本。我正在将AES 128 CBC与PKCS7填充一起使用。如果我尝试在文本少于16个字符时使用mbedTLS进行加密并使用Java进行解密,则可以使用。我可以用Java解密它,并且文本匹配。如果更长,则不再起作用。我在做什么错了?
unsigned char key[17] = "asdfghjklqwertzu";
unsigned char iv[17] = "qwertzuiopasdfgh";
unsigned char output[1024];
size_t olen;
size_t total_len = 0;
mbedtls_cipher_context_t ctx;
mbedtls_cipher_init(&ctx);
mbedtls_cipher_set_padding_mode(&ctx,MbedTLS_PADDING_PKCS7);
mbedtls_cipher_setup(&ctx,mbedtls_cipher_info_from_values(MbedTLS_CIPHER_ID_AES,128,MbedTLS_MODE_CBC));
mbedtls_cipher_setkey(&ctx,key,MbedTLS_ENCRYPT);
mbedtls_cipher_set_iv(&ctx,iv,16);
mbedtls_cipher_reset(&ctx);
char aa[] = "Hello World! test long padd";
for( int offset = 0; offset < strlen(aa); offset += mbedtls_cipher_get_block_size( &ctx ) ) {
int ilen = ( (unsigned int) strlen(aa) - offset > mbedtls_cipher_get_block_size( &ctx ) ) ?
mbedtls_cipher_get_block_size( &ctx ) : (unsigned int) ( strlen(aa) - offset );
char sub[100];
strncpy ( sub,aa+offset,ilen );
unsigned char* sub2 = reinterpret_cast<unsigned char *>(sub);
mbedtls_cipher_update(&ctx,sub2,ilen,output,&olen);
total_len += olen;
}
// After the loop
mbedtls_cipher_finish(&ctx,&olen);
total_len += olen;
mbedtls_cipher_free(&ctx);
Java代码:
try {
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
SecretKeySpec skey = new SecretKeySpec(encryptionKey.getBytes(),"AES");
Cipher cipherDecrypt = Cipher.getInstance("AES/CBC/PKCS7Padding");
cipherDecrypt.init(Cipher.DECRYPT_MODE,skey,ivParameterSpec);
return Optional.ofNullable(ByteString.copyFrom(cipherDecrypt.doFinal(message.toByteArray())));
} catch (BadPaddingException | IllegalBlockSizeException | InvalidAlgorithmParameterException | InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException e) {
log.error("Error during message decryption: ",e);
}
Java抛出javax.crypto.BadPaddingException:填充块已损坏
谢谢
//编辑:
尝试了一种更新方法,但仍然没有运气,同样的异常:
unsigned char key[17] = "asdfghjklqwertzu";
unsigned char iv[17] = "qwertzuiopasdfgh";
//unsigned char buffer[1024];
unsigned char output[1024];
size_t olen;
unsigned char text[] = "abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc";
mbedtls_cipher_context_t ctx;
mbedtls_cipher_init(&ctx);
mbedtls_cipher_set_padding_mode(&ctx,16);
mbedtls_cipher_reset(&ctx);
mbedtls_cipher_update(&ctx,text,strlen((char*) text),&olen); // Olen is 48
mbedtls_cipher_finish(&ctx,&olen); // Olen is 16
mbedtls_cipher_free(&ctx);
// 48 + 16 = 64 which is according to https://www.devglan.com/online-tools/aes-encryption-decryption correct
Java获取64字节的数据,但仍会引发相同的异常。
Topaco,请您提供使用更新和完成功能的简短示例吗?谢谢
解决方法
由于它是用注释编写的,因此仍然无法使用。这是您的代码,其中包含@Topaco在注释中建议的更改:
#include <stdio.h>
#include <string.h>
#include <mbedtls/cipher.h>
int main() {
unsigned char key[17] = "asdfghjklqwertzu";
unsigned char iv[17] = "qwertzuiopasdfgh";
unsigned char output[1024];
size_t olen;
size_t total_len = 0;
mbedtls_cipher_context_t ctx;
mbedtls_cipher_init(&ctx);
mbedtls_cipher_set_padding_mode(&ctx,MBEDTLS_PADDING_PKCS7);
mbedtls_cipher_setup(&ctx,mbedtls_cipher_info_from_values(MBEDTLS_CIPHER_ID_AES,128,MBEDTLS_MODE_CBC));
mbedtls_cipher_setkey(&ctx,key,MBEDTLS_ENCRYPT);
mbedtls_cipher_set_iv(&ctx,iv,16);
mbedtls_cipher_reset(&ctx);
char aa[] = "hello world! test long padd";
for (int offset = 0; offset < strlen(aa); offset += mbedtls_cipher_get_block_size(&ctx)) {
int ilen = ((unsigned int) strlen(aa) - offset > mbedtls_cipher_get_block_size(&ctx)) ?
mbedtls_cipher_get_block_size(&ctx) : (unsigned int) (strlen(aa) - offset);
char sub[100];
strncpy (sub,aa + offset,ilen);
unsigned char *sub2 = (unsigned char *) (sub);
mbedtls_cipher_update(&ctx,sub2,ilen,output + total_len,&olen);
total_len += olen;
}
mbedtls_cipher_finish(&ctx,&olen);
total_len += olen;
for (int i = 0; i < total_len; i++)
printf("%02X",output[i]);
mbedtls_cipher_free(&ctx);
return 0;
}
测试
我添加了两行:
for (int i = 0; i < total_len; i++)
printf("%02X",output[i]);
这给出了输出:
2FE64A72125E30B15C376FD2142D36FA778142DC4FD4A1F36EECDBCDA19BFAA0
如果我稍微修改一下Java端并使用以下命令传输消息:
byte[] message = hexStringToByteArray("2FE64A72125E30B15C376FD2142D36FA778142DC4FD4A1F36EECDBCDA19BFAA0");
从以下答案中获取hexStringToByteArray:https://stackoverflow.com/a/140861/2331445
我在Java端获得以下代码:
public Optional<ByteString> test() {
String encryptionKey = "asdfghjklqwertzu";
String iv = "qwertzuiopasdfgh";
byte[] message = hexStringToByteArray("2FE64A72125E30B15C376FD2142D36FA778142DC4FD4A1F36EECDBCDA19BFAA0");
Security.addProvider(new BouncyCastleProvider());
try {
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
SecretKeySpec skey = new SecretKeySpec(encryptionKey.getBytes(),"AES");
Cipher cipherDecrypt = Cipher.getInstance("AES/CBC/PKCS7Padding");
cipherDecrypt.init(Cipher.DECRYPT_MODE,skey,ivParameterSpec);
return Optional.ofNullable(ByteString.copyFrom(cipherDecrypt.doFinal(message)));
} catch (BadPaddingException | IllegalBlockSizeException | InvalidAlgorithmParameterException | InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException e) {
System.out.println("Error during message decryption: " + e);
}
return null;
}
最终将以下输出输出到调试控制台:
Optional[<ByteString@1e127982 size=27 contents="hello world! test long padd">]
那是原始消息-这样就可以工作。
,您的代码中还有一个问题。 在设置填充模式之前,您需要设置密码信息。否则函数 mbedtls_cipher_set_padding_mode 将返回错误