How To Encrypt/Decrypt Data with Java Cipher and IvParameterSpec Class

Posted on November 8, 2012. Filed under: java/j2EE |


ciperDemo#mkdir cipherdemo
ciperDemo#cd cipherdemo/
ciperDemo#vi CipherDemo.java

ciperDemo#cat CipherDemo.java

import java.security.*;

import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
public class CipherDemo {
public static void main(String [] args) {
try {
String[] algorithm = {“AES”,”Blowfish”,”DES”};//”3DES”,”RSA”
String[] modes = {“ECB”,”CBC”,”CFB”,”OFB”,”OFB32″,”CFB8″};
String[] padding = {“PKCS5Padding”};//”NoPadding”
KeyGenerator kg = null;
Cipher cipher = null;
for(int i=0; i<algorithm.length; i++) {
SecretKey sk = KeyGenerator.getInstance(algorithm[i]).generateKey();
for(int j=0; j<modes.length; j++) {
for(int k=0; k<padding.length; k++) {
cipher = Cipher.getInstance(algorithm[i]+”/”+modes[j]+”/”+padding[k]);
cipher.init(Cipher.ENCRYPT_MODE, sk);
printeParam(cipher);
System.out.println();
byte[] encryptedText = cipher.doFinal(“data”.getBytes());
if(cipher.getIV()!=null) {
cipher.init(Cipher.DECRYPT_MODE, sk, new IvParameterSpec(cipher.getIV()));
}else{
cipher.init(Cipher.DECRYPT_MODE, sk);
}

System.out.println(“Decrypted data:”+new String(cipher.doFinal(encryptedText)));
cipher = Cipher.getInstance(algorithm[i]+”/”+modes[j]+”/”+padding[k]);
System.out.println();
}

}
}
KeyGenerator kg1 = KeyGenerator.getInstance(“AES”);//3DES, Blowfish
Cipher cipher1 = Cipher.getInstance(“AES/ECB/PKCS5Padding”);//CBC,CFB,OFB

} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
}
}

private static void printeParam(Cipher ciper) {
System.out.println(“ciper.getProvider(): “+ciper.getProvider());
System.out.println(“ciper.getAlgorithm(): “+ciper.getAlgorithm());
System.out.println(“ciper.getBlockSize(): “+ciper.getBlockSize());
System.out.println(“ciper.getParameters(): “+ciper.getParameters());
System.out.println(“ciper.getIV(): “+ciper.getIV());
}

}

ciperDemo#

ciperDemo#javac CipherDemo.java

ciperDemo#java CipherDemo
ciper.getProvider(): SunJCE version 1.6
ciper.getAlgorithm(): AES/ECB/PKCS5Padding
ciper.getBlockSize(): 16
ciper.getParameters(): null
ciper.getIV(): null

Decrypted data:data

ciper.getProvider(): SunJCE version 1.6
ciper.getAlgorithm(): AES/CBC/PKCS5Padding
ciper.getBlockSize(): 16
ciper.getParameters():
iv:
[0000: 84 7E CC 54 CC C3 3B 67 E0 CC 9A 6C 6C 2C 6F C6 …T..;g…ll,o.
]

