티스토리 뷰
1. 로그인 jsp 가기전에 RSA 에 필요한 값을 세팅한다.
로그인 화면 나오기 전에 session하고 model에 아래와 같이 값을 저장한 후
로그인 jsp로 이동한다.
String publicKeyModule="";
String publicKeyExponent="";
final int KEY_SIZE=1024;
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
generator.initialize(KEY_SIZE);
KeyPair keyPair = generator.genKeyPair();
PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
RSAPublicKeySpec publicSpec = (RSAPublicKeySpec) keyFactory.getKeySpec(publicKey, RSAPublicKeySpec.class);
publicKeyModule = publicSpec.getModulus().toString(16);
publicKeyExponent = publicSpec.getPublicExponent().toString(16);
model.addObject("publicKeyExponent", publicKeyExponent);
model.addObject("publicKeyModule", publicKeyModule);
session.setAttribute("privateKey", privateKey);
2. 로그인 jsp 화면에서 받은값을 이용해 암호화
jsbn.js, rsa.js, prng4.js, rng.js 를 import 한 후
var publicKeyModule = "${publicKeyModule}";
var publicKeyExponent = "${publicKeyExponent}";
var rsa = new RSAKey();
rsa.setPublic(publicKeyModule, publicKeyExponent);
var _securedPw = rsa.encrypt(loginPw);
$("#password").val(escape(encodeURIComponent(_securedPw)));
$('form').submit();
3. 복호화
스프링에서 복호화 한다.
//세션에 저장되어 있는 privateKey를 가져온다.
PrivateKey privateKey = (PrivateKey)RequestContextHolder.getRequestAttributes().getAttribute("privateKey", RequestAttributes.SCOPE_SESSION);
Cipher cipher = Cipher.getInstance("RSA");
byte[] encryptedBytes = this.hexToByteArray(pass.toString());
cipher.init(Cipher.DECRYP_MODE, privateKey);
byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
pass = new String(decryptedBytes, "utf-8");
로그인 화면 나오기 전에 session하고 model에 아래와 같이 값을 저장한 후
로그인 jsp로 이동한다.
String publicKeyModule="";
String publicKeyExponent="";
final int KEY_SIZE=1024;
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
generator.initialize(KEY_SIZE);
KeyPair keyPair = generator.genKeyPair();
PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
RSAPublicKeySpec publicSpec = (RSAPublicKeySpec) keyFactory.getKeySpec(publicKey, RSAPublicKeySpec.class);
publicKeyModule = publicSpec.getModulus().toString(16);
publicKeyExponent = publicSpec.getPublicExponent().toString(16);
model.addObject("publicKeyExponent", publicKeyExponent);
model.addObject("publicKeyModule", publicKeyModule);
session.setAttribute("privateKey", privateKey);
2. 로그인 jsp 화면에서 받은값을 이용해 암호화
jsbn.js, rsa.js, prng4.js, rng.js 를 import 한 후
var publicKeyModule = "${publicKeyModule}";
var publicKeyExponent = "${publicKeyExponent}";
var rsa = new RSAKey();
rsa.setPublic(publicKeyModule, publicKeyExponent);
var _securedPw = rsa.encrypt(loginPw);
$("#password").val(escape(encodeURIComponent(_securedPw)));
$('form').submit();
3. 복호화
스프링에서 복호화 한다.
//세션에 저장되어 있는 privateKey를 가져온다.
PrivateKey privateKey = (PrivateKey)RequestContextHolder.getRequestAttributes().getAttribute("privateKey", RequestAttributes.SCOPE_SESSION);
Cipher cipher = Cipher.getInstance("RSA");
byte[] encryptedBytes = this.hexToByteArray(pass.toString());
cipher.init(Cipher.DECRYP_MODE, privateKey);
byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
pass = new String(decryptedBytes, "utf-8");
'자바(Java)' 카테고리의 다른 글
자바 정수 랜덤 (java integer random) (0) | 2017.12.08 |
---|---|
자바 오늘 날짜로 디렉토리(폴더) 만들기 (0) | 2017.12.05 |
스프링 세션 사용 (0) | 2017.11.10 |
[Java] 자바 버전 확인 (0) | 2017.10.19 |
[자바배치실행] Error: A JNI error has occurred, please check your installation and try again Exception in thread "main" java.lang.NoClassDefFoundError (0) | 2017.09.05 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- ocajp
- 오라클
- php
- 스크래핑
- esql
- 라이믹스 모듈
- 자바 smtp
- webix
- 인포믹스
- EC
- 파싱
- XE3
- 프로씨
- XE
- 이클립스
- Python
- 플러터
- C언어
- 문자열
- proc
- 자바
- xe addon
- 파이썬
- JDBC
- KG
- xe애드온
- C
- 포인터
- ocjap
- MySQL
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함