开发者

Construct Public Key from Big Integers?

I would like construct a public key which is constructed using C# XML RSAKeys, however I would like to reconstruct it using java, The problem is I receive the M & E themselves from the Key as bytes values, and to construct the key I've to use two BigIntegers, How do I construct the public key ?

Edit: The problem is the mod, exp byte arrays that was base64 decoded, are the M,N of the Public Key...

    byte[] 开发者_如何学Pythonmod  = Base64.decodeBase64(modulus.getBytes());
    byte[] exp  = Base64.decodeBase64(exponent.getBytes());

    int[] copyMod = new int[mod.length];

    for (int x = 0; x < mod.length; x++) {
        copyMod[x] = unsignedToBytes((byte) mod[x]);
    }

    int[] copyExp = new int[exp.length];

    for (int x = 0; x < exp.length; x++) {
        copyExp[x] = unsignedToBytes((byte) exp[x]);
    }

    String Mod =  Arrays.toString(copyMod);
    String Exp = Arrays.toString(copyExp);

    BigInteger m = new BigInteger(Mod.getBytes());
    BigInteger e = new BigInteger(Exp.getBytes());

        java.security.spec.RSAPublicKeySpec spec = new java.security.spec.RSAPublicKeySpec(m, e);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PublicKey publicKey = keyFactory.generatePublic(spec);


You don't tell us what object type you want. But if you want an RSA public key, you can do (BouncyCastle):

RSAPublicKey rsaPubKey = new JCERSAPublicKey(
    new RSAKeyParameters(false, modulus, exponent));

See the class definition.


java.security.spec.RSAPublicKeySpec spec = new java.security.spec.RSAPublicKeySpec(m, e);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(spec);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