์๋ฐฉํฅ ์ํธํ(RSA) ๋จ๋ฐฉํฅ ์ํธํ(SHA 256) ์ฐจ์ด
RSA | SHA 256 |
์๋ฐฉํฅ ์ํธํ = ๋ณตํธํ๊ฐ ๊ฐ๋ฅํ๋ค. | ๋จ๋ฐฉํฅ ์ํธํ = ๋ณตํธํ๊ฐ ๋ถ๊ฐ๋ฅํ๋ค. |
์ฌ์ฉ์๋ ๊ณต๊ฐ๋ ํค์ ์์ ๋ง ์๋ ๊ฐ์ธํค๋ฅผ ๊ฐ์ง๋ค. | hash ํจ์์ ์ผ์ข |
๊ณต๊ฐํค, ๊ฐ์ธํค๋ก ์ํธํ/๋ณตํธํ๋ฅผ ํ๋ ์์คํ | SHA ์๊ณ ๋ฆฌ์ฆ์ ํ ์ข
๋ฅ๋ก 256๋นํธ๋ก ๊ตฌ์ฑ 64์๋ฆฌ ๋ฌธ์์ด์ ๋ฐํ |
ํค์์ฑ - ํค๋ถ๋ฐฐ - ์ํธํ - ๋ณตํธํ ์์ผ๋ก ์ด๋ฃจ์ด์ง. | ๋ณตํธํ๋ฅผ ํ์ง ์์๋ ๋ ์๋๊ฐ ๋น ๋ฅด๋ค. |
web(JSP) -> server(controller)๋ก ํ๋ผ๋ฏธํฐ๋ฅผ ๋ณด๋ผ ๋ ์ค๊ฐ์์ ์ ๋ณด๋ฅผ ๊ฐ๋ก์ฑ ์ ์๋ค.
์ด๋ฌํ ๋ณด์ ๋ฌธ์ ๋ฅผ ๋ง๊ธฐ ์ํด JSP์์ RSA๋ฅผ ์ฌ์ฉํ์ฌ ๋น๋ฐ๋ฒํธ๋ฅผ ์ํธํํด์ค๋ค..
RSA๋ก ์ํธํ๋ ํ๋ผ๋ฏธํฐ๋ฅผ controller์์ ๋ณตํธํ์ํค๊ณ ,
๋ณตํธํ์ํจ ํ๋ผ๋ฏธํฐ ๊ฐ์ ๋จ๋ฐฉํฅ ์ํธํ์ธ SHA256์ผ๋ก ์ํธํ์ํจ๋ค.
RSA ์ํธํ
Rsa.java
package egovframework.let.utl.sim.service;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.RSAPublicKeySpec;
import javax.crypto.Cipher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
public class Rsa {
public static String RSA_WEB_KEY = "_RSA_WEB_Key_"; // ๊ฐ์ธํค session key
public static String RSA_INSTANCE = "RSA"; // rsa transformation
/**
* ๋ณตํธํ
*
* @param privateKey
* @param securedValue
* @return
* @throws Exception
*/
public String decryptRsa(PrivateKey privateKey, String securedValue) throws Exception {
Cipher cipher = Cipher.getInstance(Rsa.RSA_INSTANCE);
byte[] encryptedBytes = hexToByteArray(securedValue);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
String decryptedValue = new String(decryptedBytes, "utf-8"); // ๋ฌธ์ ์ธ์ฝ๋ฉ ์ฃผ์.
return decryptedValue;
}
/**
* 16์ง ๋ฌธ์์ด์ byte ๋ฐฐ์ด๋ก ๋ณํํ๋ค.
*
* @param hex
* @return
*/
public static byte[] hexToByteArray(String hex) {
if (hex == null || hex.length() % 2 != 0) { return new byte[] {}; }
byte[] bytes = new byte[hex.length() / 2];
for (int i = 0; i < hex.length(); i += 2) {
byte value = (byte) Integer.parseInt(hex.substring(i, i + 2), 16);
bytes[(int) Math.floor(i / 2)] = value;
}
return bytes;
}
/**
* rsa ๊ณต๊ฐํค, ๊ฐ์ธํค ์์ฑ
*
* @param request
*/
public void initRsa(HttpServletRequest request) {
HttpSession session = request.getSession();
KeyPairGenerator generator;
try {
generator = KeyPairGenerator.getInstance(Rsa.RSA_INSTANCE);
generator.initialize(1024);
KeyPair keyPair = generator.genKeyPair();
KeyFactory keyFactory = KeyFactory.getInstance(Rsa.RSA_INSTANCE);
PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();
session.setAttribute(Rsa.RSA_WEB_KEY, privateKey); // session์ RSA ๊ฐ์ธํค๋ฅผ ์ธ์
์ ์ ์ฅ
RSAPublicKeySpec publicSpec = (RSAPublicKeySpec) keyFactory.getKeySpec(publicKey, RSAPublicKeySpec.class);
String publicKeyModulus = publicSpec.getModulus().toString(16);
String publicKeyExponent = publicSpec.getPublicExponent().toString(16);
request.setAttribute("RSAModulus", publicKeyModulus); // rsa modulus ๋ฅผ request ์ ์ถ๊ฐ
request.setAttribute("RSAExponent", publicKeyExponent); // rsa exponent ๋ฅผ request ์ ์ถ๊ฐ
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
login.jsp
RSA ์๋ฐฉํฅ ์ํธํ๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด์ RSA ์๋ฐ์คํฌ๋ฆฝํธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ถ๊ฐ๊ฐ ํ์ํ๋ค.
์ฌ๊ธฐ์ js๋ฅผ ๋ค์ด๋ก๋ ๋ฐ์ ํ๋ก์ ํธ์ ๋ฃ๋๊ฑธ ์ถ์ฒ.
<script type="text/javascript" src="http://www-cs-students.stanford.edu/~tjw/jsbn/rsa.js"></script>
<script type="text/javascript" src="http://www-cs-students.stanford.edu/~tjw/jsbn/jsbn.js"></script>
<script type="text/javascript" src="http://www-cs-students.stanford.edu/~tjw/jsbn/prng4.js"></script>
<script type="text/javascript" src="http://www-cs-students.stanford.edu/~tjw/jsbn/rng.js"></script>
// ๋น๋ฐ๋ฒํธ ์ํธํ
function frm_check(){
var pw = document.loginForm.password.value;
// rsa ์ํธํ
var rsa = new RSAKey();
rsa.setPublic($('#RSAModulus').val(),$('#RSAExponent').val());
$("#password").val(rsa.encrypt(pw));
}
์๋ฒ ์ธก์์ RSA ๊ณต๊ฐํค์ ๊ฐ์ธํค(์ํธํค)๋ฅผ ์์ฑํ์ฌ ๊ฐ์ธํค๋ ์ธ์ ์ ์ ์ฅํ๊ณ ๊ณต๊ฐํค๋ HTML ๋ก๊ทธ์ธ ํผ ํ์ด์ง์ Input [type=hidden] value ๊ฐ์ ์ธํ .
<form:form id="loginForm" name="loginForm" action="${pageContext.request.contextPath}/actionLogin.do" method="post" onsubmit="return frm_check();" >
<div class="user_login_ultop">
<input type="hidden" id="RSAModulus" value="${RSAModulus}"/>
<input type="hidden" id="RSAExponent" value="${RSAExponent}"/>
<ul>
<li>
<label for="id"></label>
<input type="text" class="input_style" title="์์ด๋๋ฅผ ์
๋ ฅํ์ธ์." id="id" name="id" maxlength="10"/>
</li>
<li>
<label for="password"></label>
<input type="password" class="input_style" maxlength="25" title="๋น๋ฐ๋ฒํธ๋ฅผ ์
๋ ฅํ์ธ์." id="password" name="password"/>
</li>
<li>
<input type="checkbox" name="checkId" id="checkId" /><label for="checkId">ID์ ์ฅ</label>
</li>
</ul>
<input type="submit" alt="๋ก๊ทธ์ธ ํ๊ธฐ" value="๋ก๊ทธ์ธ" class="btn_style"/>
</div>
</form:form>
RSA ๋ณตํธํ
LoginController.java
Rsa r = new Rsa();
HttpSession session = request.getSession();
//๋ก๊ทธ์ธ์ ์ ์ธ์
์ ์ ์ฅ๋ ๊ฐ์ธํค๋ฅผ ๊ฐ์ ธ์จ๋ค.
PrivateKey privateKey = (PrivateKey) session.getAttribute(Rsa.RSA_WEB_KEY);
//์ํธํ ๋ ๋น๋ฐ๋ฒํธ๋ฅผ ๋ณตํธํ ์ํจ๋ค.
String password = r.decryptRsa(privateKey, LoginVO.getPassword());
// ShA 256 ์ํธํ = ๋จ๋ฐฉํฅ
String sha = EgovFileScrty.encryptPassword(password);
LoginVO.setPassword(sha);
SHA 256 ์ํธํ
EgovFileScrty.java
/**
* ๋น๋ฐ๋ฒํธ๋ฅผ ์ํธํํ๋ ๊ธฐ๋ฅ(๋ณตํธํ๊ฐ ๋๋ฉด ์๋๋ฏ๋ก SHA-256 ์ธ์ฝ๋ฉ ๋ฐฉ์ ์ ์ฉ).
*
* deprecated : ๋ณด์ ๊ฐํ๋ฅผ ์ํ์ฌ salt๋ก ID๋ฅผ ์ง์ ํ๋ encryptPassword(password, id) ์ฌ์ฉ
*
* @param String data ์ํธํํ ๋น๋ฐ๋ฒํธ
* @return String result ์ํธํ๋ ๋น๋ฐ๋ฒํธ
* @exception Exception
*/
@Deprecated
public static String encryptPassword(String data) throws Exception {
if (data == null) {
return "";
}
byte[] plainText = null; // ํ๋ฌธ
byte[] hashValue = null; // ํด์ฌ๊ฐ
plainText = data.getBytes();
MessageDigest md = MessageDigest.getInstance("SHA-256");
hashValue = md.digest(plainText);
return new String(Base64.encodeBase64(hashValue));
}
์ฐธ๊ณ :
'Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[EgovFramework] Spring Interceptor session AJAX ์ฒ๋ฆฌ (0) | 2021.10.18 |
---|---|
[EgovFramework] context-syslogaop.xml aop Error (0) | 2021.10.12 |
[Spring Error] Can not find the tag library descriptor for (0) | 2021.08.31 |
[JAVA] Lamda ํํ์ ์์ (0) | 2021.08.27 |
[MAVEN] java.util.zip.ZipException: invalid LOC header (bad signature) ์ค๋ฅ (0) | 2021.08.12 |