溫馨提示×

Android中JSON數(shù)據(jù)的加密解密方法

小樊
81
2024-10-15 03:51:51
欄目: 編程語言

在Android中,對JSON數(shù)據(jù)進行加密和解密可以使用多種方法。以下是一些建議的方法:

  1. 使用AES加密算法:

AES是一種對稱加密算法,可以使用相同的密鑰進行加密和解密。首先,需要生成一個隨機的密鑰,然后使用這個密鑰對JSON數(shù)據(jù)進行加密。解密時,使用相同的密鑰進行解密。

以下是使用AES加密和解密的示例代碼:

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class AESUtil {
    private static final String ALGORITHM = "AES/CBC/PKCS5Padding";
    private static final String TRANSFORMATION = "AES/CBC/PKCS5Padding";

    public static String encrypt(String data, String key) throws Exception {
        SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES");
        IvParameterSpec ivParameterSpec = new IvParameterSpec("1234567812345678".getBytes(StandardCharsets.UTF_8));
        Cipher cipher = Cipher.getInstance(TRANSFORMATION);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivParameterSpec);
        byte[] encryptedData = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
        return Base64.getEncoder().encodeToString(encryptedData);
    }

    public static String decrypt(String data, String key) throws Exception {
        SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES");
        IvParameterSpec ivParameterSpec = new IvParameterSpec("1234567812345678".getBytes(StandardCharsets.UTF_8));
        Cipher cipher = Cipher.getInstance(TRANSFORMATION);
        cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);
        byte[] decryptedData = cipher.doFinal(Base64.getDecoder().decode(data));
        return new String(decryptedData, StandardCharsets.UTF_8);
    }
}
  1. 使用RSA加密算法:

RSA是一種非對稱加密算法,可以使用一對公鑰和私鑰進行加密和解密。首先,需要生成一對公鑰和私鑰,然后使用公鑰對JSON數(shù)據(jù)進行加密。解密時,使用私鑰進行解密。

以下是使用RSA加密和解密的示例代碼:

import javax.crypto.Cipher;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Base64;

public class RSAUtil {
    public static void generateKeyPair() throws Exception {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(2048);
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        PublicKey publicKey = keyPair.getPublic();
        PrivateKey privateKey = keyPair.getPrivate();
        // 保存公鑰和私鑰,以便后續(xù)使用
    }

    public static String encrypt(String data, PublicKey publicKey) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] encryptedData = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
        return Base64.getEncoder().encodeToString(encryptedData);
    }

    public static String decrypt(String data, PrivateKey privateKey) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] decryptedData = cipher.doFinal(Base64.getDecoder().decode(data));
        return new String(decryptedData, StandardCharsets.UTF_8);
    }
}

請注意,這些示例代碼僅用于演示目的,實際應用中可能需要根據(jù)具體需求進行調(diào)整。在使用加密和解密時,請確保正確處理異常,并在安全的地方存儲密鑰和初始化向量(IV)。

0