-
Notifications
You must be signed in to change notification settings - Fork 0
/
CipherSample.java
56 lines (39 loc) · 1.49 KB
/
CipherSample.java
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
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
public class CipherSample {
private static final String KEY_SPEC = "AES";
private static final String CIPHER_MODE = "AES/ECB/PKCS5Padding";
public static void main(String[] args) {
String key = "abcdefghijklmnop"; // <-128bit = 16文字のキー
byte[] keyBytes = key.getBytes(); // バイト列に変換
CipherSample cs = new CipherSample();
byte[] enBytes = cs.encryptECB("hello".getBytes(), keyBytes);
// Base64して表示
System.out.println(Base64.getEncoder().encodeToString(enBytes));
byte[] deBytes = cs.decryptECB(enBytes, keyBytes);
System.out.println(new String(deBytes));
}
public byte[] encryptECB(byte[] data, byte[] secret_key) {
SecretKeySpec sKey = new SecretKeySpec(secret_key, KEY_SPEC);
try {
Cipher cipher = Cipher.getInstance(CIPHER_MODE);
cipher.init(Cipher.ENCRYPT_MODE, sKey);
return cipher.doFinal(data);
} catch (Exception e) {
e.printStackTrace();
}
return new byte[0];
}
public byte[] decryptECB(byte[] encData, byte[] secret_key) {
SecretKeySpec sKey = new SecretKeySpec(secret_key, KEY_SPEC);
try {
Cipher cipher = Cipher.getInstance(CIPHER_MODE);
cipher.init(Cipher.DECRYPT_MODE, sKey);
return cipher.doFinal(encData);
} catch (Exception e) {
e.printStackTrace();
}
return new byte[0];
}
}