douxidang9092 2019-03-27 05:59
浏览 513
已采纳

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

I am trying to replicate the java code for AES encryption into Golang. However I am not getting the same output in golang

I tried below code:

Java Code:

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 code output: Plain Text Before Encryption: AES Symmetric Encryption Decryption Encrypted Text After Encryption: vSmrgH3qU+qEq+3ui0YvwCa6PDBcMyhgOlbh3+zzM+cON6feLk2u1iPW7lITD3vn Decrypted Text After Decryption: AES Symmetric Encryption Decryption

Golang Code:

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 Code output: Encrypted Text is : 7UMh49c5Wqb2BzlttKBEnq5g4fxMK9oJs1EUDIgWzVwlY28k+qd/oFG9SJckBsaX6DHp

  • 写回答

1条回答 默认 最新

  • doubao12345 2019-03-27 07:35
    关注

    In the go code, you are using AES in GCM mode with 12 bytes of zero as IV, but in java code you are using default mode of AES which is not the same in different java versions.

    By using GCM mode(Provided in BouncyCastle) and setting the same IV(12 bytes of zero) I got same output:

    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;
        }
    }
    

    Output:

    Plain Text Before Encryption: AES Symmetric Encryption Decryption
    Encrypted Text After Encryption: 7UMh49c5Wqb2BzlttKBEnq5g4fxMK9oJs1EUDIgWzVwlY28k+qd/oFG9SJckBsaX6DHp
    Decrypted Text After Decryption: AES Symmetric Encryption Decryption
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

    报告相同问题?

    悬赏问题

    • ¥15 关于#tensorflow#的问题:有没有什么方法可以让机器自己学会像素风格的图片
    • ¥15 Oracle触发器字段变化时插入指定值
    • ¥15 docker无法进入容器内部
    • ¥15 qt https 依赖openssl 静态库
    • ¥15 python flask 报错
    • ¥15 改个密码引发的项目启动问题
    • ¥100 CentOS7单线多拨
    • ¥15 debian安装过程中老是出现无法将g21dr复制到g21dr怎么解决呀?
    • ¥15 如何用python实现跨工作簿的指定区域批量复制粘贴
    • ¥15 基于CH573f的雷迪安CR1400m通讯代码