Home > Mobile >  RSA decryption, the same code in Java it is normal that Android will be in front of the correct resu
RSA decryption, the same code in Java it is normal that Android will be in front of the correct resu

Time:10-06

JDK version is 1.8 (is done with the installation of Android Studio to install the JDK)
Public key:
 MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCdHc7rWSuL8JCVRY0PwaeIsHX/gel/hvKA3zi5eH2C2bk9HFGbsNKX2Wg + rbXse0SfLLkF42gzD0ghlc7JeVC8DIJI4ef33UUjfcDyQ1xtsuFZDsIg4hFJPAeuaW0CNM1ydl2c2IzIiNL6AL0q736vkl0rqOS5IKw74/K2uQFcgQIDAQAB 

私钥:
MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJ0dzutZK4vwkJVFjQ/Bp4iwdf+B6X+G8oDfOLl4fYLZuT0cUZuw0pfZaD6ttex7RJ8suQXjaDMPSCGVzsl5ULwMgkjh5/fdRSN9wPJDXG2y4VkOwiDiEUk8B65pbQI0zXJ2XZzYjMiI0voAvSrvfq+SXSuo5LkgrDvj8ra5AVyBAgMBAAECgYA1OfXxcQH5pIO2rhs9rUNOYUl4R9CMrXDWC8kxvAn5HD1O4Jsc/Wg6nSJdqYeqF5CFxCbiQcn5EkJDbnbhdkHNCRgbfz/TubcX3KQtax0CRsGwRVj98uQNmoNucVk+J5UPX9LyXXWr+d6WxG2hIFVMg9oo/A/kZe/5v+n6X72tgQJBAO6OdcG5bEpDesUM7Oq+2NzRzkA8dbdkOXbV+811ylFy2IARLdNnaAw3VgjjDQ2DnLAYr/Ows+Y37cKmuuN4R7kCQQComt8l3NQy1S3m2Qh3smOyJ7ipv5U2yHPNZFQekE2IaRKbY3JEWxHDB5rNHY1cJygWMWzanv66I4DL2+dtqw8JAkBoxzefhAK1R5g3zmqM3JBBxQETI7sMdr/qmygC4TaguZGwBlubW/LOUsu+EYPdKK58hgtaPIV3m58ZfMPfPyg5AkBwHUPDTpuADcePGMSLq4Clx8oAfle/bP2sF0nkZPpZed0YnMEv+QrPkC/8suj73UHHuCf1HAKwTcloRZsXjsbZAkEAqPM4NgdRZhUAwRiI1ZBeED4/pIKRQyb3QHPLvuBuvLpxALGVDfxKHHqPz28AAm3+Ska4EERrtIveAq826IdqKA==

Encrypted content:
 MGXvX + LVhDJPhrJXnZai6CDvg8Tgs8eIzFe0i5XKhmu5wcavibVjsYm1 + 3 omoxl3rfs07vq1gm1hqwhtsb9edp4wffrvhdxhcq3tdnocjwf2bycoubiqljx0nysvocprwgsnsvjyknxyhoovrv1wer1v9fmio7u3ju6qo2xv1us=

The right decryption result: what __
Java code: (online copy, and add a constructor)
Because using Java Android. Util. Sdk26 Base64 need, but I want to be compatible to sdk21, so in the Java package the following code into the jar and then direct call...
//
//the Source code recreated the from a. class file by IntelliJ IDEA
//(powered by Fernflower decompiler)
//

Package MyBase64;

Import the Java. Security. KeyFactory;
Import the Java. Security. PrivateKey;
Import the Java. Security. PublicKey;
Import the Java. Security. Signature;
Import the Java. Security. Spec. PKCS8EncodedKeySpec;
Import the Java. Security. Spec. X509EncodedKeySpec;
Import the Java. Util. Base64;
The import javax.mail. Crypto. Cipher;

Public class MyBase64 {
Public static final String KEY_ALGORITHM="RSA";
Public static final String SIGNATURE_ALGORITHM="MD5withRSA";
Private static final ints KEY_SIZE=1024;
Private static final String PUBLIC_KEY="RSAPublicKey";
Private static final String PRIVATE_KEY="RSAPrivateKey";
Private static String str_pubK="";
Private static String str_priK="";

Public MyBase64 (String pubK, String priK) {
Str_priK=priK;
Str_pubK=pubK;
}

Public static PublicKey getPublicKey (String key) throws the Exception {
Byte [] keyBytes=Base64. GetDecoder (). The decode (key);
X509EncodedKeySpec keySpec=new X509EncodedKeySpec (keyBytes);
KeyFactory KeyFactory=KeyFactory. GetInstance (" RSA ");
PublicKey PublicKey=keyFactory. GeneratePublic (keySpec);
Return publicKey;
}

Public PrivateKey getPrivateKey (String key) throws the Exception {
Byte [] keyBytes=Base64. GetDecoder (). The decode (key);
PKCS8EncodedKeySpec keySpec=new PKCS8EncodedKeySpec (keyBytes);
KeyFactory KeyFactory=KeyFactory. GetInstance (" RSA ");
PrivateKey PrivateKey=keyFactory. GeneratePrivate (keySpec);
Return privateKey;
}

Public byte [] sign (byte [] data) throws the Exception {
PrivateKey priK=this. GetPrivateKey (str_priK);
Signature sig=Signature. GetInstance (" MD5withRSA ");
Sig. InitSign (priK);
Sig. Update (data);
Return sig. Sign ();
}

Public Boolean verify (byte [] data, byte [] sign) throws the Exception {
PublicKey pubK=getPublicKey (str_pubK);
Signature sig=Signature. GetInstance (" MD5withRSA ");
Sig. InitVerify (pubK);
Sig. Update (data);
Return sig. Verify (sign);
}

Public byte [] encrypt (byte [] bt_plaintext) throws the Exception {
PublicKey PublicKey=getPublicKey (str_pubK);
RSA Cipher Cipher=Cipher. GetInstance (" ");
Cipher. The init (1, publicKey);
Byte [] bt_encrypted=cipher. DoFinal (bt_plaintext);
Return bt_encrypted;
}

Public byte [] decrypt (String bt_encrypted) throws the Exception {
PrivateKey PrivateKey=this. GetPrivateKey (str_priK);
RSA Cipher Cipher=Cipher. GetInstance (" ");
Cipher. The init (2, privateKey);
Return cipher. DoFinal (Base64. GetDecoder (). The decode (bt_encrypted));
}

Public static void main (String [] args) throws the Exception {
String pub="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCdHc7rWSuL8JCVRY0PwaeIsHX/gel/hvKA3zi5eH2C2bk9HFGbsNKX2Wg + rbXse0SfLLkF42gzD0ghlc7JeVC8DIJI4ef33UUjfcDyQ1xtsuFZDsIg4hFJPAeuaW0CNM1ydl2c2IzIiNL6AL0q736vkl0rqOS5IKw74/K2uQFcgQIDAQAB";
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related