MbedTLS AES 128在Java中加密和解密

huangapple go评论144阅读模式
英文:

MbedTLS AES 128 encrypt and decrypt in Java

问题

mbedTLS代码段:

  1. unsigned char key[17] = "asdfghjklqwertzu";
  2. unsigned char iv[17] = "qwertzuiopasdfgh";
  3. unsigned char output[1024];
  4. size_t olen;
  5. size_t total_len = 0;
  6. mbedtls_cipher_context_t ctx;
  7. mbedtls_cipher_init(&ctx);
  8. mbedtls_cipher_set_padding_mode(&ctx, MBEDTLS_PADDING_PKCS7);
  9. mbedtls_cipher_setup(&ctx,
  10. mbedtls_cipher_info_from_values(MBEDTLS_CIPHER_ID_AES, 128,
  11. MBEDTLS_MODE_CBC));
  12. mbedtls_cipher_setkey(&ctx, key, 128, MBEDTLS_ENCRYPT);
  13. mbedtls_cipher_set_iv(&ctx, iv, 16);
  14. mbedtls_cipher_reset(&ctx);
  15. char aa[] = "hello world! test long padd";
  16. for (int offset = 0; offset < strlen(aa); offset += mbedtls_cipher_get_block_size(&ctx)) {
  17. int ilen = ((unsigned int)strlen(aa) - offset > mbedtls_cipher_get_block_size(&ctx)) ?
  18. mbedtls_cipher_get_block_size(&ctx) : (unsigned int)(strlen(aa) - offset);
  19. char sub[100];
  20. strncpy(sub, aa + offset, ilen);
  21. unsigned char *sub2 = reinterpret_cast<unsigned char *>(sub);
  22. mbedtls_cipher_update(&ctx, sub2, ilen, output, &olen);
  23. total_len += olen;
  24. }
  25. // 循环结束后
  26. mbedtls_cipher_finish(&ctx, output, &olen);
  27. total_len += olen;
  28. mbedtls_cipher_free(&ctx);

Java代码:

  1. try {
  2. IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
  3. SecretKeySpec skey = new SecretKeySpec(encryptionKey.getBytes(), "AES");
  4. Cipher cipherDecrypt = Cipher.getInstance("AES/CBC/PKCS7Padding");
  5. cipherDecrypt.init(Cipher.DECRYPT_MODE, skey, ivParameterSpec);
  6. return Optional.ofNullable(ByteString.copyFrom(cipherDecrypt.doFinal(message.toByteArray())));
  7. } catch (BadPaddingException | IllegalBlockSizeException | InvalidAlgorithmParameterException | InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException e) {
  8. log.error("Error during message decryption: ", e);
  9. }

Java抛出javax.crypto.BadPaddingException: pad block corrupted错误。

// 编辑:

