티스토리 뷰
출처:
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));
}
}

'자바(Java)' 카테고리의 다른 글
자바 aes 암호화 복호화 예제 (0) | 2019.12.09 |
---|---|
톰캣 로그 설정 [WAS] Tomcat 로그 설정(무분별하게 커지는 catalina.out 용량 설정하기) (0) | 2019.12.04 |
파일명 밀리세컨드 (0) | 2019.11.01 |
자바 다음달 날짜 구하기 (0) | 2019.10.24 |
이클립스 svn 체크아웃 후 버튼 먹통 동작 안할때 (0) | 2019.10.16 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- KG
- ocajp
- php
- 라이믹스 모듈
- xe addon
- 문자열
- EC
- JDBC
- 파싱
- ocjap
- 플러터
- C언어
- 오라클
- 스크래핑
- esql
- xe애드온
- MySQL
- C
- 자바
- 자바 smtp
- Python
- 파이썬
- XE
- 포인터
- 프로씨
- XE3
- proc
- 인포믹스
- webix
- 이클립스
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함