天天看點

我的java web登入RSA加密

之前一直沒關注過web應用登入密碼加密的問題,這兩天用appscan掃描應用,最嚴重的問題就是這個了,提示我明文發送密碼。這個的确很不安全,以前也大概想過,但是沒有具體研究過,都不了了之,這次借這個機會,終于搞定了這個問題。

首先,有不少文章說在用戶端用js對密碼進行md5摘要,然後送出給登入處理的url。這種做法無非是自欺欺人,就算别人抓包抓不到你原始密碼,用這個md5後的密碼一樣可以模拟登入系統,無非稍微安全了一點點,也就是直接通過登入頁沒法直接輸入使用者名密碼來登入,但是人家的手段你知道有啥呢?用程式模拟登陸也不是什麼太難的事情。

https當然也是個選擇,但是對于一般應用來說,還需要生成密鑰之類的,還需要拿去給那些認證機構簽名,麻煩不說,銀子是必須的。如果說讓使用者安裝證書,應用系統還可以,網站就不太現實了,畢竟不是所有使用者都有那麼高的計算機操作水準,就算有,人家一用感覺這麼麻煩,也不見得去操作。

這次專心搜尋了1個小時,還是覺得非對稱加密比較靠譜,有一些RSA加密的文章值得借鑒。這裡向這些文章作者緻敬,我參考可不隻一篇文章,因為問題多多的。廢話到此結束,說說我的處理方式吧。

加密解密的流程:

 a)在login.jsp中,加入一段java代碼,生成公鑰和私鑰,私鑰對象儲存在session中;公鑰中,我把Exponent, modulus放到request的attribute中,并在頁面上輸出給js加密函數,用于密碼加密。使用security.js的功能加密

var key = new RSAUtils.getKeyPair("${publicKeyExponent}", "", "${publicKeyModulus}");

       var reversedPwd = password.split("").reverse().join("");//js裡面是反序的字元串,不知道為啥
       var encrypedPwd = RSAUtils.encryptedString(key,reversedPwd);
           

b)在點選送出按鈕時,調用登陸js函數。這個函數是用ajax方式将使用者名,密碼送出給登陸處理url的。在送出之前,先利用a步驟中的公鑰Exponent,和modulus,對密碼進行加密,然後再發送給伺服器端。

c)在登陸處理url中,(我是login.action),從session中取得私鑰對象,對密碼進行解密。随後的步驟都一樣了,到庫裡去查詢之類的,不細說了。

下面說說我處理的步驟和遇到的主要問題。

1.隻能使用RSA這種非對稱加密,才能讓密碼破解成為僅僅“理論上”的可能。是以決定使用這種加密方式。

2.尋找合适的用戶端javascript加密代碼。這個我是不太懂了,隻能去找。最後找到了security.js。網上有些文章用的3個檔案,BigInt.js,RSA.js還有個啥來着,Barrett.js這3個來實作,開始我用了。但是和服務端配合不了(我自己的問題),結果後來找到這個security.js,實際上是把這個3個js都封裝到1個裡面了,而且最後修改時間是2010年,比較新,就用這個吧。那3個js檔案應該也是能用的。

3.在伺服器端生成公鑰和私鑰,這個本來想對簡單,代碼可參考的很多。但是我遇到的問題不少,解密的時候總是出錯。

   問題一:在login.jsp中,公鑰的Exponent,和modulus輸出格式問題

       開始總是什麼:長度過大,必須以0開始之類的異常。我想到很可能是js加密和純java加密那些地方不同導緻的。後來發現,原來是我公鑰的Exponent,和modulus輸出直接用的toString()方法,實際上應該用toString(16),用16進制輸出,因為在security.js中,那個

