移動APP如何儲存使用者密碼

來源:互聯網
上載者:User

標籤:加密   安全   移動   

<span style="font-size:14px;">為了更好的使用者體驗,移動APP用戶端一般都會將使用者資訊進行儲存以便後續可以自動登入.</span>

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

解決的方法大概有一下幾種:

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

A.用戶端將密碼Hash加密,登入成功後將hash值儲存到Sqlite.服務端得到使用者名稱和hash值,採用同樣的演算法對密碼進行Hash運算,然後和使用者傳來的hash值進行比較,一致則登入成功.更加可靠的是對密碼加鹽加密.例如可以採用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值,然後採用同樣的演算法對資料庫裡面的密碼進行計算

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一致,則登入成功.同時用戶端將加密後的字串儲存到本機資料庫,下次登入時直接從資料庫讀取.

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

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

另參考文章:http://blog.csdn.net/hengyunabc/article/details/34623957





移動APP如何儲存使用者密碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.