使用golang做rsa加密,java解密出现点问题
public static byte[] rsaDecrypt(String cryptograph, String private_key) throws Exception {
BASE64Decoder b64d = new BASE64Decoder();
byte[] keyByte = b64d.decodeBuffer(private_key);
PKCS8EncodedKeySpec s8ek = new PKCS8EncodedKeySpec(keyByte);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(s8ek);
Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA1AndMGF1Padding");
cipher.init(2, privateKey);
BASE64Decoder decoder = new BASE64Decoder();
byte[] b1 = decoder.decodeBuffer(cryptograph);
return cipher.doFinal(b1);
}
func RsaEncrypt(data string) (string, error) {
encrypt, err := rsa.EncryptOAEP(sha1.New(), rand.Reader, publicKey, []byte(data), []byte(""))
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(encrypt), nil
}
报错信息:
javax.crypto.BadPaddingException: Decryption error
临时犯傻! 用的公钥和私钥不是一对 已解决