发送RSA公钥的问题,javaME,bouncy城​​堡 [英] Problem transmiting a RSA public key, javaME , bouncy castle

查看:157
本文介绍了发送RSA公钥的问题,javaME,bouncy城​​堡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将实例消息传递应用程序从Java移植到JavaME,这也实现了加密。问题是我想将我的公钥发送到服务器。桌面客户端具有此作业的代码:

I'm working on the porting of an instance messaging application from Java to JavaME ,that also implements cryptography. The problem is that I want to send my public key to the server. The desktop client has this code for this job:

byte[] encoded_public_key=publick_key.getEncoded();

服务器有此代码来检索密钥:

And the server has this code to retrieve the key:

EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(encoded_public_key);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey puKey = keyFactory.generatePublic(publicKeySpec);

现在我已经看到了getEncoded的API,它表示它返回DER编码形式的密钥作为字节数组(http://www.docjar.com/docs/api/sun/s...tml#getEncoded)

Now I've looked the API for the getEncoded and it says that it returns the DER-encoded form of the key as a byte array (http://www.docjar.com/docs/api/sun/s...tml#getEncoded)

我的实现为在JavaME中是这样的:

My implementation for that in JavaME was this:

RSAPublicKeyStructure public_key_JAVAME=new RSAPublicKeyStructure(modulus,exponent);
byte[] DER_encoded_public_key_JAVAME=public_key_JAVAME.getDEREncoded();

//the getEncoded functions returns exact the same byte array.

但是,当我尝试使用服务器代码检索JavaME创建的DER编码密钥时,换句话说,我尝试这样:

However when I try to retrieve the JavaME created DER encoded key with the server code ,in other words when I try this:

EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(DER_encoded_public_key_JAVAME);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey puKey = keyFactory.generatePublic(publicKeySpec);

我得到

java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: IOException: algid parse error, not a sequence
at sun.security.rsa.RSAKeyFactory.engineGeneratePublic(RSAKeyFactory.java:188)
at java.security.KeyFactory.generatePublic(KeyFactory.java:304)

Caused by: java.security.InvalidKeyException: IOException: algid parse error, not a sequence
at sun.security.x509.X509Key.decode(X509Key.java:380)
at sun.security.x509.X509Key.decode(X509Key.java:386)
at sun.security.rsa.RSAPublicKeyImpl.<init>(RSAPublicKeyImpl.java:66)
at sun.security.rsa.RSAKeyFactory.generatePublic(RSAKeyFactory.java:281)
at sun.security.rsa.RSAKeyFactory.engineGeneratePublic(RSAKeyFactory.java:184)

有趣的是:正常Java中的DER编码密钥(使用getencoded()函数)是一个字节数组,长度为162个字节,而SAME密钥为DER enc在JavaME中使用弹跳城堡是140字节长。这两个DER编码的密钥不应该是相同的长度吗?我的意思是DER编码格式中的密钥是相同的,所以它们应该是一样的。

Interesting point : The DER encoded key from the normal Java (using the getencoded() function) is a byte array is 162 bytes long while the SAME key DER encoded in JavaME using bouncy castle is 140 bytes long. Shouldn't these 2 DER encoded key be of the same lenght?I mean it's the same key in DER encoded format so they should be the same.

我在做什么错误?

True我没注意到。问题是你知道如何从PublicKey创建一个subjectPublickeyInfo对象疯狂的我试过:

True I didn't notice that.Problem is do you know how to create a subjectPublickeyInfo object from a PublicKey in bouncyCastle? I've tried:

ByteArrayInputStream bIn = new ByteArrayInputStream(RSApublickey.toString().bybytes());
SubjectPublicKeyInfo info = new SubjectPublicKeyInfo((ASN1Sequence)new ASN1InputStream(bIn).readObject());

工作我也尝试过:

ByteArrayInputStream(RSApublicKeyStructure.getEncoded());
SubjectPublicKeyInfo info = new SubjectPublicKeyInfo((ASN1Sequence)new ASN1InputStream(bIn).readObject());

其实我确实希望不行,但是我不得不尝试。那么我怎么可以从RSAkeyparameters创建一个Subjectpublickeyinfo?(这是bouncy的城堡API的晦涩难懂的点之一我猜想)

Actually I did expect that not to work , but well I had to try it . So how can I create a Subjectpublickeyinfo from RSAkeyparameters?( This is one of the points where the obscurity of bouncy's castle API really shines I guess)

再次感谢你的回应你一直很有帮助。你把我放在正确的轨道上。

Again thank you for your response you've been of great help.You've put me on the right track.

推荐答案

DER编码只是编码的标准。说一个键是DER编码的,相当于说它是XML编码的:你需要在之间达成一致,就是DER / / XML编码才能对它进行解码。

DER-encoding is just a standard for encoding. Saying that a key is DER-encoded is equivalent to saying it is XML-encoded: you need to agree on how it is DER-/XML-encoded to be able to decode it.

在这种情况下,您的 RSAPublicKeyStructure.getEncoded()返回密钥作为ASN.1的$ DER编码 RSAPublicKey

In this case your RSAPublicKeyStructure.getEncoded() returns the key as the DER-encoding of an ASN.1 RSAPublicKey:

RSAPublicKey ::= SEQUENCE {
  modulus INTEGER, -- n
  publicExponent INTEGER -- e 
}

另一方面,X509EncodedKeySpec 希望被递交ASN.1的DER编码。 PublicKeyInfo

The X509EncodedKeySpec on the other hand expects to be handed the DER-encoding of an ASN.1 PublicKeyInfo:

PublicKeyInfo ::= SEQUENCE {
  algorithm AlgorithmIdentifier,
  PublicKey BIT STRING
}

使用BouncyCastle创建一个 PublicKeyInfo (由 GregS ):

To create a PublicKeyInfo using BouncyCastle do this (courtesy of GregS):

RSAPublicKeyStructure rsaPublicKey = /* ... */
AlgorithmIdentifier rsaEncryption = new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption, null); 
SubjectPublicKeyInfo publicKeyInfo = new SubjectPublicKeyInfo(rsaEncryption, rsaPublicKey);
byte[] encodedPublicKeyInfo = publicKeyInfo.getEncoded();

这篇关于发送RSA公钥的问题,javaME,bouncy城​​堡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