Android加密传参
| 阅读时长: 11分钟
| 作者: GeoDev
| 浏览: 5

加密类
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!!";
private static final String IV = "16ByteIVParameter!";
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 {
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(256);
SecretKey secretKey = keyGen.generateKey();
String base64Key = Base64.getEncoder().encodeToString(secretKey.getEncoded());
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();
}
}
}