RSAUtils.getKeyPair(publicKeyExponent, "", ${publicKeyModulus);方法内部,明顯是從16進制進行轉換的。改完後應該是這樣:

String publicKeyExponent = publicKey.getPublicExponent().toString(16);//16進制
	    String publicKeyModulus = publicKey.getModulus().toString(16);//16進制
	    request.setAttribute("publicKeyExponent", publicKeyExponent);
	    request.setAttribute("publicKeyModulus", publicKeyModulus);
           

 問題二:有個什麼Padding之類的異常,是RSA算法中前面補齊的問題,原因時js和java預設的RSA算法不一緻。

     經過分析,用RSA其他的provider可以解決此問題,于是在生成密碼對的代碼中,使用了

KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA", new org.bouncycastle.jce.provider.BouncyCastleProvider());  
           

這個的補齊方式和js的就一緻了。

問題三:provider的認證問題

  剛用上,感覺能通過了,但是馬上就是一個異常:jce cannot authenticate the provider bc。意思好了解,就是沒經過認證。怎麼讓他通過呢,結果我在運作應用伺服器的javahome\jre\lib\security\java.security檔案中添加了如下代碼:

security.provider.11=org.bouncycastle.jce.provider.BouncyCastleProvider
           

11是序号,順着它原來的加就可以了。

感覺上ok了,啟動一下看看,還是那個問題。這個問題是我在jboss-eap-6.2上出現的。其他的應用伺服器可能比較簡單些(比如直接放到伺服器的lib下)。

于是查jboss的資料,終于找到了,說是在jboss中,不能讓這個provider的jar包在應用的lib下,需要使用全局的jar包。如果使用maven,那必須讓這個包的scope是provided;(反正别讓BouncyCastle這個jar包打入到war裡面就可以)。需要在eap6.2下進行配置:

在jboss_home/modules下建立目錄org\bouncycastle\main,在main目錄中,放入bcprov-jdk16-1.46.jar,并加入module的配置檔案module.xml,内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.1" name="org.bouncycastle">
    <resources>
        <resource-root path="bcprov-jdk16-1.46.jar"/>
    </resources>
    <dependencies>
        <module name="javax.api" slot="main" export="true"/>
    </dependencies>
</module>
           

還需要在web應用的META-INF\MANIFEST.MF中加入Dependencies: org.bouncycastle

我是maven工程,需要配置pom:

<plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>${version.war.plugin}</version>
            <configuration>
               <!-- Java EE 6 doesn't require web.xml, Maven needs to catch up! -->
               <failOnMissingWebXml>true</failOnMissingWebXml>
               <version>3.0</version>
               <archive>
		            <manifestEntries>
					<Dependencies>org.bouncycastle</Dependencies>
					</manifestEntries>
				</archive>
            </configuration>
            
         </plugin>
           

主要是archive節點的配置,這樣打包後MANIFEST.MF的内容就會變了

不能傳附件可咋整呢?貼代碼吧:

login.jsp

<script src="js/lib/security.js" type="text/javascript"></script>
<script type="text/javascript">
	<%
		HashMap<String, Object> map = RSAUtils.getKeys();  
	    //生成公鑰和私鑰  
	    RSAPublicKey publicKey = (RSAPublicKey) map.get("public");  
	    RSAPrivateKey privateKey = (RSAPrivateKey) map.get("private");
	    
	    session.setAttribute("privateKey", privateKey);//私鑰儲存在session中,用于解密
	    
	    //公鑰資訊儲存在頁面,用于加密
	    String publicKeyExponent = publicKey.getPublicExponent().toString(16);
	    String publicKeyModulus = publicKey.getModulus().toString(16);
	    request.setAttribute("publicKeyExponent", publicKeyExponent);
	    request.setAttribute("publicKeyModulus", publicKeyModulus);
	%>
    function login() {
       //登入
       var username = $("#txtUsername").val();
       var password = $("#txtPassword").val();
       var randCode = $("#txtRandCode").val();
       var rememberMeObj = document.getElementById("cbRememberMe");
       var rem = rememberMeObj.checked ? "1" : "";

       
       RSAUtils.setMaxDigits(200);
       //setMaxDigits(256);
       var key = new RSAUtils.getKeyPair("${publicKeyExponent}", "", "${publicKeyModulus}");

       var encrypedPwd = RSAUtils.encryptedString(key,password);
       $.post("login.action", { username: username, password: encrypedPwd, randcode: randCode, rememberme: rem }, function (jsonData) {
           //請求完成
           //如果為true,證明該使用者已經下載下傳過,顯示已下載下傳提示,否則直接下載下傳
           if (jsonData.success == true) {
               //登入成功
               window.location.href = 'index.jsp';
           } else {
               //提示錯誤資訊divErrors
               var ettText = "";
               if (username == "") {
            	   ettText="使用者名不能為空";
               }
               else if (password == "") {
            	   ettText="密碼不能為空";
               }
               else if (randCode == "") {
            	   ettText="驗證碼不能為空";
               }
               else {
            	   ettText=jsonData.msg;
               }
               alert(ettText);
           }
       });
    }</script>
           

login.action

RSAPrivateKey privateKey = (RSAPrivateKey)request.getSession().getAttribute("privateKey");
String descrypedPwd = RSAUtils.decryptByPrivateKey(password, privateKey); //解密後的密碼,password是送出過來的密碼
           

RSAUtils.java

package com.myapp.util;
import java.math.BigInteger;  
import java.security.KeyFactory;  
import java.security.KeyPair;  
import java.security.KeyPairGenerator;  
import java.security.NoSuchAlgorithmException;  
import java.security.interfaces.RSAPrivateKey;  
import java.security.interfaces.RSAPublicKey;  
import java.security.spec.RSAPrivateKeySpec;  
import java.security.spec.RSAPublicKeySpec;  
import java.util.HashMap; 
import javax.crypto.Cipher; 

public class RSAUtils {
	/** 
     * 生成公鑰和私鑰 
     * @throws NoSuchAlgorithmException  
     * 
     */  
    public static HashMap<String, Object> getKeys() throws NoSuchAlgorithmException{  
        HashMap<String, Object> map = new HashMap<String, Object>();  
        KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA", new org.bouncycastle.jce.provider.BouncyCastleProvider());  
        keyPairGen.initialize(1024);  
        KeyPair keyPair = keyPairGen.generateKeyPair();  
        RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();  
        RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();  
        map.put("public", publicKey);  
        map.put("private", privateKey);  
        return map;  
    }  
    /** 
     * 使用模和指數生成RSA公鑰 
     *  
     *  
     * @param modulus 
     *            模 
     * @param exponent 
     *            指數 
     * @return 
     */  
    public static RSAPublicKey getPublicKey(String modulus, String exponent) {  
        try {  
            BigInteger b1 = new BigInteger(modulus);  
            BigInteger b2 = new BigInteger(exponent);  
            KeyFactory keyFactory = KeyFactory.getInstance("RSA", new org.bouncycastle.jce.provider.BouncyCastleProvider());  
            RSAPublicKeySpec keySpec = new RSAPublicKeySpec(b1, b2);  
            return (RSAPublicKey) keyFactory.generatePublic(keySpec);  
        } catch (Exception e) {  
            e.printStackTrace();  
            return null;  
        }  
    }  
  
    /** 
     * 使用模和指數生成RSA私鑰 
     
     * /None/NoPadding】 
     *  
     * @param modulus 
     *            模 
     * @param exponent 
     *            指數 
     * @return 
     */  
    public static RSAPrivateKey getPrivateKey(String modulus, String exponent) {  
        try {  
            BigInteger b1 = new BigInteger(modulus);  
            BigInteger b2 = new BigInteger(exponent);  
            KeyFactory keyFactory = KeyFactory.getInstance("RSA", new org.bouncycastle.jce.provider.BouncyCastleProvider());  
            RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(b1, b2);  
            return (RSAPrivateKey) keyFactory.generatePrivate(keySpec);  
        } catch (Exception e) {  
            e.printStackTrace();  
            return null;  
        }  
    }  
  
    /** 
     * 公鑰加密 
     *  
     * @param data 
     * @param publicKey 
     * @return 
     * @throws Exception 
     */  
    public static String encryptByPublicKey(String data, RSAPublicKey publicKey)  
            throws Exception {  
        Cipher cipher = Cipher.getInstance("RSA", new org.bouncycastle.jce.provider.BouncyCastleProvider());  
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);  
        // 模長  
        int key_len = publicKey.getModulus().bitLength() / 8;  
        // 加密資料長度 <= 模長-11  
        String[] datas = splitString(data, key_len - 11);  
        String mi = "";  
        //如果明文長度大于模長-11則要分組加密  
        for (String s : datas) {  
            mi += bcd2Str(cipher.doFinal(s.getBytes()));  
        }  
        return mi;  
    }  
  
    /** 
     * 私鑰解密 
     *  
     * @param data 
     * @param privateKey 
     * @return 
     * @throws Exception 
     */  
    public static String decryptByPrivateKey(String data, RSAPrivateKey privateKey)  
            throws Exception {  
        Cipher cipher = Cipher.getInstance("RSA", new org.bouncycastle.jce.provider.BouncyCastleProvider());  
        cipher.init(Cipher.DECRYPT_MODE, privateKey);  
        //模長  
        int key_len = privateKey.getModulus().bitLength() / 8;  
        byte[] bytes = data.getBytes();  
        byte[] bcd = ASCII_To_BCD(bytes, bytes.length);  
        //System.err.println(bcd.length);  
        //如果密文長度大于模長則要分組解密  
        String ming = "";  
        byte[][] arrays = splitArray(bcd, key_len);  
        for(byte[] arr : arrays){  
            ming += new String(cipher.doFinal(arr));  
        }  
        return ming;  
    }  
    /** 
     * ASCII碼轉BCD碼 
     *  
     */  
    public static byte[] ASCII_To_BCD(byte[] ascii, int asc_len) {  
        byte[] bcd = new byte[asc_len / 2];  
        int j = 0;  
        for (int i = 0; i < (asc_len + 1) / 2; i++) {  
            bcd[i] = asc_to_bcd(ascii[j++]);  
            bcd[i] = (byte) (((j >= asc_len) ? 0x00 : asc_to_bcd(ascii[j++])) + (bcd[i] << 4));  
        }  
        return bcd;  
    }  
    public static byte asc_to_bcd(byte asc) {  
        byte bcd;  
  
        if ((asc >= '0') && (asc <= '9'))  
            bcd = (byte) (asc - '0');  
        else if ((asc >= 'A') && (asc <= 'F'))  
            bcd = (byte) (asc - 'A' + 10);  
        else if ((asc >= 'a') && (asc <= 'f'))  
            bcd = (byte) (asc - 'a' + 10);  
        else  
            bcd = (byte) (asc - 48);  
        return bcd;  
    }  
    /** 
     * BCD轉字元串 
     */  
    public static String bcd2Str(byte[] bytes) {  
        char temp[] = new char[bytes.length * 2], val;  
  
        for (int i = 0; i < bytes.length; i++) {  
            val = (char) (((bytes[i] & 0xf0) >> 4) & 0x0f);  
            temp[i * 2] = (char) (val > 9 ? val + 'A' - 10 : val + '0');  
  
            val = (char) (bytes[i] & 0x0f);  
            temp[i * 2 + 1] = (char) (val > 9 ? val + 'A' - 10 : val + '0');  
        }  
        return new String(temp);  
    }  
    /** 
     * 拆分字元串 
     */  
    public static String[] splitString(String string, int len) {  
        int x = string.length() / len;  
        int y = string.length() % len;  
        int z = 0;  
        if (y != 0) {  
            z = 1;  
        }  
        String[] strings = new String[x + z];  
        String str = "";  
        for (int i=0; i<x+z; i++) {  
            if (i==x+z-1 && y!=0) {  
                str = string.substring(i*len, i*len+y);  
            }else{  
                str = string.substring(i*len, i*len+len);  
            }  
            strings[i] = str;  
        }  
        return strings;  
    }  
    /** 
     *拆分數組  
     */  
    public static byte[][] splitArray(byte[] data,int len){  
        int x = data.length / len;  
        int y = data.length % len;  
        int z = 0;  
        if(y!=0){  
            z = 1;  
        }  
        byte[][] arrays = new byte[x+z][];  
        byte[] arr;  
        for(int i=0; i<x+z; i++){  
            arr = new byte[len];  
            if(i==x+z-1 && y!=0){  
                System.arraycopy(data, i*len, arr, 0, y);  
            }else{  
                System.arraycopy(data, i*len, arr, 0, len);  
            }  
            arrays[i] = arr;  
        }  
        return arrays;  
    }
    public static void main(String[] args) throws Exception{
    	HashMap<String, Object> map = getKeys();  
        //生成公鑰和私鑰  
        RSAPublicKey publicKey = (RSAPublicKey) map.get("public");  
        RSAPrivateKey privateKey = (RSAPrivateKey) map.get("private");  
          
        //模  
        String modulus = publicKey.getModulus().toString();  
        System.out.println("pubkey modulus="+modulus);
        //公鑰指數  
        String public_exponent = publicKey.getPublicExponent().toString();
        System.out.println("pubkey exponent="+public_exponent);
        //私鑰指數  
        String private_exponent = privateKey.getPrivateExponent().toString();  
        System.out.println("private exponent="+private_exponent);
        //明文  
        String ming = "111";  
        //使用模和指數生成公鑰和私鑰  
        RSAPublicKey pubKey = RSAUtils.getPublicKey(modulus, public_exponent);  
        RSAPrivateKey priKey = RSAUtils.getPrivateKey(modulus, private_exponent);  
        //加密後的密文  
        String mi = RSAUtils.encryptByPublicKey(ming, pubKey);  
        System.err.println("mi="+mi);  
        //解密後的明文  
        String ming2 = RSAUtils.decryptByPrivateKey(mi, priKey);  
        System.err.println("ming2="+ming2);  
    }
}
           

最後,那個js的RSA實作我上傳了:

https://download.csdn.net/download/yys79/11587195