forked from hacker85/JavaLessons
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCipherLesson.java
More file actions
42 lines (36 loc) · 1.45 KB
/
Copy pathCipherLesson.java
File metadata and controls
42 lines (36 loc) · 1.45 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
package security;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.*;
/**
* Created by max on 2/17/17.
*/
public class CipherLesson {
public static void main(String[] args) throws NoSuchPaddingException, NoSuchAlgorithmException, BadPaddingException, IllegalBlockSizeException, InvalidKeyException, UnsupportedEncodingException {
Cipher cipher = Cipher.getInstance("AES");
int mode = Cipher.ENCRYPT_MODE;
// KeyGenerator kgen = KeyGenerator.getInstance("AES");
// kgen.init(128);
// SecretKey key = kgen.generateKey();
SecretKeySpec key = new SecretKeySpec("Bar12345Bar12345".getBytes(), "AES");
cipher.init(mode, key);
String hello = "hello world";
byte[] bytes = cipher.doFinal(hello.getBytes(), 0, hello.length());
for(byte b : bytes) {
System.out.print(b);
}
System.out.println("\n----------");
Cipher decryptCipher = Cipher.getInstance("AES");
decryptCipher.init(Cipher.DECRYPT_MODE, key);
byte[] bytes2 = decryptCipher.doFinal(bytes, 0, bytes.length);
for(byte b : bytes2) {
System.out.print((char)b);
}
// CipherInputStream inputStream;
// CipherOutputStream outputStream;
}
}