java - 从java到golang对话的AESBase64加密解密
问题描述
我想将以下代码从 java 重写为 golang。在这里,我主要是在寻找一个可以与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));
}
}
解决方案
您可以使用encoding/base64包来解码 Base64 编码的字符串:
decoded, err := base64.StdEncoding.DecodeString(s)
推荐阅读
- elixir - 如何在 Elixir 中获得二进制文件的长度?
- android - 如何在应用程序内获取 Firebase 云消息,而不是推送通知
- genexus - 单独工作面板
- powerbi - 拉取对应于最大值或最小值的行的最佳方法
- javascript - 注入 DOM 后如何使用 API 处理 Wistia 视频
- javascript - 猫鼬 findOneAndUpdate 为 err 和 doc 返回 null(使用 Jest)
- tfs - 如何查看我在 VSTS 项目中所做的更改?
- php - Laravel CSV 导入 - 我可以使用请求验证吗?
- intellij-idea - 使用 IntelliJ Idea 和 SonarLint 连接到 SonarQube 服务器时出现错误
- php - 一个while循环中有两个查询