移動APP怎樣儲存使用者password

來源:互聯網
上載者:User

標籤:stat   jni   rgb   使用者   art   throws   sqlite   file   rate   

<span style="font-size:14px;">為了更好的使用者體驗,移動APPclient一般都會將使用者資訊進行儲存以便興許能夠自己主動登入.</span>

儲存了使用者資訊便涉及到了安全問題.

解決辦法大概有一下幾種:

1.首先,假設client和服務端都是你來設計開發,那麼有兩種比較可靠的方案

A.client將passwordHash加密,登入成功後將hash值儲存到Sqlite.服務端得到username和hash值,採用相同的演算法對password進行Hash運算,然後和使用者傳來的hash值進行比較,一致則登入成功.更加可靠的是對password加鹽加密.比如能夠採用PBKDF2加鹽加密.

<span style="font-size:14px;">public static String createHash(String password)throws NoSuchAlgorithmException, InvalidKeySpecException {return createHash(password.toCharArray());}/** * Returns a salted PBKDF2 hash of the password. *  * @param password *            the password to hash * @return a salted PBKDF2 hash of the password */public static String createHash(char[] password)throws NoSuchAlgorithmException, InvalidKeySpecException {// Generate a random saltSecureRandom random = new SecureRandom();byte[] salt = new byte[SALT_BYTE_SIZE];random.nextBytes(salt);// Hash the passwordbyte[] hash = pbkdf2(password, salt, PBKDF2_ITERATIONS, HASH_BYTE_SIZE);return PBKDF2_ITERATIONS + ":" + toHex(salt) + ":" + toHex(hash);}</span>

加密後的字串為1000:1507039de0a3c2c88ddf896233278e37d05fd8a0fadc570d:99222374678d4afe5d7d9bf9be4786e17f045ac217c6a2ca,

1000為迭代的次數,後面各自是salt和hash值.

服務端得到這個字串後,從中解析出迭代次數,salt,hash1值,然後採用相同的演算法對資料庫裡面的password進行計算

public static boolean validatePassword(String password, String correctHash)throws NoSuchAlgorithmException, InvalidKeySpecException {return validatePassword(password.toCharArray(), correctHash);}/** * Validates a password using a hash. *  * @param password *            the password to check * @param correctHash *            the hash of the valid password * @return true if the password is correct, false if not */public static boolean validatePassword(char[] password, String correctHash)throws NoSuchAlgorithmException, InvalidKeySpecException {// Decode the hash into its parametersString[] params = correctHash.split(":");int iterations = Integer.parseInt(params[ITERATION_INDEX]);byte[] salt = fromHex(params[SALT_INDEX]);byte[] hash = fromHex(params[PBKDF2_INDEX]);// Compute the hash of the provided password, using the same salt,// iteration count, and hash lengthbyte[] testHash = pbkdf2(password, salt, iterations, hash.length);// Compare the hashes in constant time. The password is correct if// both hashes match.return slowEquals(hash, testHash);}


假設hash2和hash1一致,則登入成功.同一時候client將加密後的字串儲存到本機資料庫,下次登入時直接從資料庫讀取.

B.使用非對稱式加密演算法對password進行加密.

  1. client使用公開金鑰加密password,得到加密串,然後將其發送到服務端.
  2. 服務端使用私密金鑰解密password。進行驗證,
  3. 登入成功後,client將加密串儲存到本地,便於下次自己主動登入;
使用非對稱式加密比較可靠。即使加密串被泄露也無法得到password.
2.假設你僅僅是負責client。對服務端無能為力,那麼你可能僅僅能使用對稱式加密了.(如你正在為學校圖書館寫個client。你還想設定自己主動登入。那麼你本地僅僅能使用對稱式加密了,將加密串儲存到本地。然後下次自己主動登入時。從資料庫取出加密串然後解密...服務端僅僅識別原始的password)這樣的情況。你僅僅能考慮怎樣產生加密金鑰,以及怎樣儲存密鑰,怎樣混淆.考慮了一種方法:加解密函數 DES(passwd,key,encode);str1 =  DES(passwd,key,encode);str2 =  DES(key,str1,encode);本機資料庫中儲存 str1:str2.解密時,str2以str1解密得到key.然後。str1以key解密得到passwd.非對稱式加密僅僅能以這樣的邏輯上的複雜度添加password的強度.
3. 使用JNI加解密。
另參考文章:http://blog.csdn.net/hengyunabc/article/details/34623957

android中使用jni對字串加解密實現分析

加鹽password雜湊:怎樣正確使用





移動APP怎樣儲存使用者password

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.