给出n和e的Java中的RSA

时间:2022-08-27 21:34:20

My application receives the raw pieces of a public RSA key (n and e) and needs to use these 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.

我的应用程序接收公共RSA密钥(n和e)的原始部分,并需要使用它们来加密密文。我一直在尝试使用BouncyCastle,但我的代码无效。尝试创建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.

我想真正的问题是n和e是分开的,我不知道如何组合它们。

2 个解决方案

#1


3  

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

你为什么不使用新的RSAPublicKeySpec(n,e)?

#2


-1  

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;
        }

#1


3  

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

你为什么不使用新的RSAPublicKeySpec(n,e)?

#2


-1  

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;
        }