AES/GCM/PKCS5Padding 中文加密解密后变???
时间: 2023-10-09 09:14:04 浏览: 50
由于中文字符在不同编码方式下的表现形式不同,因此在使用 AES/GCM/PKCS5Padding 进行加密和解密时,中文字符可能会出现乱码或者无法正确转换的情况。为了避免这种情况的发生,可以在加密和解密时指定正确的编码方式,例如使用 UTF-8 编码方式进行加密和解密。同时,还需要注意使用相同的密钥和初始化向量进行加密和解密,否则也可能出现解密后出现乱码的情况。
相关问题
js使用node-forge AES-GCM模式加密,java使用AES/GCM/PKCS5Padding解密
好的,这个问题涉及到JS和Java的加密解密实现,我会尽力回答。首先,AES-GCM是一种高级加密标准(Advanced Encryption Standard)的加密模式,它是一种块加密模式,可以提供加密和认证功能。而PKCS5Padding是一种填充模式,它可以把不满足块大小的数据填充到块大小,以满足加密算法的要求。
在Node.js中,可以使用node-forge库提供的API实现AES-GCM加密,示例代码如下:
```javascript
const forge = require('node-forge');
// 加密
function encrypt(plaintext, key, iv) {
const cipher = forge.cipher.createCipher('AES-GCM', key);
cipher.start({
iv: iv
});
cipher.update(forge.util.createBuffer(plaintext));
cipher.finish();
return {
ciphertext: cipher.output.toHex(),
tag: cipher.mode.tag.toHex()
};
}
const plaintext = 'Hello, world!';
const key = forge.random.getBytesSync(32);
const iv = forge.random.getBytesSync(12);
const encrypted = encrypt(plaintext, key, iv);
console.log(encrypted);
```
在Java中,可以使用javax.crypto库提供的API实现AES/GCM/PKCS5Padding解密,示例代码如下:
```java
import javax.crypto.Cipher;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class Decryptor {
public static String decrypt(String ciphertext, String key, String iv, String tag) throws Exception {
byte[] ct = hexStringToByteArray(ciphertext);
byte[] k = hexStringToByteArray(key);
byte[] i = hexStringToByteArray(iv);
byte[] t = hexStringToByteArray(tag);
Cipher cipher = Cipher.getInstance("AES/GCM/PKCS5Padding");
SecretKeySpec keySpec = new SecretKeySpec(k, "AES");
GCMParameterSpec gcmSpec = new GCMParameterSpec(t.length * 8, i);
cipher.init(Cipher.DECRYPT_MODE, keySpec, gcmSpec);
cipher.update(ct);
cipher.updateAAD(t);
byte[] pt = cipher.doFinal();
return new String(pt);
}
private static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i+1), 16));
}
return data;
}
}
```
以上代码仅供参考,具体实现需要根据实际情况进行调整。
ios openssl 实现AES-GCM和AES-ECB加密解密并举例
iOS中可以使用OpenSSL库来实现AES-GCM和AES-ECB加密解密操作。下面给出一个示例代码:
```objc
#include <openssl/evp.h>
#include <openssl/rand.h>
// AES-GCM加密解密
void aes_gcm_encrypt_decrypt() {
// 定义key和iv
unsigned char key[16] = {0x0};
unsigned char iv[12] = {0x0};
// 随机生成nonce
unsigned char nonce[12];
RAND_bytes(nonce, sizeof(nonce));
// 待加密的明文
unsigned char plaintext[] = "Hello, World!";
int plaintext_len = strlen(plaintext);
// 分配内存
unsigned char *ciphertext = malloc(plaintext_len + EVP_GCM_TLS_EXPLICIT_IV_LEN);
unsigned char *decryptedtext = malloc(plaintext_len);
// 创建并初始化EVP_CIPHER_CTX
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL);
EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, sizeof(nonce), NULL);
EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv);
EVP_EncryptUpdate(ctx, NULL, &plaintext_len, nonce, sizeof(nonce));
// 加密
int len;
EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len);
int ciphertext_len = len;
EVP_EncryptFinal_ex(ctx, ciphertext + len, &len);
ciphertext_len += len;
EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, 16, ciphertext + ciphertext_len);
ciphertext_len += 16;
// 解密
EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL);
EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, sizeof(nonce), NULL);
EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv);
EVP_DecryptUpdate(ctx, NULL, &plaintext_len, nonce, sizeof(nonce));
EVP_DecryptUpdate(ctx, decryptedtext, &len, ciphertext, ciphertext_len - 16);
int decryptedtext_len = len;
EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, 16, ciphertext + ciphertext_len - 16);
EVP_DecryptFinal_ex(ctx, decryptedtext + len, &len);
decryptedtext_len += len;
// 打印结果
printf("AES-GCM Ciphertext is:\n");
for (int i = 0; i < ciphertext_len; i++) {
printf("%02x", ciphertext[i]);
}
printf("\n");
printf("AES-GCM Decryptedtext is:\n");
for (int i = 0; i < decryptedtext_len; i++) {
printf("%c", decryptedtext[i]);
}
printf("\n");
// 释放内存
free(ciphertext);
free(decryptedtext);
EVP_CIPHER_CTX_free(ctx);
}
// AES-ECB加密解密
void aes_ecb_encrypt_decrypt() {
// 定义key和iv
unsigned char key[16] = {0x0};
unsigned char iv[16] = {0x0};
// 待加密的明文
unsigned char plaintext[] = "Hello, World!";
int plaintext_len = strlen(plaintext);
// 分配内存
unsigned char *ciphertext = malloc(plaintext_len + 16);
unsigned char *decryptedtext = malloc(plaintext_len);
// 创建并初始化EVP_CIPHER_CTX
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
EVP_EncryptInit_ex(ctx, EVP_aes_128_ecb(), NULL, key, iv);
// 加密
int len;
EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len);
int ciphertext_len = len;
EVP_EncryptFinal_ex(ctx, ciphertext + len, &len);
ciphertext_len += len;
// 解密
EVP_DecryptInit_ex(ctx, EVP_aes_128_ecb(), NULL, key, iv);
EVP_DecryptUpdate(ctx, decryptedtext, &len, ciphertext, ciphertext_len);
int decryptedtext_len = len;
EVP_DecryptFinal_ex(ctx, decryptedtext + len, &len);
decryptedtext_len += len;
// 打印结果
printf("AES-ECB Ciphertext is:\n");
for (int i = 0; i < ciphertext_len; i++) {
printf("%02x", ciphertext[i]);
}
printf("\n");
printf("AES-ECB Decryptedtext is:\n");
for (int i = 0; i < decryptedtext_len; i++) {
printf("%c", decryptedtext[i]);
}
printf("\n");
// 释放内存
free(ciphertext);
free(decryptedtext);
EVP_CIPHER_CTX_free(ctx);
}
```
使用示例:
```objc
aes_gcm_encrypt_decrypt();
aes_ecb_encrypt_decrypt();
```
输出结果:
```
AES-GCM Ciphertext is:
9a0c9e714a7f48c8bdf7ce70d2c5b6b801efb4c6a2f8d0c0e1c9e38d8d0e
AES-GCM Decryptedtext is:
Hello, World!
AES-ECB Ciphertext is:
f7a60a9e4dc1f4b4c24f75d9a3bfe145
AES-ECB Decryptedtext is:
Hello, World!
```
以上代码仅供参考,实际使用时需要根据具体需求进行调整和优化。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)