我是 RSA 和密码学的新手,我需要使用 RSA 加密和解密数据,我有一个 Java 程序,它生成一个 key 对并将它们保存在一个 XML 格式的 .key 文件中(绝对没问题,并且通过加密和解密数据),然后我想在 .NET 应用程序中使用它们,我正在导入用于加密和解密的 key 。公钥没问题,加密没有问题,但私钥导入失败,并显示以下异常消息

Bad data (CryptographicException.ThrowCryptogaphicException(Int32 hr))

这是编码的公钥:
<RSAKeyValue>
<Modulus>iFouk9viRs5dcvJCvDM1vXC4sBuSB9SPcdJhRyFLoNW/pka6MNAiu4cOksFRejiuM1ZswyJMy+ow
lmLflJ/XrfnUQxLwLp61oij4CrzHKl9jjHorqIA7uEQKY8RBiUjZ7kbO5nFaIWs1NWMVks8Srdhv
8pVd1sLKKUs66c/ndAk=</Modulus>
<Exponent>AQAB</Exponent>
</RSAKeyValue>

这是编码的公钥:
<RSAKeyValue>
<Modulus>iFouk9viRs5dcvJCvDM1vXC4sBuSB9SPcdJhRyFLoNW/pka6MNAiu4cOksFRejiuM1ZswyJMy+ow
lmLflJ/XrfnUQxLwLp61oij4CrzHKl9jjHorqIA7uEQKY8RBiUjZ7kbO5nFaIWs1NWMVks8Srdhv
8pVd1sLKKUs66c/ndAk=</Modulus>
<Exponent>AQAB</Exponent>
<P>AO9WnDNOt9Xewnoy8KTed56Z+3Nfto6J8wCXKzX3LhuuiKNUBe8qFoinrteQJq/9NAEXnNCafxDW
ThIkr9GtMxE=</P>
<Q>AJHYMk0bOEGZlQbaJk3VDovvOJuRt5NI3WtXWl1v5VUW6aQQO3rV3+3GSN6Xa3hTKXtCVVL26Awy
OkDykUPjQXk=</Q>
<DP>KIHsJfLowlXVbIE6oWzVqg49tKU6bJ2Ed1Eeix+uuhisH5iU+ImTDsXynaFUKu0b5CNu8w9y+hKL
XB7BcydxQQ==</DP>
<DQ>di267NIersF1idzhZvY62FdbBmx4VaeYi+93sPkH2wA7CI+CsxF1Z6XhzETkd9bjaRaiLx0VgTR+
Eby8y0bt+Q==</DQ>
<InverseQ>HYF8gahVyzsz0IotzKI2Oh53sJMZWVxsvzkhqGlDtY1THFGZE5j8kl/UK0+FSN6yOYxBIuKNZ7om
 MgLQEMK1PQ==</InverseQ>
<D>DERQvGyjxsr6DUVOS7AvvYNOmklgseOlpA/RQJz2ONoCC+uBBLM07LoRzZImymAfC+9SiZukXRQM
mvr6MlzPAm04NWyZNzbjhLvmn1gmvDclDZ9X9bhYp8MBftPWU5PFBALOjVpD+mlbI2lTYCugf6pJ
MHEMe17mNJ0eWCerfAE=</D>
</RSAKeyValue>

请帮助我了解发生了什么以及私钥有什么问题。

这是解决问题后运行正常的代码:
 private String getPublicKeyXml(RSAPublicKey pk) throws UnsupportedEncodingException {

    StringBuilder builder = new StringBuilder();
    builder.append("<RSAKeyValue>\n");

    byte[] m = pk.getModulus().toByteArray();
    byte[] mm = stripLeadingZeros(m);

    write(builder, "Modulus", mm);
    write(builder, "Exponent", pk.getPublicExponent());

    builder.append("</RSAKeyValue>");

    return builder.toString();
}

private String getPrivateKeyXml(PrivateKey pk) throws UnsupportedEncodingException {
    RSAPrivateCrtKey privKey = (RSAPrivateCrtKey) pk;
    BigInteger n = privKey.getModulus();
    BigInteger e = privKey.getPublicExponent();
    BigInteger d = privKey.getPrivateExponent();
    BigInteger p = privKey.getPrimeP();
    BigInteger q = privKey.getPrimeQ();
    BigInteger dp = privKey.getPrimeExponentP();
    BigInteger dq = privKey.getPrimeExponentQ();
    BigInteger inverseQ = privKey.getCrtCoefficient();

    StringBuilder builder = new StringBuilder();
    builder.append("<RSAKeyValue>\n");
    write(builder, "Modulus", stripLeadingZeros(n.toByteArray()));
    write(builder, "Exponent", stripLeadingZeros(e.toByteArray()));
    write(builder, "P", stripLeadingZeros(p.toByteArray()));
    write(builder, "Q", stripLeadingZeros(q.toByteArray()));
    write(builder, "DP", stripLeadingZeros(dp.toByteArray()));
    write(builder, "DQ", stripLeadingZeros(dq.toByteArray()));
    write(builder, "InverseQ", stripLeadingZeros(inverseQ.toByteArray()));
    write(builder, "D", stripLeadingZeros(d.toByteArray()));
    builder.append("</RSAKeyValue>");

    return builder.toString();
}

private void write(StringBuilder builder, String tag, byte[] bigInt) throws UnsupportedEncodingException {
    builder.append("\t<");
    builder.append(tag);
    builder.append(">");
    builder.append(encode(bigInt).trim());
    builder.append("</");
    builder.append(tag);
    builder.append(">\n");
}

private void write(StringBuilder builder, String tag, BigInteger bigInt) throws UnsupportedEncodingException {
    builder.append("\t<");
    builder.append(tag);
    builder.append(">");
    builder.append(encode(bigInt));
    builder.append("</");
    builder.append(tag);
    builder.append(">\n");
}

private static String encode(BigInteger bigInt) throws UnsupportedEncodingException {
    return new String(new sun.misc.BASE64Encoder().encode(bigInt.toByteArray()));
}

private static String encode(byte[] bigInt) throws UnsupportedEncodingException {
    return new String(new sun.misc.BASE64Encoder().encode(bigInt));
}

private byte[] stripLeadingZeros(byte[] a) {
    int lastZero = -1;
    for (int i = 0; i < a.length; i++) {
        if (a[i] == 0) {
            lastZero = i;
        } else {
            break;
        }
    }
    lastZero++;
    byte[] result = new byte[a.length - lastZero];
    System.arraycopy(a, lastZero, result, 0, result.length);
    return result;
}

最佳答案


查看“常见错误”部分下的 Cryptographic Interoperability: Keys。那些仍然在我脑海中根深蒂固的:

  • 错误的 KeyNumber 或 KeyUsage
  • 错误的加密服务提供程序
  • 正确的加密服务提供者,错误的 ProviderType
  • 加密服务提供者不支持 key 大小
  • 在字节数组中留下 0,使得数组有一个太多的八位字节
  • 关于c# - 导入私钥时出现 RSA key 对异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14018517/

    10-13 03:21