91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

android sharedpreference怎樣進行數據加密

小樊
81
2024-11-23 03:50:12
欄目: 編程語言

在Android中,SharedPreferences本身不提供內置的數據加密功能。但是,您可以在將數據存儲到SharedPreferences之前對其進行加密,并在從SharedPreferences讀取數據時進行解密。以下是一個使用AES加密和解密的示例:

首先,您需要添加加密庫,例如:

implementation 'com.google.crypto:crypto-commons:1.1.1'

然后,您可以使用以下代碼進行加密和解密:

import android.content.Context;
import android.security.keystore.KeyGenParameterSpec;
import android.security.keystore.KeyProperties;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.KeyStore;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

public class EncryptionHelper {

    private static final String KEY_ALIAS = "my_key_alias";
    private static final String TRANSFORMATION = "AES";

    public static SecretKey getSecretKey(Context context) throws NoSuchAlgorithmException, UnrecoverableKeyException {
        KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
        keyStore.load(null);

        if (!keyStore.containsAlias(KEY_ALIAS)) {
            KeyGenerator keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
            KeyGenParameterSpec keyGenParameterSpec = new KeyGenParameterSpec.Builder(
                    KEY_ALIAS, KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
                    .setBlockModes(KeyProperties.BLOCK_MODE_GCM)
                    .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
                    .build();
            keyGenerator.init(keyGenParameterSpec);
            keyGenerator.generateKey();
        }

        return (SecretKey) keyStore.getKey(KEY_ALIAS, null);
    }

    public static String encrypt(String data, SecretKey secretKey) throws Exception {
        Cipher cipher = Cipher.getInstance(TRANSFORMATION);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        byte[] encryptedBytes = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
        return Base64.getEncoder().encodeToString(encryptedBytes);
    }

    public static String decrypt(String encryptedData, SecretKey secretKey) throws Exception {
        Cipher cipher = Cipher.getInstance(TRANSFORMATION);
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        byte[] decodedBytes = Base64.getDecoder().decode(encryptedData);
        byte[] decryptedBytes = cipher.doFinal(decodedBytes);
        return new String(decryptedBytes, StandardCharsets.UTF_8);
    }
}

現在,您可以使用EncryptionHelper類來加密和解密數據:

try {
    Context context = getApplicationContext();
    SecretKey secretKey = EncryptionHelper.getSecretKey(context);

    String originalData = "Hello, World!";
    String encryptedData = EncryptionHelper.encrypt(originalData, secretKey);
    String decryptedData = EncryptionHelper.decrypt(encryptedData, secretKey);

    System.out.println("Original Data: " + originalData);
    System.out.println("Encrypted Data: " + encryptedData);
    System.out.println("Decrypted Data: " + decryptedData);
} catch (Exception e) {
    e.printStackTrace();
}

請注意,這個示例使用了Android KeyStore系統來生成和存儲密鑰。這樣,即使設備受到攻擊,密鑰也不會輕易泄露。

0
宁乡县| 永靖县| 扶沟县| 连州市| 招远市| 五台县| 蓬溪县| 万荣县| 淄博市| 兴仁县| 乌拉特后旗| 门头沟区| 巩义市| 马龙县| 张北县| 菏泽市| 五华县| 邵阳县| 当阳市| 垦利县| 连云港市| 舞阳县| 瑞安市| 邯郸市| 图木舒克市| 静海县| 江门市| 灯塔市| 大姚县| 溧阳市| 宁晋县| 博罗县| 襄垣县| 治县。| 花莲县| 福清市| 大化| 舟山市| 南溪县| 漾濞| 潢川县|