Problems outsourcing RSA encryption and decryption

2020-03-04 08:38发布

问题:

i have a problem with RSA encryption and decryption. I'm developing in android and would like to outsource the RSA encryption and decryption. My source code worked well before i tried to outsource it.

I created a private key and public key and saved it as private.key and public.key. The error is a ClassNotFoundException caused by this method:

public Key getPrivateKey(){
  try {
    InputStream fis = activity.getResources().openRawResource(R.raw.private);
    ObjectInputStream ois = new ObjectInputStream(fis);
    Key RSAprivateKey = (Key)ois.readObject();
    return RSAprivateKey;
  }
  catch (FileNotFoundException e) {
    Log.e("FileNotFound","FileNotFound");
    e.printStackTrace();
  } catch (IOException e) {
    Log.e("IOEXception","IOEXception");
    e.printStackTrace();
  } catch (ClassNotFoundException e) {
    Log.e("ClassNotFound","ClassNotFound");
    Log.e("Errro", "Error: "+ e.getMessage());
    Log.e("error", e.toString());
    e.printStackTrace();
  }
  return null;
}

I looked at the logcat and got this error message:

E/ClassNotFound(1205): ClassNotFound
03-19 13:54:52.176: E/Errro(1205): Error: 
com.android.org.bouncycastle.jce.provider.JCERSAPrivateCrtKey
03-19 13:54:52.176: E/error(1205): java.lang.ClassNotFoundException: 
com.android.org.bouncycastle.jce.provider.JCERSAPrivateCrtKey

I hope you understand my problem, because English is not my native language.

Edit: I found out that the problem is not caused by outsourcing the code. So i guess the topic can be marked as solved.

回答1:

RSAPublicKey and RSAPrivateKey are interfaces. When you get a Key you actually receive an implementation by the cryptographic provider of this interface. These providers differ for different Java platforms (although, at least officially, Android/Dalvik isn't even a Java platform). So you should never expect serialization to work unless you are working on the same platform.

There are however ways to serialize public and private keys in Java; the Key interface contains the getEncoded() method which returns the most common binary encoding of the key. In the case of RSAPublicKey this is the PKCS#1 encoding within X5.09 SubjectKeyIdentifier. In the case of RSAPrivateKey this is the inner PKCS#8 encoding wrapped around the PKCS#1 defined structure. These can be represented using X509EncodedKeySpec and PKCS8EncodedKeySpec and converted back into keys using an RSA KeyFactory.

Note that the private key will not be encrypted if you call getEncoded. Normally you don't want to transport private keys at all, and if you do you should really encrypt them. You can do this using the Cipher.wrap and Cipher.unwrap methods.