package wssec;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.IvParameterSpec;

import java.security.Key;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

public class PlainBCTest {

	String plainData = "<ns1:testMethod xmlns:ns1=\"uri:LogTestService2\" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xenc=\"http://www.w3.org/2001/04/xmlenc#\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"></ns1:testMethod>";

	Key key;

	KeyGenerator keyGen;

	Cipher encrypt;

	Cipher decrypt;

	public static void main(String[] args) {
		java.security.Security.insertProviderAt(new BouncyCastleProvider(), 2);
//        java.security.Security.addProvider(new BouncyCastleProvider());
		
		PlainBCTest bcTest = new PlainBCTest();
		bcTest.performTest();
	}

	void performTest() {
		java.security.Security.addProvider(new BouncyCastleProvider());

		try {
			// "BC" is the name of the BouncyCastle provider
			keyGen = KeyGenerator.getInstance("DESede", "BC");
			// keyGen.init();

			key = keyGen.generateKey();

			encrypt = Cipher.getInstance("DESede/CBC/ISO10126Padding", "BC");
			decrypt = Cipher.getInstance("DESede/CBC/ISO10126Padding");
			System.out.println("Other provider: " + decrypt.getProvider().getName());
			
			encrypt.init(Cipher.ENCRYPT_MODE, key);
			byte[] encryptedBytes = encrypt.doFinal(plainData.getBytes("UTF-8"));

			IvParameterSpec ivp = new IvParameterSpec(encrypt.getIV());		
			
			decrypt.init(Cipher.DECRYPT_MODE, key, ivp);

			byte[] plainBytes;
			plainBytes = decrypt.doFinal(encryptedBytes, 0,
					encryptedBytes.length);
			System.out.println("'" + new String(plainBytes) + "'");
		} catch (Exception e) {
			System.err.println(e);
			System.exit(1);
		}
	}
}
