AESBase64加密解密从Java到Golang的对话

huangapple go评论90阅读模式
英文:

AESBase64 encryption decryption from java to golang conversation

问题

我可以帮你将以下Java代码重写为Go语言:

package main

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

func main() {
	encryptedText := "encrypted text"
	secretKey := "$sgettng@&Key%"

	decryptedText, err := decrypt(encryptedText, secretKey)
	if err != nil {
		fmt.Println("Decryption error:", err)
		return
	}

	fmt.Println("Decrypted text:", decryptedText)
}

func decrypt(encryptedText string, secretKey string) (string, error) {
	cipheredBytes, err := base64.StdEncoding.DecodeString(encryptedText)
	if err != nil {
		return "", err
	}

	key := []byte(secretKey)
	block, err := aes.NewCipher(key)
	if err != nil {
		return "", err
	}

	iv := make([]byte, aes.BlockSize)
	stream := cipher.NewCTR(block, iv)

	decryptedBytes := make([]byte, len(cipheredBytes))
	stream.XORKeyStream(decryptedBytes, cipheredBytes)

	return string(decryptedBytes), nil
}

请注意,此代码仅提供了解密功能。如果您需要加密功能,请让我知道,我可以为您提供相应的代码。

英文:

I want to rewrite the following code from java to golang.
Here mostly I am looking for a function which can work same as byte[] cipheredBytes = Base64.getDecoder().decode((encryptedText));

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.KeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Base64;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class AESBase64UsedForPartners {
    private final String characterEncoding = "UTF-8";
    private final String cipherTransformation = "AES/CBC/PKCS5Padding";
    private final String aesEncryptionAlgorithm = "AES";
    static String SecretKey = "$sgettng@&Key%";
public String encrypt(String plainText) throws UnsupportedEncodingException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
    byte[] plainTextbytes = plainText.getBytes(characterEncoding);
    byte[] keyBytes = getKeyBytes(SecretKey);
    byte[] str = Base64.getEncoder().encode(encrypt(plainTextbytes, keyBytes, keyBytes));
    return new String(str);
}

public byte[] encrypt(byte[] plainText, byte[] key, byte[] initialVector) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
    Cipher cipher = Cipher.getInstance(cipherTransformation);
    SecretKeySpec secretKeySpec = new SecretKeySpec(key, aesEncryptionAlgorithm);
    IvParameterSpec ivParameterSpec = new IvParameterSpec(initialVector);
    cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
    plainText = cipher.doFinal(plainText);
    return plainText;
}

public String decrypt(String encryptedText) throws KeyException, GeneralSecurityException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, IOException {
    byte[] cipheredBytes = Base64.getDecoder().decode((encryptedText));
    byte[] keyBytes = getKeyBytes(SecretKey);
    return new String(decrypt(cipheredBytes, keyBytes, keyBytes), characterEncoding);
}

public byte[] decrypt(byte[] cipherText, byte[] key, byte[] initialVector) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
    Cipher cipher = Cipher.getInstance(cipherTransformation);
    SecretKeySpec secretKeySpecy = new SecretKeySpec(key, aesEncryptionAlgorithm);
    IvParameterSpec ivParameterSpec = new IvParameterSpec(initialVector);
    cipher.init(Cipher.DECRYPT_MODE, secretKeySpecy, ivParameterSpec);
    cipherText = cipher.doFinal(cipherText);
    return cipherText;
}


private byte[] getKeyBytes(String key) throws UnsupportedEncodingException {
    byte[] keyBytes = new byte[16];
    byte[] parameterKeyBytes = key.getBytes(characterEncoding);
    System.arraycopy(parameterKeyBytes, 0, keyBytes, 0, Math.min(parameterKeyBytes.length, keyBytes.length));
    return keyBytes;
}

public static void main(String a[]) throws GeneralSecurityException, IOException {
    String str = new AESBase64UsedForPartners().encrypt("Text");
    System.out.println("Encrypted---->" + str);
    System.out.println(new AESBase64UsedForPartners().decrypt(str));
}

}

答案1

得分: 1

你可以使用encoding/base64包来解码Base64编码的字符串:

decoded, err := base64.StdEncoding.DecodeString(s)
英文:

You can use the encoding/base64 package for decoding Base64 encoded strings:

decoded, err := base64.StdEncoding.DecodeString(s)

huangapple
  • 本文由 发表于 2021年9月16日 15:57:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/69204482.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定