-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCipherDemoIVparam.java
More file actions
63 lines (56 loc) · 1.97 KB
/
CipherDemoIVparam.java
File metadata and controls
63 lines (56 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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());
}
}