dsfdsfds521521 2016-06-14 04:16
浏览 745
已采纳

RSA OAEP,Golang解密,Java加密

The Android client encrypt message, the java code

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);

The Golang server decrypt message, the golang code

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
}

and the error in golang : crypto/rsa: decryption error

I can't change the code on the server, so how to change the code in Java?

Need your help

Thx.

  • 写回答

1条回答 默认 最新

  • doufutao4428 2016-06-14 06:59
    关注

    The hash functions in the two programs are not the same. In Java version, SHA-256 is used, but in Go version, SHA-1 is used.

    You can try to use RSA/ECB/OAEPWithSHA-1AndMGF1Padding in the Java side.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程