0

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

Ganesh
  • 924
  • 2
  • 12
  • 34

1 Answers1

0

If it's encrypted with the private key it isn't encrypted at all, as anyone who can get the public key can decrypt it, which could be anybody.

If the intention was really to encrypt it, it should have been encrypted with the public key.

Possibly however the intention was to sign it, in which case what you have to do is verify it against the public key. For which you need the public key, which is in the certificate. So you load the certificate into a Certificate, using the java.security.cert API and then use a java.security.Signature object to verify the signature with, using the public key obtained from the Certificate.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Yes I am using the same API like KeyStore keystore = KeyStore.getInstance("PKCS12"); keystore.load(con.getAssets().open("rsaPrivate.p12"), "password".toCharArray()); pk = (PrivateKey)keystore.getKey("1", "password".toCharArray()); – Ganesh May 15 '14 at 08:51
  • The above code is able to getting Privatekey but when doing decryption using Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.DECRYPT_MODE, pk); descryptedData = cipher.doFinal(data); Getting an exception of java.security.InvalidKeyException: unknown key type passed to RSA at com.android.org.bouncycastle.jcajce.provider.asymmetric.rsa.CipherSpi.engineInit(CipherSpi.java:277) – Ganesh May 15 '14 at 09:21
  • You don't seem to have read a word I said. In any case you need to post those exceptions and their entire stack traces and your code. Edit it into your question. And indicate which line number is which. – user207421 May 15 '14 at 09:39