ciper.getIV(): [B@4a4e79f1

Decrypted data:data

ciper.getProvider(): SunJCE version 1.6
ciper.getAlgorithm(): AES/CFB/PKCS5Padding
ciper.getBlockSize(): 16
ciper.getParameters():
iv:
[0000: 6E EE E3 B6 C9 10 30 76 38 63 82 0A B9 99 DB 8F n…..0v8c……
]

ciper.getIV(): [B@6627e353

Decrypted data:data

ciper.getProvider(): SunJCE version 1.6
ciper.getAlgorithm(): AES/OFB/PKCS5Padding
ciper.getBlockSize(): 16
ciper.getParameters():
iv:
[0000: 8F 16 CD 6E C5 22 7C 01 21 9A 0E 82 FE 4E 49 49 …n.”..!….NII
]

ciper.getIV(): [B@44bd928a

Decrypted data:data

ciper.getProvider(): SunJCE version 1.6
ciper.getAlgorithm(): AES/OFB32/PKCS5Padding
ciper.getBlockSize(): 16
ciper.getParameters():
iv:
[0000: 67 58 16 9C B1 09 0B B6 92 A2 50 40 C4 A9 C0 DB gX……..P@….
]

ciper.getIV(): [B@79dfc547

Decrypted data:data

ciper.getProvider(): SunJCE version 1.6
ciper.getAlgorithm(): AES/CFB8/PKCS5Padding
ciper.getBlockSize(): 16
ciper.getParameters():
iv:
[0000: E5 CC 06 53 48 76 28 AD 25 19 D7 CE 5E 2F 59 5B …SHv(.%…^/Y[
]

ciper.getIV(): [B@5210f6d3

Decrypted data:data

ciper.getProvider(): SunJCE version 1.6
ciper.getAlgorithm(): Blowfish/ECB/PKCS5Padding
ciper.getBlockSize(): 8
ciper.getParameters(): null
ciper.getIV(): null

Decrypted data:data

ciper.getProvider(): SunJCE version 1.6
ciper.getAlgorithm(): Blowfish/CBC/PKCS5Padding
ciper.getBlockSize(): 8
ciper.getParameters():
iv:
[0000: B6 08 06 AB 08 C2 62 74 ……bt
]

ciper.getIV(): [B@7e5a9de6

Decrypted data:data

ciper.getProvider(): SunJCE version 1.6
ciper.getAlgorithm(): Blowfish/CFB/PKCS5Padding
ciper.getBlockSize(): 8
ciper.getParameters():
iv:
[0000: 55 06 66 CE 55 25 93 A8 U.f.U%..
]

ciper.getIV(): [B@6f34a7d8

Decrypted data:data

ciper.getProvider(): SunJCE version 1.6
ciper.getAlgorithm(): Blowfish/OFB/PKCS5Padding
ciper.getBlockSize(): 8
ciper.getParameters():
iv:
[0000: DF E8 C3 9C C1 EC FE 7B ……..
]

ciper.getIV(): [B@69f78ef1

Decrypted data:data

ciper.getProvider(): SunJCE version 1.6
ciper.getAlgorithm(): Blowfish/OFB32/PKCS5Padding
ciper.getBlockSize(): 8
ciper.getParameters():
iv:
[0000: 87 28 11 C5 09 03 ED D6 .(……
]

ciper.getIV(): [B@7bc9a690

Decrypted data:data

ciper.getProvider(): SunJCE version 1.6
ciper.getAlgorithm(): Blowfish/CFB8/PKCS5Padding
ciper.getBlockSize(): 8
ciper.getParameters():
iv:
[0000: C6 E5 C1 02 48 5A A6 EF ….HZ..
]

ciper.getIV(): [B@73d6776d

Decrypted data:data

ciper.getProvider(): SunJCE version 1.6
ciper.getAlgorithm(): DES/ECB/PKCS5Padding
ciper.getBlockSize(): 8
ciper.getParameters(): null
ciper.getIV(): null

Decrypted data:data

ciper.getProvider(): SunJCE version 1.6
ciper.getAlgorithm(): DES/CBC/PKCS5Padding
ciper.getBlockSize(): 8
ciper.getParameters():
iv:
[0000: D9 F8 DA DC 12 10 D0 A6 ……..
]

ciper.getIV(): [B@3a21b220

Decrypted data:data

ciper.getProvider(): SunJCE version 1.6
ciper.getAlgorithm(): DES/CFB/PKCS5Padding
ciper.getBlockSize(): 8
ciper.getParameters():
iv:
[0000: 6E 7A 93 E5 99 5A DE B2 nz…Z..
]

ciper.getIV(): [B@7a3570b0

Decrypted data:data

ciper.getProvider(): SunJCE version 1.6
ciper.getAlgorithm(): DES/OFB/PKCS5Padding
ciper.getBlockSize(): 8
ciper.getParameters():
iv:
[0000: 64 7D 31 98 1D 31 FB 4A d.1..1.J
]

ciper.getIV(): [B@4a79717e

Decrypted data:data

ciper.getProvider(): SunJCE version 1.6
ciper.getAlgorithm(): DES/OFB32/PKCS5Padding
ciper.getBlockSize(): 8
ciper.getParameters():
iv:
[0000: 1D 4E 8D D4 4D 9A 8E 99 .N..M…
]

ciper.getIV(): [B@6b0a2d64

Decrypted data:data

ciper.getProvider(): SunJCE version 1.6
ciper.getAlgorithm(): DES/CFB8/PKCS5Padding
ciper.getBlockSize(): 8
ciper.getParameters():
iv:
[0000: 3D 8F E9 02 5B EF 79 37 =…[.y7
]

ciper.getIV(): [B@692a3722

Decrypted data:data

ciperDemo#

Advertisements

Make a Comment

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

Liked it here?
Why not try sites on the blogroll...

%d bloggers like this: