자바(Java)
[Java] 암호화를 위한 Cipher 클래스 (Java로 AES, RSA 암호화, 복호화)
xemaker
2019. 11. 26. 14:43
출처:
https://m.blog.naver.com/PostView.nhn?blogId=javaking75&logNo=220552245734&proxyReferer=https%3A%2F%2Fwww.google.com%2F
Java에서 암호화, 복호화를 하려면 javax.crypto.Cipher 클래스를 사용한다. AES와 DES, RSA 등 다양한 암호 방식을 지원 한다.
AES ; 고급 암호화 표준 ( 출처 : wikipedia.org )
https://ko.wikipedia.org/wiki/고급_암호화_표준
RSA 암호 설명 ( 출처 : wikipedia.org )
https://ko.wikipedia.org/wiki/RSA_암호
Java API Doc
Cipher - http://docs.oracle.com/javase/7/docs/api/javax/crypto/Cipher.html
[코드] AESEncryptionSample.java - AES에 의한 공통키 암호화 방식
Colored By Color Scripter™
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
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
public class AESEncryptionSample {
public static void main(String[] args) throws NoSuchAlgorithmException
, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException
, BadPaddingException, UnsupportedEncodingException {
// 암호화에 사용할 키, 디폴트로 128bit(16Byte)
String encryptionKey = "happyprogrammer!";
// 암호화할 문자열
String target = "Java 마스터!";
// AES로 암호화 =================================================
// AES Cipher 객체 생성
Cipher cipher = Cipher.getInstance("AES");
// 암호화 Chipher 초기화
SecretKeySpec secretKeySpec = new SecretKeySpec(encryptionKey.getBytes(),"AES");
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
// 암호화 완료
byte[] encryptBytes = cipher.doFinal(target.getBytes("UTF-8"));
System.out.println(new String(encryptBytes)); // => 똑같은 암호화키로 복호화
// AES로 복호화 =================================================
// 복호화 Chipher 초기화, 똑같은 암호화키로 복호화
cipher.init(cipher.DECRYPT_MODE, secretKeySpec);
byte[] decryptBytes = cipher.doFinal(encryptBytes);
System.out.println(new String(decryptBytes, "UTF-8"));
}
}

[코드] RSAEncryptionSample.java - RSA에 의한 비밀키 암호화 방식
Colored By Color Scripter™
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
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
public class RSAEncryptionSample {
public static void main(String[] args) throws NoSuchAlgorithmException
, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException
, BadPaddingException, UnsupportedEncodingException {
// 암호화할 문자열
String target = "Java 마스터!";
// RSA 로 암호화 =================================================
// RSA 비밀키와 공개키를 생성
KeyPairGenerator keypairgen = KeyPairGenerator.getInstance("RSA");
KeyPair keyPair = keypairgen.generateKeyPair();
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
// Cipher 객체 생성과 비밀키 초기화
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
// 암호화 완료
byte[] encryptBytes = cipher.doFinal(target.getBytes());
System.out.println(new String(encryptBytes)); // => 암호화되어 읽지못함
// RSA로 복호화 =================================================
// 복호화 Chipher 초기화, 비밀키와 쌍인 공개키로 복호화함.
cipher.init(cipher.DECRYPT_MODE, publicKey);
byte[] decryptBytes = cipher.doFinal(encryptBytes);
System.out.println(new String(decryptBytes));
}
}

https://m.blog.naver.com/PostView.nhn?blogId=javaking75&logNo=220552245734&proxyReferer=https%3A%2F%2Fwww.google.com%2F
Java에서 암호화, 복호화를 하려면 javax.crypto.Cipher 클래스를 사용한다. AES와 DES, RSA 등 다양한 암호 방식을 지원 한다.
AES ; 고급 암호화 표준 ( 출처 : wikipedia.org )
https://ko.wikipedia.org/wiki/고급_암호화_표준
RSA 암호 설명 ( 출처 : wikipedia.org )
https://ko.wikipedia.org/wiki/RSA_암호
Java API Doc
Cipher - http://docs.oracle.com/javase/7/docs/api/javax/crypto/Cipher.html
[코드] AESEncryptionSample.java - AES에 의한 공통키 암호화 방식
Colored By Color Scripter™
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
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
public class AESEncryptionSample {
public static void main(String[] args) throws NoSuchAlgorithmException
, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException
, BadPaddingException, UnsupportedEncodingException {
// 암호화에 사용할 키, 디폴트로 128bit(16Byte)
String encryptionKey = "happyprogrammer!";
// 암호화할 문자열
String target = "Java 마스터!";
// AES로 암호화 =================================================
// AES Cipher 객체 생성
Cipher cipher = Cipher.getInstance("AES");
// 암호화 Chipher 초기화
SecretKeySpec secretKeySpec = new SecretKeySpec(encryptionKey.getBytes(),"AES");
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
// 암호화 완료
byte[] encryptBytes = cipher.doFinal(target.getBytes("UTF-8"));
System.out.println(new String(encryptBytes)); // => 똑같은 암호화키로 복호화
// AES로 복호화 =================================================
// 복호화 Chipher 초기화, 똑같은 암호화키로 복호화
cipher.init(cipher.DECRYPT_MODE, secretKeySpec);
byte[] decryptBytes = cipher.doFinal(encryptBytes);
System.out.println(new String(decryptBytes, "UTF-8"));
}
}

[코드] RSAEncryptionSample.java - RSA에 의한 비밀키 암호화 방식
Colored By Color Scripter™
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
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
public class RSAEncryptionSample {
public static void main(String[] args) throws NoSuchAlgorithmException
, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException
, BadPaddingException, UnsupportedEncodingException {
// 암호화할 문자열
String target = "Java 마스터!";
// RSA 로 암호화 =================================================
// RSA 비밀키와 공개키를 생성
KeyPairGenerator keypairgen = KeyPairGenerator.getInstance("RSA");
KeyPair keyPair = keypairgen.generateKeyPair();
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
// Cipher 객체 생성과 비밀키 초기화
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
// 암호화 완료
byte[] encryptBytes = cipher.doFinal(target.getBytes());
System.out.println(new String(encryptBytes)); // => 암호화되어 읽지못함
// RSA로 복호화 =================================================
// 복호화 Chipher 초기화, 비밀키와 쌍인 공개키로 복호화함.
cipher.init(cipher.DECRYPT_MODE, publicKey);
byte[] decryptBytes = cipher.doFinal(encryptBytes);
System.out.println(new String(decryptBytes));
}
}