尝试使用一次更新的方法仍然没有成功,仍然出现相同的异常:

  1. unsigned char key[17] = "asdfghjklqwertzu";
  2. unsigned char iv[17] = "qwertzuiopasdfgh";
  3. unsigned char output[1024];
  4. size_t olen;
  5. unsigned char text[] = "abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc";
  6. mbedtls_cipher_context_t ctx;
  7. mbedtls_cipher_init(&ctx);
  8. mbedtls_cipher_set_padding_mode(&ctx, MBEDTLS_PADDING_PKCS7);
  9. mbedtls_cipher_setup(&ctx,
  10. mbedtls_cipher_info_from_values(MBEDTLS_CIPHER_ID_AES, 128,
  11. MBEDTLS_MODE_CBC));
  12. mbedtls_cipher_setkey(&ctx, key, 128, MBEDTLS_ENCRYPT);
  13. mbedtls_cipher_set_iv(&ctx, iv, 16);
  14. mbedtls_cipher_reset(&ctx);
  15. mbedtls_cipher_update(&ctx, text, strlen((char*) text), output, &olen); // Olen is 48
  16. mbedtls_cipher_finish(&ctx, output, &olen); // Olen is 16
  17. mbedtls_cipher_free(&ctx);
  18. // 48 + 16 = 64,根据https://www.devglan.com/online-tools/aes-encryption-decryption 的信息是正确的
  19. Java得到了64字节的数据,但仍然抛出相同的异常。
  20. Topaco,请你能提供一下updatefinish函数的简短用法示例吗?谢谢。
  21. <details>
  22. <summary>英文:</summary>
  23. I am trying to encrypt some text on microprocessor running FreeRTOS with mbedTLS. I am using AES 128 CBC with PKCS7 padding. If I try to encrypt in mbedTLS and decrypt in Java when text is shorter than 16 characters it works. I can decrypt it in Java and the text matches. If it is longer then it no longer works. What am I doing wrong?
  24. mbedTLS code:
  25. unsigned char key[17] = &quot;asdfghjklqwertzu&quot;;
  26. unsigned char iv[17] = &quot;qwertzuiopasdfgh&quot;;
  27. unsigned char output[1024];
  28. size_t olen;
  29. size_t total_len = 0;
  30. mbedtls_cipher_context_t ctx;
  31. mbedtls_cipher_init(&amp;ctx);
  32. mbedtls_cipher_set_padding_mode(&amp;ctx, MBEDTLS_PADDING_PKCS7);
  33. mbedtls_cipher_setup(&amp;ctx,
  34. mbedtls_cipher_info_from_values(MBEDTLS_CIPHER_ID_AES, 128,
  35. MBEDTLS_MODE_CBC));
  36. mbedtls_cipher_setkey(&amp;ctx, key, 128, MBEDTLS_ENCRYPT);
  37. mbedtls_cipher_set_iv(&amp;ctx, iv, 16);
  38. mbedtls_cipher_reset(&amp;ctx);
  39. char aa[] = &quot;hello world! test long padd&quot;;
  40. for( int offset = 0; offset &lt; strlen(aa); offset += mbedtls_cipher_get_block_size( &amp;ctx ) ) {
  41. int ilen = ( (unsigned int) strlen(aa) - offset &gt; mbedtls_cipher_get_block_size( &amp;ctx ) ) ?
  42. mbedtls_cipher_get_block_size( &amp;ctx ) : (unsigned int) ( strlen(aa) - offset );
  43. char sub[100];
  44. strncpy ( sub, aa+offset, ilen );
  45. unsigned char* sub2 = reinterpret_cast&lt;unsigned char *&gt;(sub);
  46. mbedtls_cipher_update(&amp;ctx, sub2, ilen, output, &amp;olen);
  47. total_len += olen;
  48. }
  49. // After the loop
  50. mbedtls_cipher_finish(&amp;ctx, output, &amp;olen);
  51. total_len += olen;
  52. mbedtls_cipher_free(&amp;ctx);
  53. Java code:
  54. try {
  55. IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
  56. SecretKeySpec skey = new SecretKeySpec(encryptionKey.getBytes(), &quot;AES&quot;);
  57. Cipher cipherDecrypt = Cipher.getInstance(&quot;AES/CBC/PKCS7Padding&quot;);
  58. cipherDecrypt.init(Cipher.DECRYPT_MODE, skey, ivParameterSpec);
  59. return Optional.ofNullable(ByteString.copyFrom(cipherDecrypt.doFinal(message.toByteArray())));
  60. } catch (BadPaddingException | IllegalBlockSizeException | InvalidAlgorithmParameterException | InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException e) {
  61. log.error(&quot;Error during message decryption: &quot;, e);
  62. }
  63. Java throws javax.crypto.BadPaddingException: pad block corrupted
  64. Thanks
  65. // EDIT:
  66. Tried with one update approach and still no luck, the same exception:
  67. unsigned char key[17] = &quot;asdfghjklqwertzu&quot;;
  68. unsigned char iv[17] = &quot;qwertzuiopasdfgh&quot;;
  69. //unsigned char buffer[1024];
  70. unsigned char output[1024];
  71. size_t olen;
  72. unsigned char text[] = &quot;abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc&quot;;
  73. mbedtls_cipher_context_t ctx;
  74. mbedtls_cipher_init(&amp;ctx);
  75. mbedtls_cipher_set_padding_mode(&amp;ctx, MBEDTLS_PADDING_PKCS7);
  76. mbedtls_cipher_setup(&amp;ctx,
  77. mbedtls_cipher_info_from_values(MBEDTLS_CIPHER_ID_AES, 128,
  78. MBEDTLS_MODE_CBC));
  79. mbedtls_cipher_setkey(&amp;ctx, key, 128, MBEDTLS_ENCRYPT);
  80. mbedtls_cipher_set_iv(&amp;ctx, iv, 16);
  81. mbedtls_cipher_reset(&amp;ctx);
  82. mbedtls_cipher_update(&amp;ctx, text, strlen((char*) text), output, &amp;olen); // Olen is 48
  83. mbedtls_cipher_finish(&amp;ctx, output, &amp;olen); // Olen is 16
  84. mbedtls_cipher_free(&amp;ctx);
  85. // 48 + 16 = 64 which is according to https://www.devglan.com/online-tools/aes-encryption-decryption correct
  86. Java gets a 64 bytes of data but still throws the same exception.
  87. Topaco please can you provide short example of usage of update and finish functions? Thank you
  88. </details>
  89. # 答案1
  90. **得分**: 1
  91. Here is the translated content:
  92. 自从在评论中写明仍然不起作用以来,这里是按照@Topaco在评论中建议的更改的代码
  93. ```c
  94. #include <stdio.h>
  95. #include <string.h>
  96. #include <mbedtls/cipher.h>
  97. int main() {
  98. unsigned char key[17] = "asdfghjklqwertzu";
  99. unsigned char iv[17] = "qwertzuiopasdfgh";
  100. unsigned char output[1024];
  101. size_t olen;
  102. size_t total_len = 0;
  103. mbedtls_cipher_context_t ctx;
  104. mbedtls_cipher_init(&ctx);
  105. mbedtls_cipher_set_padding_mode(&ctx, MBEDTLS_PADDING_PKCS7);
  106. mbedtls_cipher_setup(&ctx,
  107. mbedtls_cipher_info_from_values(MBEDTLS_CIPHER_ID_AES, 128,
  108. MBEDTLS_MODE_CBC));
  109. mbedtls_cipher_setkey(&ctx, key, 128, MBEDTLS_ENCRYPT);
  110. mbedtls_cipher_set_iv(&ctx, iv, 16);
  111. mbedtls_cipher_reset(&ctx);
  112. char aa[] = "hello world! test long padd";
  113. for (int offset = 0; offset < strlen(aa); offset += mbedtls_cipher_get_block_size(&ctx)) {
  114. int ilen = ((unsigned int) strlen(aa) - offset > mbedtls_cipher_get_block_size(&ctx)) ?
  115. mbedtls_cipher_get_block_size(&ctx) : (unsigned int) (strlen(aa) - offset);
  116. char sub[100];
  117. strncpy(sub, aa + offset, ilen);
  118. unsigned char *sub2 = (unsigned char *)(sub);
  119. mbedtls_cipher_update(&ctx, sub2, ilen, output + total_len, &olen);
  120. total_len += olen;
  121. }
  122. mbedtls_cipher_finish(&ctx, output + total_len, &olen);
  123. total_len += olen;
  124. for (int i = 0; i < total_len; i++)
  125. printf("%02X", output[i]);
  126. mbedtls_cipher_free(&ctx);
  127. return 0;
  128. }

测试

我添加了两行额外的代码:

  1. for (int i = 0; i < total_len; i++)
  2. printf("%02X", output[i]);

这将产生以下输出:

  1. 2FE64A72125E30B15C376FD2142D36FA778142DC4FD4A1F36EECDBCDA19BFAA0

如果我稍微修改Java端并传递消息:

  1. byte[] message = hexStringToByteArray("2FE64A72125E30B15C376FD2142D36FA778142DC4FD4A1F36EECDBCDA19BFAA0");

其中hexStringToByteArray 是从这个答案中获取的:https://stackoverflow.com/a/140861/2331445

我在Java端得到以下代码:

  1. public Optional<ByteString> test() {
  2. String encryptionKey = "asdfghjklqwertzu";
  3. String iv = "qwertzuiopasdfgh";
  4. byte[] message = hexStringToByteArray("2FE64A72125E30B15C376FD2142D36FA778142DC4FD4A1F36EECDBCDA19BFAA0");
  5. Security.addProvider(new BouncyCastleProvider());
  6. try {
  7. IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
  8. SecretKeySpec skey = new SecretKeySpec(encryptionKey.getBytes(), "AES");
  9. Cipher cipherDecrypt = Cipher.getInstance("AES/CBC/PKCS7Padding");
  10. cipherDecrypt.init(Cipher.DECRYPT_MODE, skey, ivParameterSpec);
  11. return Optional.ofNullable(ByteString.copyFrom(cipherDecrypt.doFinal(message)));
  12. } catch (BadPaddingException | IllegalBlockSizeException | InvalidAlgorithmParameterException | InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException e) {
  13. System.out.println("Error during message decryption: " + e);
  14. }
  15. return null;
  16. }

最终在调试控制台上得到以下输出:

  1. Optional[<ByteString@1e127982 size=27 contents="hello world! test long padd">]

这就是原始消息 - 这样它就能工作了。

英文:

Since it was written in comments that it still doesn't work. Here your code with the changes suggested by @Topaco in the comments:

  1. #include &lt;stdio.h&gt;
  2. #include &lt;string.h&gt;
  3. #include &lt;mbedtls/cipher.h&gt;
  4. int main() {
  5. unsigned char key[17] = &quot;asdfghjklqwertzu&quot;;
  6. unsigned char iv[17] = &quot;qwertzuiopasdfgh&quot;;
  7. unsigned char output[1024];
  8. size_t olen;
  9. size_t total_len = 0;
  10. mbedtls_cipher_context_t ctx;
  11. mbedtls_cipher_init(&amp;ctx);
  12. mbedtls_cipher_set_padding_mode(&amp;ctx, MBEDTLS_PADDING_PKCS7);
  13. mbedtls_cipher_setup(&amp;ctx,
  14. mbedtls_cipher_info_from_values(MBEDTLS_CIPHER_ID_AES, 128,
  15. MBEDTLS_MODE_CBC));
  16. mbedtls_cipher_setkey(&amp;ctx, key, 128, MBEDTLS_ENCRYPT);
  17. mbedtls_cipher_set_iv(&amp;ctx, iv, 16);
  18. mbedtls_cipher_reset(&amp;ctx);
  19. char aa[] = &quot;hello world! test long padd&quot;;
  20. for (int offset = 0; offset &lt; strlen(aa); offset += mbedtls_cipher_get_block_size(&amp;ctx)) {
  21. int ilen = ((unsigned int) strlen(aa) - offset &gt; mbedtls_cipher_get_block_size(&amp;ctx)) ?
  22. mbedtls_cipher_get_block_size(&amp;ctx) : (unsigned int) (strlen(aa) - offset);
  23. char sub[100];
  24. strncpy (sub, aa + offset, ilen);
  25. unsigned char *sub2 = (unsigned char *) (sub);
  26. mbedtls_cipher_update(&amp;ctx, sub2, ilen, output + total_len, &amp;olen);
  27. total_len += olen;
  28. }
  29. mbedtls_cipher_finish(&amp;ctx, output + total_len, &amp;olen);
  30. total_len += olen;
  31. for (int i = 0; i &lt; total_len; i++)
  32. printf(&quot;%02X&quot;, output[i]);
  33. mbedtls_cipher_free(&amp;ctx);
  34. return 0;
  35. }

Test

I added two additional lines:

  1. for (int i = 0; i &lt; total_len; i++)
  2. printf(&quot;%02X&quot;, output[i]);

This gives the output:

  1. 2FE64A72125E30B15C376FD2142D36FA778142DC4FD4A1F36EECDBCDA19BFAA0

If I modify Java side a bit and transfer message with:

  1. byte[] message = hexStringToByteArray(&quot;2FE64A72125E30B15C376FD2142D36FA778142DC4FD4A1F36EECDBCDA19BFAA0&quot;);

where hexStringToByteArray is taken from this answer: https://stackoverflow.com/a/140861/2331445

I get on Java side the following code:

  1. public Optional&lt;ByteString&gt; test() {
  2. String encryptionKey = &quot;asdfghjklqwertzu&quot;;
  3. String iv = &quot;qwertzuiopasdfgh&quot;;
  4. byte[] message = hexStringToByteArray(&quot;2FE64A72125E30B15C376FD2142D36FA778142DC4FD4A1F36EECDBCDA19BFAA0&quot;);
  5. Security.addProvider(new BouncyCastleProvider());
  6. try {
  7. IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
  8. SecretKeySpec skey = new SecretKeySpec(encryptionKey.getBytes(), &quot;AES&quot;);
  9. Cipher cipherDecrypt = Cipher.getInstance(&quot;AES/CBC/PKCS7Padding&quot;);
  10. cipherDecrypt.init(Cipher.DECRYPT_MODE, skey, ivParameterSpec);
  11. return Optional.ofNullable(ByteString.copyFrom(cipherDecrypt.doFinal(message)));
  12. } catch (BadPaddingException | IllegalBlockSizeException | InvalidAlgorithmParameterException | InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException e) {
  13. System.out.println(&quot;Error during message decryption: &quot; + e);
  14. }
  15. return null;
  16. }

which finally gives the following output onto the debug console:

  1. Optional[&lt;ByteString@1e127982 size=27 contents=&quot;hello world! test long padd&quot;&gt;]

That is the original message - so this way it works.

答案2

得分: 0

还有一个问题留在你的代码中。
在设置填充模式之前,您需要设置密码信息。否则,函数mbedtls_cipher_set_padding_mode将返回一个错误。

英文:

There is still one issue left in your code.
You will need to setup cipher info before setting padding mode. Otherwise function
mbedtls_cipher_set_padding_mode will return an error

huangapple
  • 本文由 发表于 2020年8月13日 23:00:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/63397882.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定