here is 3DES encryption/decryption example:
private final static ENC_TYPE = “TripleDES”;
// you can generate from InputStream too
SecretKey key = KeyGenerator.getInstance( ENC_TYPE ).generateKey();
// encrypt
Cipher eCipher = Cipher.getInstance( ENC_TYPE );
eCipher.init( Cipher.ENCRYPT_MODE, key );
String plainText = “hello world”;
byte[] utf8 = plainText.getBytes( “UTF8″ );
byte[] encrypted = eCipher.doFinal( utf8 );
String base64Encoded = new BASE64Encoder().encode( encrypted );
out.println( “Encrypted text – ” + base64Encode );
// decrypt
Cipher dCipher = Cipher.getInstance( ENC_TYPE );
dCipher.init( Cipher.DECRYPT_MODE, key );
byte[] encData = new BASE64Decoder().decodeBuffer( base64Encoded );
byte[] decryptedUtf8 = dCipher.doFinal( encData );
String realText = new String( decryptedUtf8 );
out.println( “Decrypted text – ” + realText );
Recent Comments