从大整数构建公钥?

时间:2011-10-09 13:15:31

标签: java c#-4.0 cryptography rsa

我想构建一个使用C#XML RSAKeys构建的公钥,但是我想用java重建它,问题是我收到了M& E本身来自Key作为字节值,并构造密钥我将使用两个BigIntegers,如何构造公钥?

编辑:问题是被解码为base64的mod,exp字节数组,是公钥的M,N ......

    byte[] mod  = 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);

2 个答案:

答案 0 :(得分:1)

您没有告诉我们您想要的对象类型。但是如果你想要一个RSA公钥,你可以做(​​BouncyCastle):

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

请参阅class definition

答案 1 :(得分:1)

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