RSA加解密,.net公钥/私钥兼容java

背景介绍

之前老程序使用.net进行数据的RSA加解密,现在用JAVA重写,但是.net的公钥和私钥是xml格式,跟java的不一样,需要手动转换一下。目前网上的大部分都是java转.net。我这里来个.net转java。

一、.net公钥转java

1、公钥格式

PFJTQUtleVZhbHVlPjxNb2R1bHVzPi9lRk42TUFyVXE4QWhvYmpsamRjTWhjeEhTWjRnMXFZK3Fmeldud0o2aVhwTWZBVzNmaEJXK2krblRXTnZwckcvelZ5OFpQT2VTOStHWmlYQlVJODNpczBVZzB3eVJKT2E0TWU2d3hmR1pJWTE1WFpGUFdjcEtSaVZqeWxaZGJ0ZmNTY3ZQUnFCMTE5N2kvZkVwNjVrY1Fjc1AzT3U5b3B4ZHJmeEJOMEgwRT08L01vZHVsdXM+PEV4cG9uZW50PkFRQUI8L0V4cG9uZW50PjwvUlNBS2V5VmFsdWU+

2、Base64解码

可以看到是base64编码过的,解码后看看,是一段xml

<RSAKeyValue>
    <Modulus>/eFN6MArUq8AhobjljdcMhcxHSZ4g1qY+qfzWnwJ6iXpMfAW3fhBW+i+nTWNvprG/zVy8ZPOeS9+GZiXBUI83is0Ug0wyRJOa4Me6wxfGZIY15XZFPWcpKRiVjylZdbtfcScvPRqB1197i/fEp65kcQcsP3Ou9opxdrfxBN0H0E=Modulus>
    <Exponent>AQABExponent>
RSAKeyValue>

3、转换

思路:

  1. 解码base64 公钥
  2. 读取xml节点中的ModulusExponent
  3. ModulusExponent进行base64解码
  4. 创建公钥需要的BigInterg格式的参数
  5. 生成公钥

这段代码中使用了 hutoolspring的工具包来解析json和xml,各位可以自行替换。

private  PublicKey getPublicKey(String xmlPublicKey) throws NoSuchAlgorithmException, InvalidKeySpecException {
     
        byte[] bytes = Base64.getDecoder().decode(xmlPublicKey);
        JSONObject jsonObject = XML.toJSONObject(new String(bytes, StandardCharsets.UTF_8));
        String Modulus = jsonObject.getJSONObject("RSAKeyValue").getStr("Modulus");
        String Exponent = jsonObject.getJSONObject("RSAKeyValue").getStr("Exponent");
        byte[] modulus = Base64Utils.decode(Modulus.getBytes());
        byte[] exponent = Base64Utils.decode(Exponent.getBytes());
        BigInteger b1 = new BigInteger(1, modulus);
        BigInteger b2 = new BigInteger(1, exponent);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        RSAPublicKeySpec keySpec = new RSAPublicKeySpec(b1, b2);
        return keyFactory.generatePublic(keySpec);
    }

二、.net私钥转java

1、私钥格式

PFJTQUtleVZhbHVlPjxNb2R1bHVzPi9lRk42TUFyVXE4QWhvYmpsamRjTWhjeEhTWjRnMXFZK3Fmeldud0o2aVhwTWZBVzNmaEJXK2krblRXTnZwckcvelZ5OFpQT2VTOStHWmlYQlVJODNpczBVZzB3eVJKT2E0TWU2d3hmR1pJWTE1WFpGUFdjcEtSaVZqeWxaZGJ0ZmNTY3ZQUnFCMTE5N2kvZkVwNjVrY1Fjc1AzT3U5b3B4ZHJmeEJOMEgwRT08L01vZHVsdXM+PEV4cG9uZW50PkFRQUI8L0V4cG9uZW50PjxQPi93YTQyRnEyWGpkMUE5K1paR1dHR29zclB5YmpPZk8rZkZ4UkxlcFl0cW1TLytEVnpHRGhMaG9lMG1hNnpFQ3FCaFQ0aUgyUHRRWmhiZWtCQlU2T2dRPT08L1A+PFE+L3RsMlFvNWd2ck1JcWhVYkV4QitNRFlFTmgxdzNrUWQzK0M1bmZTWjA4TDBqV3N5Z2N3SjdSU0hPY2ZFclM0bEF5UkwzR0RtbVNrK3hYNVdWRUt3d1E9PTwvUT48RFA+N0lqZWp3VHIyczBUQWltMGl2TERCQUdLZGdOcWc0aTRaMzMwYVdMUEwzUnVmRmViMVBrU1FnRkdEeDNyS2FEY1VkSEhoa2ozTGc2aDlTQkVxeGhtZ1E9PTwvRFA+PERRPnNUQ0ZWdFVqVXJPcjdJVnNJNTh0L1ZzeEptYmwyTVBIZWFjSHAxa3ZzdG96cWpmRUVCYkxOL2t6NCtxUFJsRjFEbkFvQm51aEdSQmlRbklLbDk4ZWdRPT08L0RRPjxJbnZlcnNlUT5TYWZ4eC9GTUVrQzZZTmg0SlNwaUpoSlhiQjB6ajhQSnRSTmNxWWIyNVNLcE1aZW5FSmcxSmNDK0xZbWlLQUFVTFFwK0xROURVcUhBenJMSGlPS3g4Zz09PC9JbnZlcnNlUT48RD42LytaUmFnRlhhb1NDZnV2NmI1eWRKbThiUVdLOTBlcmR5M1NlS1ZmTGdxcHVHTWZUcDFzWER5Qm5HL2c0SExPVXdSallnOXMycGY4VjF6UjNmQ1IxSFpLaE9YRkliY2plajB5TFdURk9xT0Q1MlRLQW5LTE1lWm16TGI3c3NoR0JScXNhaEV5TTJlc2ZUbTM5VUN3TzA0eEJJMHlwT09XMTVQU3pQY0s0QUU9PC9EPjwvUlNBS2V5VmFsdWU+

