[英]How to save Oauth Access token securely in android
我在身份驗證后說"uyhjjfjfgg567f8fhjkkf"
現在要安全地將其保存在設備中,我從服務器訪問令牌。 我查看了android開發者網站中的Keystore和Keychain。 我不清楚它是如何工作的以及我們應該如何從密鑰庫中檢索令牌。
KeyPairGenerator kpg = KeyPairGenerator.getInstance(
KeyProperties.KEY_ALGORITHM_EC, "AndroidKeyStore");
kpg.initialize(new KeyGenParameterSpec.Builder(
alias,
KeyProperties.PURPOSE_SIGN | KeyProperties.PURPOSE_VERIFY)
.setDigests(KeyProperties.DIGEST_SHA256,
KeyProperties.DIGEST_SHA512)
.build());
KeyPair kp = kpg.generateKeyPair();
/*
* Load the Android KeyStore instance using the the
* "AndroidKeyStore" provider to list out what entries are
* currently stored.
*/
KeyStore ks = KeyStore.getInstance("AndroidKeyStore");
ks.load(null);
Enumeration<String> aliases = ks.aliases();
您無需保存訪問令牌,因為它的生命周期很短。 將它保存在內存中就足夠了。
您確實需要保留刷新令牌,並且您有以下幾種選擇:
SharedPreferences
AccountManager
考慮使用StoredCredential
。 對於流本身,我建議您使用Google AppAuth庫 。
當然,您也可以使用密碼加密密鑰:
private static byte[] encrypt(byte[] key, byte[] text) throws GeneralSecurityException {
final SecretKeySpec skeySpec = new SecretKeySpec(key, KEY_ALGORITHM);
final Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, sInitVectorSpec);
return cipher.doFinal(text);
}
密鑰可以存儲在KeyStore
。
在這里,您可以找到Androidauthority關於Android安全可用性的非常好的文章。
可以在此處找到Android密鑰庫實現的綜合示例。
另一個不錯的選擇是Google的keyczar ,您可以在git存儲庫中查看示例和詳細信息。 在那里,您還可以找到已知安全問題的詳細列表,以便了解它是否適合您的進一步實施。
對於您當前的問題,我建議按照上面第二個鏈接中的示例實現繼續使用Android Keystore。
祝好運 !
我們使用自定義SharedPreference實例,在添加時加密鍵和值,並在請求時解密。
SecurePreferences preferences = ...
preferences.edit().putString( "key", "value" ).apply(); // key and value are encrypted automatically
String value = preferences.getString( "key", null ); // key and value are decrypted automatically
如果值是加密的,我只建議使用SharedPreferences,因為即使xml文件僅對應用程序可用,也可以在root設備上訪問它。
如果您已經使用了SqlLiteDB,我可能會使用它。 如果沒有,僅僅保存令牌就有點沉重。
編輯:
oauth令牌與用於簽署應用程序的密鑰和密鑰庫完全無關。
oauth令牌是服務器在驗證用戶憑據后在應用程序中提供的令牌。
密鑰庫包含一個或多個用於對應用程序進行數字簽名的證書。 這是為了防止其他人上傳與您的包名相同的應用並替換它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.