자바(Java)
[스프링] 자바 RSA 로그인 적용
xemaker
2017. 11. 15. 17:29
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");