第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

在Java和golang中使用AES時(shí)獲得不同的結(jié)果(密碼文本)

在Java和golang中使用AES時(shí)獲得不同的結(jié)果(密碼文本)

肥皂起泡泡 2022-09-07 16:30:57
我正在嘗試將用于AES加密的Java代碼復(fù)制到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 對(duì)稱加密 解密 加密后加密文本:vSmrgH3qU+qEq+3ui0YvwCa6PDBcMyhgOlbh3+zzM+cON6feLk2u1iPW7lITD3vn 解密后的文本:AES 對(duì)稱加密解密戈朗代碼:package mainimport (    "crypto/aes"    "crypto/cipher"    "encoding/base64"    "fmt")
查看完整描述

1 回答

?
慕哥9229398

TA貢獻(xiàn)1877條經(jīng)驗(yàn) 獲得超6個(gè)贊

在go代碼中,您在GCM模式下使用AES,其中12個(gè)字節(jié)為零作為IV,但在java代碼中,您使用的是AES的默認(rèn)模式,這在不同的java版本中是不一樣的。


通過使用GCM模式(在BouncyCastle中提供)并設(shè)置相同的IV(12字節(jié)零),我得到了相同的輸出:


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


查看完整回答
反對(duì) 回復(fù) 2022-09-07
  • 1 回答
  • 0 關(guān)注
  • 462 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)