gpt4 book ai didi

java - 在 Java 和 golang 中使用 AES 时获得不同的结果(密文)

转载 作者:IT王子 更新时间:2023-10-29 02:35:28 26 4
gpt4 key购买 nike

我正在尝试将 AES 加密的 Java 代码复制到 Golang 中。但是我在 golang 中没有得到相同的输出

我试过下面的代码:

Java 代码:

package EncryptionTest;

import java.security.Key;
import java.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class EncryptionDecryptionAES {

static Cipher cipher;

public static void main(String[] args) throws Exception {
Key secretKey;
secretKey = (Key)new SecretKeySpec("0123456789012345".getBytes(), "AES");
cipher = Cipher.getInstance("AES");
String plainText = "AES Symmetric Encryption Decryption";
System.out.println("Plain Text Before Encryption: " + plainText);
String encryptedText = encrypt(plainText, secretKey);
System.out.println("Encrypted Text After Encryption: " + encryptedText);
String decryptedText = decrypt(encryptedText, secretKey);
System.out.println("Decrypted Text After Decryption: " + decryptedText);
}

public static String encrypt(String plainText, Key secretKey) throws Exception {
byte[] plainTextByte = plainText.getBytes();
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encryptedByte = cipher.doFinal(plainTextByte);
Base64.Encoder encoder = Base64.getEncoder();
String encryptedText = encoder.encodeToString(encryptedByte);
return encryptedText;
}

public static String decrypt(String encryptedText, Key secretKey) throws Exception {
Base64.Decoder decoder = Base64.getDecoder();
byte[] encryptedTextByte = decoder.decode(encryptedText);
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decryptedByte = cipher.doFinal(encryptedTextByte);
String decryptedText = new String(decryptedByte);
return decryptedText;
}
}

Java 代码输出:加密前的纯文本:AES对称加密解密加密后的密文:vSmrgH3qU+qEq+3ui0YvwCa6PDBcMyhgOlbh3+zzM+cON6feLk2u1iPW7lITD3vn解密后的解密文本:AES对称加密解密

Golang 代码:

package main

import (
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"fmt"
)

const NONCESIZE = 12

func main() {
key := []byte("0123456789012345")
plaintext := []byte("AES Symmetric Encryption Decryption")
block, err := aes.NewCipher(key)
if err != nil {
panic(err.Error())
}
nonce := make([]byte, NONCESIZE)
aesgcm, err := cipher.NewGCM(block)
if err != nil {
panic(err.Error())
}
ciphertext := aesgcm.Seal(nil, nonce, plaintext, nil)
fmt.Println("Encrypted Text is : ", base64.StdEncoding.EncodeToString(ciphertext))

}

Golang 代码输出:加密文本为:7UMh49c5Wqb2BzlttKBEnq5g4fxMK9oJs1EUDIgWzVwlY28k+qd/oFG9SJckBsaX6DHp

最佳答案

在 go 代码中,您在 GCM 模式下使用 AES,其中 12 个字节为零作为 IV,但在 java 代码中,您使用的是 AES 的默认模式,这在不同的 java 版本中是不同的。

通过使用 GCM 模式(在 BouncyCaSTLe 中提供)并设置相同的 IV(12 个字节为零)我得到了相同的输出:

package EncryptionTest;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

import java.security.Key;
import java.security.Security;
import java.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class EncryptionDecryptionAES {

static Cipher cipher;

public static void main(String[] args) throws Exception {
Security.addProvider(new BouncyCastleProvider());


Key secretKey;
secretKey = (Key)new SecretKeySpec("0123456789012345".getBytes(), "AES");

cipher = Cipher.getInstance("AES/GCM/NoPadding", "BC");

String plainText = "AES Symmetric Encryption Decryption";
System.out.println("Plain Text Before Encryption: " + plainText);
String encryptedText = encrypt(plainText, secretKey);
System.out.println("Encrypted Text After Encryption: " + encryptedText);
String decryptedText = decrypt(encryptedText, secretKey);
System.out.println("Decrypted Text After Decryption: " + decryptedText);
}

public static String encrypt(String plainText, Key secretKey) throws Exception {
byte[] plainTextByte = plainText.getBytes();


byte[] iv = new byte[12];
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivParameterSpec);

byte[] encryptedByte = cipher.doFinal(plainTextByte);
Base64.Encoder encoder = Base64.getEncoder();
String encryptedText = encoder.encodeToString(encryptedByte);
return encryptedText;
}

public static String decrypt(String encryptedText, Key secretKey) throws Exception {
Base64.Decoder decoder = Base64.getDecoder();
byte[] encryptedTextByte = decoder.decode(encryptedText);

byte[] iv = new byte[12];
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);

byte[] decryptedByte = cipher.doFinal(encryptedTextByte);
String decryptedText = new String(decryptedByte);
return decryptedText;
}
}

输出:

Plain Text Before Encryption: AES Symmetric Encryption Decryption
Encrypted Text After Encryption: 7UMh49c5Wqb2BzlttKBEnq5g4fxMK9oJs1EUDIgWzVwlY28k+qd/oFG9SJckBsaX6DHp
Decrypted Text After Decryption: AES Symmetric Encryption Decryption

关于java - 在 Java 和 golang 中使用 AES 时获得不同的结果(密文),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55370699/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com