2、Base64解码

可以看到是base64编码过的,解码后看看,是一段xml。参数比较多

<RSAKeyValue>
    <Modulus>/eFN6MArUq8AhobjljdcMhcxHSZ4g1qY+qfzWnwJ6iXpMfAW3fhBW+i+nTWNvprG/zVy8ZPOeS9+GZiXBUI83is0Ug0wyRJOa4Me6wxfGZIY15XZFPWcpKRiVjylZdbtfcScvPRqB1197i/fEp65kcQcsP3Ou9opxdrfxBN0H0E=Modulus>
    <Exponent>AQABExponent>
    <P>/wa42Fq2Xjd1A9+ZZGWGGosrPybjOfO+fFxRLepYtqmS/+DVzGDhLhoe0ma6zECqBhT4iH2PtQZhbekBBU6OgQ==P>
    <Q>/tl2Qo5gvrMIqhUbExB+MDYENh1w3kQd3+C5nfSZ08L0jWsygcwJ7RSHOcfErS4lAyRL3GDmmSk+xX5WVEKwwQ==Q>
    <DP>7IjejwTr2s0TAim0ivLDBAGKdgNqg4i4Z330aWLPL3RufFeb1PkSQgFGDx3rKaDcUdHHhkj3Lg6h9SBEqxhmgQ==DP>
    <DQ>sTCFVtUjUrOr7IVsI58t/VsxJmbl2MPHeacHp1kvstozqjfEEBbLN/kz4+qPRlF1DnAoBnuhGRBiQnIKl98egQ==DQ>
    <InverseQ>Safxx/FMEkC6YNh4JSpiJhJXbB0zj8PJtRNcqYb25SKpMZenEJg1JcC+LYmiKAAULQp+LQ9DUqHAzrLHiOKx8g==InverseQ>
    <D>6/+ZRagFXaoSCfuv6b5ydJm8bQWK90erdy3SeKVfLgqpuGMfTp1sXDyBnG/g4HLOUwRjYg9s2pf8V1zR3fCR1HZKhOXFIbcjej0yLWTFOqOD52TKAnKLMeZmzLb7sshGBRqsahEyM2esfTm39UCwO04xBI0ypOOW15PSzPcK4AE=D>
RSAKeyValue>

3、转换

大体思路同上,看代码就行了。还是用到了 hutoolspring的工具包来解析json和xml,各位可以自行替换。

private PrivateKey getPrivateKey(String xmlPrivateKey) throws Exception {
     
        byte[] bytes = Base64.getDecoder().decode(xmlPrivateKey);
        JSONObject jsonObject = XML.toJSONObject(new String(bytes, StandardCharsets.UTF_8));
        JSONObject rsaKeyValue = jsonObject.getJSONObject("RSAKeyValue");
        BigInteger modulus = new BigInteger(1, Base64Utils.decode(rsaKeyValue.getStr("Modulus").getBytes()));
        BigInteger exponent = new BigInteger(1, Base64Utils.decode(rsaKeyValue.getStr("Exponent").getBytes()));
        BigInteger d = new BigInteger(1, Base64Utils.decode(rsaKeyValue.getStr("D").getBytes()));
        BigInteger p = new BigInteger(1, Base64Utils.decode(rsaKeyValue.getStr("P").getBytes()));
        BigInteger q = new BigInteger(1, Base64Utils.decode(rsaKeyValue.getStr("Q").getBytes()));
        BigInteger dp = new BigInteger(1, Base64Utils.decode(rsaKeyValue.getStr("DP").getBytes()));
        BigInteger dq = new BigInteger(1, Base64Utils.decode(rsaKeyValue.getStr("DQ").getBytes()));
        BigInteger inverseQ = new BigInteger(1, Base64Utils.decode(rsaKeyValue.getStr("InverseQ").getBytes()));
        RSAPrivateCrtKeySpec privateKeySpec = new RSAPrivateCrtKeySpec(modulus, exponent, d, p, q, dp, dq, inverseQ);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        return privateKey;
    }

你可能感兴趣的:(加解密,base64,xml,json,RSA加密解密)