完美解决Linux(Android)操作系统下aes解密失败的问题

完美解决Linux操作系统下aes解密失败的问题

作者: 字体:[增加 减小] 类型:转载 时间:2013-08-28 我要评论

以下是针对在Linux操作系统下关于AES解密失败的问题进行了详细的分析介绍,需要的朋友可以过来参考下
现象描述:
windows上加解密正常,linux上加密正常,解密时发生如下异常:
复制代码代码如下:

javax.crypto.BadPaddingException: Given final block not properly padded
       at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)
       at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)
       at com.sun.crypto.provider.AESCipher.engineDoFinal(DashoA13*..)
       at javax.crypto.Cipher.doFinal(DashoA13*..)
       at chb.test.crypto.AESUtils.crypt(AESUtils.java:386)
       at chb.test.crypto.AESUtils.AesDecrypt(AESUtils.java:254)
       at chb.test.crypto.AESUtils.main(AESUtils.java:40) 

解决方法:
经过检查之后,定位在生成KEY的方法上,如下:
复制代码代码如下:

public static SecretKey getKey (String strKey) {
         try {         
            KeyGenerator _generator = KeyGenerator.getInstance( "AES" );
            _generator.init(128, new SecureRandom(strKey.getBytes()));
                return _generator.generateKey();
        }  catch (Exception e) {
             throw new RuntimeException( " 初始化密钥出现异常 " );
        }
      } 

修改到如下方式,问题解决:
复制代码代码如下:

  public static SecretKey getKey(String strKey) {
         try {         
            KeyGenerator _generator = KeyGenerator.getInstance( "AES" );
             SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG" );
            secureRandom.setSeed(strKey.getBytes());
            _generator.init(128,secureRandom);
                return _generator.generateKey();
        }  catch (Exception e) {
             throw new RuntimeException( " 初始化密钥出现异常 " );
        }
      } 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
您可以使用 OpenSSL 库来实现 Linux 下的 AES 加密和解密程序。以下是一个简单的示例程序: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <openssl/aes.h> #define AES_BLOCK_SIZE 16 int main(int argc, char *argv[]) { AES_KEY aes_key; unsigned char key[AES_BLOCK_SIZE] = {'k', 'e', 'y', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'a', 'b', 'c'}; unsigned char iv[AES_BLOCK_SIZE] = {'i', 'v', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'a', 'b', 'c', 'd'}; unsigned char plaintext[] = "Hello, world!"; unsigned char ciphertext[sizeof(plaintext)]; unsigned char decryptedtext[sizeof(plaintext)]; int plaintext_len = strlen((char *) plaintext); int ciphertext_len, decryptedtext_len; AES_set_encrypt_key(key, AES_BLOCK_SIZE * 8, &aes_key); AES_cbc_encrypt(plaintext, ciphertext, plaintext_len, &aes_key, iv, AES_ENCRYPT); ciphertext_len = strlen((char *) ciphertext); AES_set_decrypt_key(key, AES_BLOCK_SIZE * 8, &aes_key); AES_cbc_encrypt(ciphertext, decryptedtext, ciphertext_len, &aes_key, iv, AES_DECRYPT); decryptedtext_len = strlen((char *) decryptedtext); printf("Plaintext: %s\n", plaintext); printf("Ciphertext: "); for(int i = 0; i < ciphertext_len; i++) printf("%x", ciphertext[i]); printf("\n"); printf("Decrypted text: %s\n", decryptedtext); return 0; } ``` 在该程序中,我们使用了 OpenSSL 库中 AES 加密算法的 CBC 模式。首先,我们需要定义一个 16 字节的密钥和初始化向量(IV)。然后,我们使用 `AES_set_encrypt_key()` 函数设置加密密钥,并使用 `AES_cbc_encrypt()` 函数加密明文。加密后的密文存储在 `ciphertext` 数组中,并使用 `strlen()` 函数计算密文长度。 接下来,我们使用 `AES_set_decrypt_key()` 函数设置解密密钥,并使用 `AES_cbc_encrypt()` 函数解密密文。解密后的明文存储在 `decryptedtext` 数组中,并使用 `strlen()` 函数计算明文长度。 最后,我们输出原始明文、加密后的密文和解密后的明文,以验证加密和解密过程是否正确。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值