我是xml加密的新手,我想加密.net中的xml文件,并使用x509证书解密java中的加密文件。可以这样做吗?
答案 0 :(得分:1)
是的,这是完全可能的。您可以使用某种编程语言使用任何众所周知的算法加密文件,并使用其他编程语言对其进行解密。 要在c#中使用x509进行加密,请查看: http://msdn.microsoft.com/en-us/library/ms229744.aspx
答案 1 :(得分:0)
请看下面的例子
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;
}
}