问题外包RSA加密和解密 [英] Problems outsourcing RSA encryption and decryption
问题描述
我有RSA加密和解密的问题。我正在开发在Android和想外包的RSA加密和解密。我的源$ C $ C行之有效之前,我想外包出去。
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.
我创建了一个私钥和公钥并将其保存为private.key和public.key。该错误是造成这种方法一个ClassNotFoundException:
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;
}
我看了看logcat的和得到这个错误信息:
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.
编辑:我发现这个问题是不是由外包code引起的。所以我猜这个话题可以被标记为解决。
I found out that the problem is not caused by outsourcing the code. So i guess the topic can be marked as solved.
推荐答案
RSAPublicKey
和 RSAPrivateKey
是接口。当你得到一个键
你实际上这个接口的加密提供者收到的实现。这些供应商为不同的Java平台不同(尽管,至少官方的Android / Dalvik的甚至不是一个Java平台)。所以,你应该休想系列化工作,除非你的工作在同一个平台上。
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.
然而,有方法可以在Java中的公共和私有密钥序列;在键
接口包含 getEn codeD()
方法,返回键的最常见的二进制编码。在 RSAPublicKey
的情况下,这是X5.09执行subjectKeyIdentifier中的PKCS#1编码。在 RSAPrivateKey
的情况下,这是的内的PKCS#8编码缠PKCS#1定义的结构。使用这些可再presented X509En codedKeySpec
和 PKCS8En codedKeySpec
和转换回使用RSA密钥的KeyFactory
。
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
.
请注意私钥的不会的是,如果你调用加密 getEn codeD
。通常情况下,你不希望在运输私钥的所有的,如果你这样做,你真的应该加密。您可以使用 Cipher.wrap
和 Cipher.unwrap
的方法做到这一点。
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.
这篇关于问题外包RSA加密和解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!