【问题标题】:Encrypt xml file in.net and Decrypt encrypted file in java using x509 certificate使用x509证书加密.net中的xml文件和解密java中的加密文件
【发布时间】:2012-04-24 12:28:59
【问题描述】:

我是 xml 加密的新手,我想加密 .net 中的 xml 文件并使用 x509 证书解密 java 中的加密文件。这个可以吗?

【问题讨论】:

  • link 这可能对你有用。
  • @PLB:我通过了链接,但我希望在 java 端解密加密文件..
  • link 也检查一下。我不擅长java,对不起。

标签: java .net encryption x509certificate2


【解决方案1】:

是的,这是完全可能的。您可以使用某种编程语言使用任何众所周知的算法加密文件,并使用另一种编程语言对其进行解密。 对于在 c# 中使用 x509 进行加密,请查看: http://msdn.microsoft.com/en-us/library/ms229744.aspx

【讨论】:

【解决方案2】:

请看下面的例子

public class CryptoUtil {

public static byte[] rsaEncrypt(byte[] publicKey, byte[] data) throws IOException, InvalidKeySpecException,
            NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
    X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicKey);

    KeyFactory kf = KeyFactory.getInstance("RSA");
    PublicKey pk = kf.generatePublic(publicKeySpec);

    Cipher rsa = Cipher.getInstance("RSA");

    rsa.init(Cipher.ENCRYPT_MODE, pk);
    ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();

    CipherOutputStream os = new CipherOutputStream(byteOutputStream, rsa);
    os.write(data);
    os.flush();
    os.close();
    return byteOutputStream.toByteArray();

}

public static byte[] rsaDecrypt(byte[] privateKey, byte[] data) throws IOException, InvalidKeySpecException,
            NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
    PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(privateKey);

    KeyFactory kf = KeyFactory.getInstance("RSA");
    PrivateKey pk = kf.generatePrivate(privateKeySpec);

    Cipher rsa = Cipher.getInstance("RSA");

    rsa.init(Cipher.DECRYPT_MODE, pk);
    ByteArrayInputStream byteInputStream = new ByteArrayInputStream(data);

    InputStream is = new CipherInputStream(byteInputStream, rsa);
    byte[] message = IOUtils.toByteArray(is);
    is.close();
    return message;

}

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-30
    • 2020-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-03
    相关资源
    最近更新 更多