gpt4 book ai didi

java - RSA OAEP , Golang 解密 , Java 加密

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

Android客户端加密报文,java代码

Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING");

byte[] publicBytes = Base64.decode(Configs.PUBLIC_KEY.getBytes("UTF-8"),Base64.DEFAULT);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicBytes);

KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey pubKey = keyFactory.generatePublic(keySpec);

cipher.init(Cipher.ENCRYPT_MODE, pubKey);

String plaintext = "test";

byte[] encryptedBytes = cipher.doFinal(plaintext.getBytes("UTF-8"));
String chipertext = Base64.encodeToString(encryptedBytes,Base64.DEFAULT);
Log.d(TAG,"encrypted (chipertext) = " + chipertext);

Golang服务器解密消息,golang代码

func RsaDecrypt(encryptedString string) (string, error) {
base64DecodeBytes, err := base64.StdEncoding.DecodeString(encryptedString)
if err != nil {
return "", err
}
privateKeyBlock, _ := pem.Decode([]byte(privateKey))
var pri *rsa.PrivateKey
pri, parseErr := x509.ParsePKCS1PrivateKey(privateKeyBlock.Bytes)
if parseErr != nil {
return "", parseErr
}
decryptedData, decryptErr := rsa.DecryptOAEP(sha1.New(), rand.Reader, pri, base64DecodeBytes, nil)
if decryptErr != nil {
return "", decryptErr
}

return string(decryptedData), nil
}

和golang中的错误:crypto/rsa: decryption error

我无法在服务器上更改代码,那么如何更改Java中的代码?

需要你的帮助

谢谢。

最佳答案

两个程序中的散列函数不一样。Java版本使用SHA-256,Go版本使用SHA-1

你可以尝试在Java端使用RSA/ECB/OAEPWithSHA-1AndMGF1Padding

关于java - RSA OAEP , Golang 解密 , Java 加密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37802869/

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