0
votes

I am new to xml encryption i want to encrypt xml file in .net and decrypt the encrypted file in java using x509 certificate. Can this be done?

2
link this may be usefull for you. - Leri
@PLB: i gone through link but i want encrypted file to be decrypted at java end.. - kevin159
link check this too. I am not good at java, sorry. - Leri

2 Answers

1
votes

Yes it is perfectly possible. You can encrypt a file using any well known algorithm using some programming language and decrypt it with another programming language. For encryption using x509 in c#, have a look at: http://msdn.microsoft.com/en-us/library/ms229744.aspx

0
votes

Please have a look at the example below

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;

}

}