TripleDES encryption/decryption example:

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 );

Advertisements

One response to “TripleDES encryption/decryption example:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

my tweets

December 2006
S S M T W T F
« Nov   Jan »
 1
2345678
9101112131415
16171819202122
23242526272829
3031  

Flickr Photos

RSS my shared links on google reader

  • An error has occurred; the feed is probably down. Try again later.
%d bloggers like this: