【发布时间】:2020-12-03 11:06:46
【问题描述】:
我正在尝试使用 mbedTLS 加密运行 FreeRTOS 的微处理器上的一些文本。我正在使用带有 PKCS7 填充的 AES 128 CBC。如果我尝试在 mbedTLS 中加密并在文本短于 16 个字符时在 Java 中解密,它可以工作。我可以用 Java 解密它并且文本匹配。如果它更长,那么它不再起作用。我做错了什么?
mbedTLS 代码:
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, 128, 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, output, &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, 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, 128, MBEDTLS_ENCRYPT);
mbedtls_cipher_set_iv(&ctx, iv, 16);
mbedtls_cipher_reset(&ctx);
mbedtls_cipher_update(&ctx, text, strlen((char*) text), output, &olen); // Olen is 48
mbedtls_cipher_finish(&ctx, output, &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,您能否提供使用更新和完成功能的简短示例?谢谢
【问题讨论】:
-
total_len += olen;是错误的,尽管您在代码中没有对它做任何事情。如果我正确阅读文档,总长度为olen。我怀疑在您的代码的另一部分中,您正在编写或发送total_length字节以供 java 解密。 -
output中的内容被每个mbedtls_cipher_update或mbedtls_cipher_finish覆盖,因为未设置当前 位置。在这两个mbedtls_cipher_update和mbedtls_cipher_finish中,output必须替换为output + total_len。顺便说一句,singlembedtls_cipher_update和mbedtls_cipher_finish调用就足够了(但这种实现可能更适合探索)。 -
我试着按照你们俩写的做,但还是不行。你能检查一下代码编辑吗?谢谢。
-
_update从output开始并给你一个长度,称之为len1。_final应该从output + len1开始并给你 另一个 长度说 len2;总密文为len1 + len2。注意@Topaco 说“both_update和-finish... 必须是 ...output + total_len” -
感谢@Topaco 和 dave_thompson_085 的帮助。
标签: java c cryptography bouncycastle mbedtls