开发者

RSA in Java given n and e

开发者 https://www.devze.com 2023-02-01 20:59 出处:网络
My application receives the raw pieces of a public RSA key (n and e) and needs to use thes开发者_开发技巧e to encrypt a cipher text.I\'ve been trying to use BouncyCastle but my code isn\'t working.The

My application receives the raw pieces of a public RSA key (n and e) and needs to use thes开发者_开发技巧e to encrypt a cipher text. I've been trying to use BouncyCastle but my code isn't working. The problem arises in trying to create the X509EncodedKeySpec.

Can anyone help me get this working? Here's the code I have:

public static PublicKey getPublicKeyFromString(String key) throws Exception
{
    KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
    EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(Base64Encoder.decode(key));

    PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);
    return publicKey;
}

I guess the real problem is that n and e are separate and I don't know how to combine them.


Why are you not using new RSAPublicKeySpec(n,e)?


public static PublicKey getPublicKeyFromString(String key) throws Exception
        {
            BASE64Decoder b64 = new BASE64Decoder();
            KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
            EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(b64.decodeBuffer(key));
            PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);
            return publicKey;
        }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号