AES-128-CBC加解密/openssl_decrypt

针对php的AES-128-CBC加解密代码,需要使用JAVA解密。

1. php加解密

//加密
$result = openssl_encrypt($data, "AES-128-CBC", $aesKey, 0, $aesIv);
//解密
$result = openssl_decrypt($data, "AES-128-CBC", $aesKey, 0, $aesIv);

需要注意$options变量的不同。
加密过程中的$options=1, 意味着使用OPENSSL_RAW_DATA。
解密过程中的$options=1, 意味着使用OPENSSL_RAW_DATA。$options=0,意味着使用base64_encoded文本。此外,$aesKey和$aesIv要求16位长度,超过限制长度,截取前16位。

2. java解密

    public String aesDecrypt(String data, String aesKey, String aesIv) throws Exception {
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(aesKey.substring(0, 16).getBytes(), ALGORITHM),
                new IvParameterSpec(aesIv.getBytes()));
        byte[] plainText = Base64.getDecoder().decode(Base64.getDecoder().decode(data));
        byte[] plainBytes = cipher.doFinal(plainText);
        return new String(plainBytes);
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是示例代码,使用 OpenSSL 库进行 AES-CBC 密和解密密: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <openssl/aes.h> void aes_encrypt_cbc(const unsigned char *plaintext, int plaintext_len, const unsigned char *key, const unsigned char *iv, unsigned char *ciphertext) { AES_KEY aes_key; AES_set_encrypt_key(key, 128, &aes_key); AES_cbc_encrypt(plaintext, ciphertext, plaintext_len, &aes_key, iv, AES_ENCRYPT); } int main() { const unsigned char *plaintext = (const unsigned char *)"待密的明文"; const unsigned char *key = (const unsigned char *)"密钥"; const unsigned char *iv = (const unsigned char *)"初始化向量"; unsigned char ciphertext[AES_BLOCK_SIZE * ((strlen((const char *)plaintext) + AES_BLOCK_SIZE - 1) / AES_BLOCK_SIZE)]; memset(ciphertext, 0, sizeof(ciphertext)); aes_encrypt_cbc(plaintext, strlen((const char *)plaintext), key, iv, ciphertext); printf("密结果:"); for (int i = 0; i < sizeof(ciphertext); i++) { printf("%02x", ciphertext[i]); } printf("\n"); return 0; } ``` 解密: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <openssl/aes.h> void aes_decrypt_cbc(const unsigned char *ciphertext, int ciphertext_len, const unsigned char *key, const unsigned char *iv, unsigned char *plaintext) { AES_KEY aes_key; AES_set_decrypt_key(key, 128, &aes_key); AES_cbc_encrypt(ciphertext, plaintext, ciphertext_len, &aes_key, iv, AES_DECRYPT); } int main() { const unsigned char *ciphertext = (const unsigned char *)"待解密的密文"; const unsigned char *key = (const unsigned char *)"密钥"; const unsigned char *iv = (const unsigned char *)"初始化向量"; unsigned char plaintext[AES_BLOCK_SIZE * ((strlen((const char *)ciphertext) + AES_BLOCK_SIZE - 1) / AES_BLOCK_SIZE)]; memset(plaintext, 0, sizeof(plaintext)); aes_decrypt_cbc(ciphertext, strlen((const char *)ciphertext), key, iv, plaintext); printf("解密结果:%s\n", plaintext); return 0; } ``` 需要注意的是,以上代码使用的是 CBC 模式和 PKCS7 填充方式。同时,需要在编译时链接 OpenSSL 库,可以使用以下命令进行编译: ``` gcc aes_cbc_encrypt.c -o aes_cbc_encrypt -lcrypto gcc aes_cbc_decrypt.c -o aes_cbc_decrypt -lcrypto ``` 其中,`-lcrypto` 参数表示链接 OpenSSL 库。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值