[英]C++/Openssl Get RSA key from encoded bytes (encoded by java)
Does somebody know how I can create an RSA key in C++ from an encoded byte array? 有人知道如何从编码的字节数组中用C ++创建RSA密钥吗?
My problem is that I try to develop a C++ client that is interacting with a server which is coded in Java. 我的问题是我尝试开发一个与用Java编码的服务器交互的C ++客户端。 Well in Java the client receives the rsa key encoded as an byte array, decodes it to a RSA RSAPublicKey and encrypts a message with this key.
在Java中,客户端接收编码为字节数组的rsa密钥,将其解码为RSA RSAPublicKey并使用此密钥加密消息。
The java server/client code: java服务器/客户端代码:
public static PublicKey decodePublicKey(byte[] p_75896_0_)
{
try
{
X509EncodedKeySpec var1 = new X509EncodedKeySpec(p_75896_0_);
KeyFactory var2 = KeyFactory.getInstance("RSA");
return var2.generatePublic(var1);
}
catch (NoSuchAlgorithmException var3)
{
;
}
catch (InvalidKeySpecException var4)
{
;
}
field_180198_a.error("Public key reconstitute failed!");
return null;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
this.publicKey = CryptManager.decodePublicKey(data.readByteArray());
After that the client is doing some encrypting stuff with his key. 之后,客户端正在使用他的密钥进行一些加密。
The key gets sent like this: 密钥发送方式如下:
public static final KeyPair keys;
static
{
try
{
KeyPairGenerator generator = KeyPairGenerator.getInstance( "RSA" );
generator.initialize( 1024 );
keys = generator.generateKeyPair();
} catch ( NoSuchAlgorithmException ex )
{
throw new ExceptionInInitializerError( ex );
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
byte[] pubKey = keys.getPublic().getEncoded();
writeBytes(pubKey);
My problem is how to get the key from the byte array in C++. 我的问题是如何从C ++中的字节数组中获取密钥。
Update: Im currently working on this code: 更新:我目前正在处理此代码:
char* publicKey = ...
int publicKeyLength = 162;
EVP_PKEY* key = EVP_PKEY_new();
if(d2i_PUBKEY(&key, (const unsigned char**) &publicKey, publicKeyLength) != 0){
logError("Problem!");
}
logMessage("Key: "+to_string((uint64_t) (void*) key));
Well my problem now is that i have an SIGSEGV error on the third line and dont know what this course. 我现在的问题是我在第三行有一个SIGSEGV错误,不知道这个课程是什么。 Well the key should be valid.
那么关键应该是有效的。
What Java returns for the public key is a SubjectPublicKeyInfo structure, which doesn't just contain the (PKCS#1 encoded) values for the public key, but also the key identifier etc. Java为公钥返回的是SubjectPublicKeyInfo结构,它不仅包含公钥的(PKCS#1编码)值,还包含密钥标识符等。
So to decode this you have to type "decode SubjectPublicKeyInfo openssl" in your favorite search engine. 所以要解码这个,你必须在你最喜欢的搜索引擎中键入“decode SubjectPublicKeyInfo openssl”。 Then you'll find (after some scrolling) the following information from here :
然后你会发现(后一些滚动)从下面的信息在这里 :
d2i_PUBKEY() and i2d_PUBKEY() decode and encode an EVP_PKEY structure using SubjectPublicKeyInfo format. They otherwise follow the conventions of other ASN.1 functions such as d2i_X509().
Obviously you'd need the decoding algorithm. 显然你需要解码算法。
Note that openssl is C so beware of buffer overruns when decoding stuff. 请注意,openssl是C,因此在解码内容时要注意缓冲区溢出。 I'd rather have a 1024 bit RSA key that is used with secure software than a 2048 bit key with software full of buffer overruns.
我宁愿使用1024位RSA密钥,与安全软件一起使用,而不是2048位密钥,软件充满缓冲区溢出。
Needless to say you need to trust the public key before importing it. 不用说在导入之前需要信任公钥。 There is a reason why it is called the public key infrastructure (PKI).
将其称为公钥基础结构 (PKI)是有原因的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.