Подтвердить что ты не робот

Перенос ключей .Net RSA xml в Java

У меня есть частные и открытые ключи из .Net-системы в формате xml. Я должен использовать эти ключи для выполнения шифрования/дешифрования на Java. Есть ли способ сделать это?

Открытый ключ выглядит примерно так:

<RSAKeyValue>
    <Modulus>jHIxcGzzpByFv...pvhxFnP0ssmlBfMALis</Modulus>
    <Exponent>AQAB</Exponent>
</RSAKeyValue>

Закрытый ключ:

<RSAKeyValue>
    <Modulus>4hjg1ibWXHIlH...ssmlBfMAListzrgk=</Modulus>
    <Exponent>AQAB</Exponent>
    <P>8QZCtrmJcr9uW7VRex+diH...jLHV5StmuBs1+vZZAQ==</P>
    <Q>8CUvJTv...yeDszMWNCQ==</Q>
    <DP>elh2Nv...cygE3657AQ==</DP>
    <DQ>MBUh5XC...+PfiMfX0EQ==</DQ>
    <InverseQ>oxvsj4WCbQ....LyjggXg==</InverseQ>
    <D>KrhmqzAVasx...uxQ5VGZmZ6yOAE=</D>
</RSAKeyValue>

Я написал немного кода для шифрования данных, но я не уверен, правильно ли он.

        Element modulusElem = root.getChild("Modulus");
        Element exponentElem = root.getChild("Exponent");

        byte[] expBytes = decoder.decodeBuffer(exponentElem.getText().trim());
        byte[] modBytes = decoder.decodeBuffer(modulusElem.getText().trim());

        RSAPublicKeySpec keySpec = new RSAPublicKeySpec(new BigInteger(1, modBytes), new BigInteger(1, expBytes));
        KeyFactory fact = KeyFactory.getInstance("RSA");
        PublicKey pubKey = fact.generatePublic(keySpec);

Как я могу сделать закрытый ключ из xml для дешифрования данных?

4b9b3361

Ответ 1

Является ли это decoder в вашем примере выполнением декодирования Base64? Похоже, вы могли бы полагаться на sun.misc.BASE64Decoder, и вообще не рекомендуется зависеть от этих внутренних классов (например, у другой JVM этого не будет). Вы можете использовать Apache Commons Codec, который имеет класс Base64 для декодирования. Здесь остальная часть того, что вам нужно, хотя для шифрования и расшифровки RSA:

byte[] expBytes = Base64.decodeBase64(exponentElem.getText().trim()));
byte[] modBytes = Base64.decodeBase64(modulusElem.getText().trim());
byte[] dBytes = Base64.decodeBase64(dElem.getText().trim());

BigInteger modules = new BigInteger(1, modBytes);
BigInteger exponent = new BigInteger(1, expBytes);
BigInteger d = new BigInteger(1, dBytes);

KeyFactory factory = KeyFactory.getInstance("RSA");
Cipher cipher = Cipher.getInstance("RSA");
String input = "test";

RSAPublicKeySpec pubSpec = new RSAPublicKeySpec(modules, exponent);
PublicKey pubKey = factory.generatePublic(pubSpec);
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
byte[] encrypted = cipher.doFinal(input.getBytes("UTF-8"));
System.out.println("encrypted: " + new String(encrypted));

RSAPrivateKeySpec privSpec = new RSAPrivateKeySpec(modules, d);
PrivateKey privKey = factory.generatePrivate(privSpec);
cipher.init(Cipher.DECRYPT_MODE, privKey);
byte[] decrypted = cipher.doFinal(encrypted);
System.out.println("decrypted: " + new String(decrypted));

Ответ 2

Попробуйте использовать

RSAPrivateKeySpec privSpec = new RSAPrivateCrtKeySpec(iModulus,iExponentBytes,iDBytes,iPBytes,iQBytes,iDPBytes,iDQBytes,iInverseQBytes)

и добавить все компоненты закрытого ключа