Java 代码
public static String encrypt(String paramString)throws Exception
{
return Base64Utils.encode(RSAUtils.encryptByPrivateKey(paramString.getBytes(), "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAIrrUGxh+yvNNI1c9hUg1rH+EtipI0nPk3zRm2Cj4mLDWLJ6DaTzdJTXTF3BYZaancWeG3QtBL+fITUi72InwBP7zaNG8uv/guwuhWT6V/YO7AaTrOFeTkg9NXuaFbn3hWVtZxQm2tIlaVa8snoNj3VGnPqIjXmGcxk4axuYd7sTAgMBAAECgYA43YhnRVh2nqJzd2k4Tt/zrmhyjhHm5fSetIKg9ZT3DrXhITsymYHQZ61X95AGATayLT1Zug/mjLIgOTO6f0ENkRQtjVCmKd8Yf/BeDEc5kRLUYDfSqoEydHK0+rCw5tJMgrAnQc5lHc+FVdGe2bOxKTEtZoss9VQ2jYuQ+Z5fUQJBANnvDOcI2OYSksX3PpHzO9F272xkmqYBRGkMc/a5RuOv1CY6FqMIkkloTf6nVl9y6XYV8gnHfbbI/wj4Q4UnPYsCQQCjLxyRYaOeEb/qOzSmFXytgMuCM9sr4eY9jpjzDgNWhpbtaVaf1QvSTXqN0zaUu4Se2tmWGX7zXw9p/dFf8DmZAkEAzl1o0FU2XhZ0WXVYEIhMunpvGSrirhNBHmAmZxjmoa/bqh8TVGpHa6+TO3JlfZioraL2QIBg8Ha/2VSNS0bvJQJALfCLaFpGh6+TicuVLNSLvwStRkB3CUmVWesVIAfn5KoLP1cSbfi6VUA+qkK18PVBhr8x1lHjLXyriDlOgmXMsQJAW9vD/IoBs4QJF87xF7tZvu/b1KRVgLM1edqOgVwMNbIQHBAXghjVjrpuln5w6z1dJ2cEjRP98OxKC0hqEIwIuQ==");
}
encrypt('159742081') --> GBn6RGIeEp8j/n35CgT5DdMmrvvNBtVCFQikO0vfJaYYrpEzBZ/F+5PkFLpzLDtYvQrj0Q/x/Fdxz3BtbEdLq57WFxB5MvkFxerWVeplA2vdlD7m+dgjsWyxBSbcVV1QX3UBNp+T3DtxL6uGuWUNxucy9yB5TOD3xCNchzGCnSU=
Python 代码
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5, PKCS1_OAEP
from base64 import b64decode, b64encode
key = 'MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAIrrUGxh+yvNNI1c9hUg1rH+EtipI0nPk3zRm2Cj4mLDWLJ6DaTzdJTXTF3BYZaancWeG3QtBL+fITUi72InwBP7zaNG8uv/guwuhWT6V/YO7AaTrOFeTkg9NXuaFbn3hWVtZxQm2tIlaVa8snoNj3VGnPqIjXmGcxk4axuYd7sTAgMBAAECgYA43YhnRVh2nqJzd2k4Tt/zrmhyjhHm5fSetIKg9ZT3DrXhITsymYHQZ61X95AGATayLT1Zug/mjLIgOTO6f0ENkRQtjVCmKd8Yf/BeDEc5kRLUYDfSqoEydHK0+rCw5tJMgrAnQc5lHc+FVdGe2bOxKTEtZoss9VQ2jYuQ+Z5fUQJBANnvDOcI2OYSksX3PpHzO9F272xkmqYBRGkMc/a5RuOv1CY6FqMIkkloTf6nVl9y6XYV8gnHfbbI/wj4Q4UnPYsCQQCjLxyRYaOeEb/qOzSmFXytgMuCM9sr4eY9jpjzDgNWhpbtaVaf1QvSTXqN0zaUu4Se2tmWGX7zXw9p/dFf8DmZAkEAzl1o0FU2XhZ0WXVYEIhMunpvGSrirhNBHmAmZxjmoa/bqh8TVGpHa6+TO3JlfZioraL2QIBg8Ha/2VSNS0bvJQJALfCLaFpGh6+TicuVLNSLvwStRkB3CUmVWesVIAfn5KoLP1cSbfi6VUA+qkK18PVBhr8x1lHjLXyriDlOgmXMsQJAW9vD/IoBs4QJF87xF7tZvu/b1KRVgLM1edqOgVwMNbIQHBAXghjVjrpuln5w6z1dJ2cEjRP98OxKC0hqEIwIuQ=='
rsakey = RSA.importKey(b64decode(key))
cipher = PKCS1_OAEP.new(rsakey)
b = cipher.encrypt(b'159742081')
print(b64encode(b))
cipher = PKCS1_v1_5.new(rsakey)
b = cipher.encrypt(b'159742081')
print(b64encode(b))
输出: b'ZkSAKcf9aGbRaOUaywZEQKIS4jdYpn+dlSxwpRFlmwjcNmVM7Cp0GQzz9yY2g7UdPVJCubRuPbKmyale2cuAqsVu+Y1n79EMoxSbShys0/o1o8V4UCuV+jP3e3EnyT86yoTyTDMXQzgDS+SRSkEF9U6db19T12/xEtfcKRcHing='
b'YQz8gjcslQ8QsYs9eU+Yu2owf55gHVedIksovrMczCRSaQ8vhml1ua6bAmf6xvg7zlw5BGZ7KlTygIgqKDvf6JgdZh5k2OoiK8uGYCFFCJFY0+3ZjhytG4KfwujqbCQWu3rZSaFZeF76MdFBQUWzJ2Q3V1BFTljF3Euu7X5KPpk='
各位老爷给看看,是哪里姿势不对
1
zeiyso 2017-03-08 23:02:58 +08:00 1
这是特性…… random padding
https://en.wikipedia.org/wiki/Padding_(cryptography) |
2
rogerchen 2017-03-08 23:04:55 +08:00 via iPad
为什么 java 里边不对 privatekey 的 b64 表示解码, python 不都先 decode 了么
|
3
iamxiaoq OP @rogerchen encryptByPrivateKey 这个方法里面会对 primarykey 进行 base64 解码
|
5
zeiyso 2017-03-08 23:16:48 +08:00
|
7
julyclyde 2017-03-09 13:27:48 +08:00
不需要关注是否一致,只需要关注能否互操作
|
10
shmily3929 2017-12-21 10:57:16 +08:00
您好 遇到同样的问题 .. 请问怎么解决的呢?
|