티스토리 뷰

웹 취약점 패스워드 평문전송 - javascript - RSA 로 암호화 로그인 - (1) 번글에서 RSA 로그인의 원리를 깨달았다면 여기서는 실제로 직접 적용해본 경험담을 적어보겠습니다.


우선 RSA 에 대한 기본 개념이 있어야 하니 잘 모르신 분은 이전 글을 먼저 읽어 보는게 좋을것 같습니다.


먼저 세션에 대해 공개키, 개인키를 생성하여 세션(서버)에 저장합니다.


====== LogInRsa.java =============================


로그인 폼(index.jsp) 로 가기 전에 먼저 호출됨.


HttpServletRequest request = getRequest();

HttpSession session = request.getSession(true);


final int KEY_SIZE=1024;


KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");

generator.initialize(KEY_SIZE);


KeyPair keyPair = generator.genKeyPair();

PublicKe publicKey = keyPair.getPublic();

PrivateKey privateKey = keyPair.getPrivate();


KeyFactory keyFactory = KeyFactory.getInstance("RSA");

RsaInfo rsaInfo = new RsaInfo();

rsaInfo.setPrivateKey(privateKey);


RSAPublicKeySpec publicSpec=(RSAPublicKeySpec) keyFactory.getKeySpec(publicKey, RSAPublicKeySpec.class);

publicKeyModule = publicSpec.getModules().toString(16);

publicKeyExponent = publicSpec.getPublicExponent().toString(16);


rsaInfo.setPublicKeyExponent(publicKeyExponent);

rsaInfo.setPublicKeyModule(publicKeyModule);


session.setAttribute("RSA_INFO", rsaInfo);


=========== RsaInfo ================================


public class RsaInfo implements Serializable{

    private String publicKeyModule;

    private String publicKeyExponent;

    private PrivateKey privateKey;


    public String getPublicKeyModule(){

        return publicKeyModule;

    }


    public void setPublicKeyModule(String publicKeyModule){

        this.publicKeyModule = publicKeyModule;

    }


    public PrivateKey getPrivateKey(){

        return privateKey;

    }


    public void setPrivateKey(PrivateKey privateKey){

        this.privateKey = privateKey;

    }


    public String getPublicKeyExponent(){

        return publicKeyExponent;

    }


    public void setPublicKeyExponent(String publicKeyExponent){

        this.publicKeyExponent = publicKeyExponent;

    }

}



=============== index.jsp ====================


RsaInfo rsa = (RsaInfo)session.getAttribute("RSA_INFO");


String publicKeyModule = rsa.getPublicKeyModule();

String publicKeyExponent = rsa.getPublicExponent();


<script src="/js/rsa/jsbn.js"></script>

<script src="/js/rsa/rsa.js"></script>

<script src="/js/rsa/prng4.js"></script>

<script src="/js/rsa/rng.js"></script>


var _securedId="";

var _securedPw="";


var publicKeyModule = "<%=publicKeyModule%>";

ar publicKeyExponent = "<%=publicKeyExponent%>";


var loginId = $('#USER_ID').val();

var loginPw = $('#PASSWORD').val();


makeEncryptInfo(loginId, loginPw, publicKeyModule, publicKeyExponent);


$('#USER_ID').val(escape(encodeURIComponent(_securedId)));

$('#PASSWORD').val(escape(encodeURIComponent(_securedPw)));


document.frm1.submit();


function makeEncryptInfo(loginId, loginPw, publicKeyModule, publicKeyExponent){

    var rsa = new RSAKey();

    rsa.setPublic(publicKeyModule,publicKeyExponent);


    _securedId = rsa.encrtypt(loginId);

    _securedPw = rsa.encrtypt(loginPw);

}


================ 복호화 LogInAct.java ================


HttpServletRequest request = getRequest;


HttpSession session = request.getSession();


String userId = request.getParameter("USER_ID");

String pass = request.getParameter("PASSWORD");

RsaInfo rsa = (RsaInfo)session.getAttribute("RSA_INFO");

PrivateKey privateKey = (PrivateKey)rsa.getPrivateKey();


Cipher cipher = Cipher.getInstance("RSA");

byte[] encrtyptedBytes = this.hexToByteArray(pass);

cipher.init(Cipher.DECRYPT_MODE, privateKey);

decryptedBytes = cipher.doFinal(encrtyptedBytes);

pass = new String(decryptedBytes, "utf-8");


==============================================


군더더기는 쏵~ 빼고 실제 쓰는것만 나타냈다. 코드가 그리 어렵지 않으니 보면 알것이다.



댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
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
글 보관함