GIS开发 | 空间分析 | 软硬件技术

Android加密传参

1ba16722eb9144c422628708d23e5afa.png

加密类

import org.json.JSONObject; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import java.util.Base64; public class AESUtils { private static final String ALGORITHM = "AES/CBC/PKCS5Padding"; private static final String KEY = "16ByteSecretKey!!"; // 必须16/24/32字节 private static final String IV = "16ByteIVParameter!"; // 必须16字节 // 加密多个字段(接收Map或自定义对象) public static String encryptFields(String... fields) throws Exception { JSONObject json = new JSONObject(); for(int i=0; i<fields.length; i+=2) { json.put(fields[i], fields[i+1]); } return encrypt(json.toString()); } // 基础加密方法 private static String encrypt(String value) throws Exception { SecretKeySpec keySpec = new SecretKeySpec(KEY.getBytes(), "AES"); IvParameterSpec ivSpec = new IvParameterSpec(IV.getBytes()); Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec); return Base64.getEncoder().encodeToString(cipher.doFinal(value.getBytes())); } // 解密并解析多个字段 public static JSONObject decryptToJson(String encrypted) throws Exception { return new JSONObject(decrypt(encrypted)); } // 基础解密方法 private static String decrypt(String encrypted) throws Exception { SecretKeySpec keySpec = new SecretKeySpec(KEY.getBytes(), "AES"); IvParameterSpec ivSpec = new IvParameterSpec(IV.getBytes()); Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec); return new String(cipher.doFinal(Base64.getDecoder().decode(encrypted))); } }

密钥生成类

import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.spec.IvParameterSpec; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.util.Base64; public class AESKeyGenerator { public static void main(String[] args) { try { // 生成256位AES密钥 KeyGenerator keyGen = KeyGenerator.getInstance("AES"); keyGen.init(256); // 可以是128, 192或256位 SecretKey secretKey = keyGen.generateKey(); String base64Key = Base64.getEncoder().encodeToString(secretKey.getEncoded()); // 生成16字节IV byte[] iv = new byte[16]; SecureRandom secureRandom = new SecureRandom(); secureRandom.nextBytes(iv); String base64IV = Base64.getEncoder().encodeToString(iv); System.out.println("生成的KEY (Base64编码): " + base64Key); System.out.println("生成的IV (Base64编码): " + base64IV); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } } }

发送类

android.content.Intent; import org.json.JSONObject; public class DataSenderActivity extends AppCompatActivity { void sendEncryptedData() { try { // 加密多个字段(键值对形式) String encrypted = AESUtils.encryptFields( "username", "张三", "age", "25", "balance", "1500.50" ); Intent intent = new Intent(this, ReceiverActivity.class); intent.putExtra("ENC_DATA", encrypted); startActivity(intent); } catch (Exception e) { e.printStackTrace(); } } }

接收类

import android.os.Bundle; import org.json.JSONObject; public class DataReceiverActivity extends AppCompatActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); try { String encrypted = getIntent().getStringExtra("ENC_DATA"); JSONObject data = AESUtils.decryptToJson(encrypted); String username = data.getString("username"); int age = data.getInt("age"); double balance = data.getDouble("balance"); } catch (Exception e) { e.printStackTrace(); } } }