Wednesday, 21 August 2013

Android : RSA Encrypt and Decrypt but when call it error

Android : RSA Encrypt and Decrypt but when call it error

I am implementing for RSA Encryption and Decryption in Android. I can
Perform Encryption(publickey) very well.But In Decryption (privatekey) I
got an Exception..
java.security.spec.InvalidKeySpecException:java.lang.RuntimeException:error:0D0680A8:an1
encoding routines:ASNl_CHECK_TLEN:wrong tag
when i call this function
try
{
encrypt_text.setText(decrypt(filecopy1,text_cipher));
}
catch(Exception e)
{
e.printStackTrace();
Toast.makeText(getBaseContext(),
e.toString(),Toast.LENGTH_LONG).show();
System.out.println("Exception is>>"+e);
}
public static String decrypt(String filePath1, String encrypted)
throws IOException, NoSuchAlgorithmException,
InvalidKeySpecException,NoSuchPaddingException, InvalidKeyException,
IllegalBlockSizeException, BadPaddingException {
InputStream stream = new FileInputStream(filePath1);
byte[] encodedKey1 = new byte[stream.available()];
stream.read(encodedKey1);
X509EncodedKeySpec publicKeySpec1 = new X509EncodedKeySpec(encodedKey1);
KeyFactory kf1 = KeyFactory.getInstance("RSA");
PublicKey pkPublic1 = kf1.generatePublic(publicKeySpec1);
Cipher pkCipher1 = Cipher.getInstance("RSA/ECB/PKCS1PADDING");
pkCipher1.init(Cipher.DECRYPT_MODE, pkPublic1);
byte[] decoded = Base64Coder.decode(encrypted);
byte[] decryptedInByte = pkCipher1.doFinal(decoded);
return new String(decryptedInByte);
// return encrypted;
}
import a privatekey.when i call will give them above pic. thank in advance.

No comments:

Post a Comment