RSA OAEP、Golang解密、Java加密

RSA OAEP、Golang解密、Java加密,java,go,cryptography,rsa,Java,Go,Cryptography,Rsa,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(publicByt

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中的代码呢

需要你的帮助吗


Thx.

这两个程序中的哈希函数不同。 在Java版本中,使用了
SHA-256
,但在Go版本中,使用了
SHA-1

您可以尝试在Java端使用RSA/ECB/OAEPWithSHA-1和MGF1padding