I am working on Android application for Encryption and decryption using RSA algorithm. My intention is to decry-pt a file which is encrypted by server using RSACertificate.der and RSAPrivatekey.p12 files.
Now I have a Example.encriptedfile, RSACertificat.der and RSAPrivatekey.p12 files I would like to decrypt the above example.encrypted file using above keys in JAVA
The implementation for getting Privatekey And Decryption code using Cipher is
The file is example.encrypted file.
byte[] descryptedData = null;
try {
byte[] data = new byte[(int) file.length()];
new FileInputStream(file).read(data)
KeyStore keystore = KeyStore.getInstance("PKCS12");
keystore.load(con.getAssets().open("rsaPrivate.p12"), "password".toCharArray());
pk = (PrivateKey)keystore.getKey("1", "password".toCharArray());
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, pk );
descryptedData = cipher.doFinal(data);
} catch (Exception e) {
e.printStackTrace();
}
return new String(descryptedData);
The exception getting for the fallowing code is
java.security.InvalidKeyException: unknown key type passed to RSA
at com.android.org.bouncycastle.jcajce.provider.asymmetric.rsa.CipherSpi.engineInit(CipherSpi.java:277)
at com.android.org.bouncycastle.jcajce.provider.asymmetric.rsa.CipherSpi.engineInit(CipherSpi.java:381)
at javax.crypto.Cipher.init(Cipher.java:519)
at javax.crypto.Cipher.init(Cipher.java:479)
So can any one provide the suggestions and solutions to implement this
Thanks in advance.
But same