外包RSA加密和解密的问题

时间:2021-03-30 18:30:01

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.

我有RSA加密和解密的问题。我正在开发android,并希望外包RSA加密和解密。在我尝试外包之前,我的源代码运行良好。

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:

我创建了一个私钥和公钥,并将其保存为private.key和public.key。该错误是由此方法引起的ClassNotFoundException:

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:

我查看了logcat并得到了以下错误消息:

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 个解决方案

#1


0  

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.

RSAPublicKey和RSAPrivateKey是接口。当您获得密钥时,您实际上会收到此接口的加密提供程序的实现。这些提供程序因不同的Java平台而不同(尽管至少在官方上,Android / Dalvik甚至不是Java平台)。因此,除非您在同一平台上工作,否则不应期望序列化工作。

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.

然而,有一些方法可以在Java中序列化公钥和私钥; Key接口包含getEncoded()方法,该方法返回密钥的最常见二进制编码。在RSAPublicKey的情况下,这是X5.09 SubjectKeyIdentifier中的PKCS#1编码。在RSAPrivateKey的情况下,这是围绕PKCS#1定义的结构的内部PKCS#8编码。这些可以使用X509EncodedKeySpec和PKCS8EncodedKeySpec表示,并使用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.

请注意,如果调用getEncoded,则不会加密私钥。通常你根本不想传输私钥,如果你这样做,你应该真正加密它们。您可以使用Cipher.wrap和Cipher.unwrap方法执行此操作。

#1


0  

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.

RSAPublicKey和RSAPrivateKey是接口。当您获得密钥时,您实际上会收到此接口的加密提供程序的实现。这些提供程序因不同的Java平台而不同(尽管至少在官方上,Android / Dalvik甚至不是Java平台)。因此,除非您在同一平台上工作,否则不应期望序列化工作。

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.

然而,有一些方法可以在Java中序列化公钥和私钥; Key接口包含getEncoded()方法,该方法返回密钥的最常见二进制编码。在RSAPublicKey的情况下,这是X5.09 SubjectKeyIdentifier中的PKCS#1编码。在RSAPrivateKey的情况下,这是围绕PKCS#1定义的结构的内部PKCS#8编码。这些可以使用X509EncodedKeySpec和PKCS8EncodedKeySpec表示,并使用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.

请注意,如果调用getEncoded,则不会加密私钥。通常你根本不想传输私钥,如果你这样做,你应该真正加密它们。您可以使用Cipher.wrap和Cipher.unwrap方法执行此操作。