故事背景:你是一名代号为 "Cipher" 的秘密特工,Android 设备是你的任务终端。AES 是你的「加密手提箱」,RSA 是你的「安全通信频道」。每份情报都需要双重保护才能安全传递!
🧳 第一章:AES 手提箱 - 对称加密特工装备
特工原理:同一把钥匙开锁关锁
AES 像密码手提箱,加密解密用同一密钥,适合大量情报快速传递
java
java
Copy
public class AesSecretAgent {
private static final String ALGORITHM = "AES";
private static final String TRANSFORMATION = "AES/CBC/PKCS5Padding";
// 特工装备初始化
public static SecretKey generateKey() throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);
keyGenerator.init(256); // 选择256位超强锁
return keyGenerator.generateKey();
}
// 装入情报并上锁(加密)
public static byte[] encrypt(byte[] data, SecretKey key) throws Exception {
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.ENCRYPT_MODE, key);
return cipher.doFinal(data); // 返回加密后的密文
}
// 解锁获取情报(解密)
public static byte[] decrypt(byte[] encryptedData, SecretKey key) throws Exception {
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.DECRYPT_MODE, key);
return cipher.doFinal(encryptedData);
}
}
行动演示:传递核弹密码
ini
java
Copy
// 特工Cipher的行动
SecretKey missionKey = AesSecretAgent.generateKey();
// 原始情报
String nuclearCodes = "Titan-7: 98234-23847-12983";
byte[] rawData = nuclearCodes.getBytes(StandardCharsets.UTF_8);
// 上锁加密
byte[] encrypted = AesSecretAgent.encrypt(rawData, missionKey);
Log.d("AGENT", "加密情报: " + Base64.encodeToString(encrypted));
// 解锁解密
byte[] decrypted = AesSecretAgent.decrypt(encrypted, missionKey);
String decoded = new String(decrypted, StandardCharsets.UTF_8);
Log.d("AGENT", "解密情报: " + decoded); // 正确显示核弹密码
关键参数:
AES/CBC/PKCS5Padding
:CBC模式更安全,PKCS5填充保证数据对齐256-bit keys
:当前最安全的密钥长度- 重要:必须安全保存密钥!泄露=手提箱被撬
📡 第二章:RSA 安全频道 - 非对称加密通信网
特工原理:公钥锁箱,私钥开箱
RSA 像安全信箱:所有人可用公钥投递,只有收件人用私钥读取
java
java
Copy
public class RsaCommunication {
private static final String ALGORITHM = "RSA";
private static final String TRANSFORMATION = "RSA/ECB/PKCS1Padding";
// 创建密钥对(信箱系统)
public static KeyPair generateKeyPair() throws Exception {
KeyPairGenerator keyGen = KeyPairGenerator.getInstance(ALGORITHM);
keyGen.initialize(2048); // 2048位安全等级
return keyGen.generateKeyPair();
}
// 用公钥加密情报(投递到信箱)
public static byte[] encryptWithPublicKey(byte[] data, PublicKey publicKey) throws Exception {
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return cipher.doFinal(data);
}
// 用私钥解密情报(开箱读取)
public static byte[] decryptWithPrivateKey(byte[] encryptedData, PrivateKey privateKey) throws Exception {
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
return cipher.doFinal(encryptedData);
}
}
行动演示:传递密钥给总部
ini
java
Copy
// 总部生成密钥对
KeyPair hqKeyPair = RsaCommunication.generateKeyPair();
PublicKey hqPublicKey = hqKeyPair.getPublic(); // 公钥公开
PrivateKey hqPrivateKey = hqKeyPair.getPrivate(); // 私钥绝密保存
// 特工获取总部公钥
String aesKeyBase64 = Base64.encodeToString(missionKey.getEncoded());
// 用公钥加密AES密钥
byte[] encryptedKey = RsaCommunication.encryptWithPublicKey(
aesKeyBase64.getBytes(),
hqPublicKey
);
// 总部用私钥解密
byte[] decryptedKeyData = RsaCommunication.decryptWithPrivateKey(
encryptedKey,
hqPrivateKey
);
String decodedKey = new String(decryptedKeyData);
SecretKey receivedKey = // 从Base64重建AES密钥...
// 现在总部也能解密核弹密码了!
行动守则:
- RSA 加密较慢,只用于加密小数据(如AES密钥)
2048-bit
是当前安全标准- 私钥必须用
AndroidKeyStore
保护!
🚨 第三章:联合行动 - AES+RSA 黄金组合
任务目标:安全传递3GB的机密卫星图像
行动计划:

代码实现:
ini
java
Copy
// 特工端:打包发送
public void sendClassifiedFile(File satelliteImage, PublicKey hqPublicKey) {
// 生成一次性AES密钥
SecretKey sessionKey = AesSecretAgent.generateKey();
// AES加密大文件
byte[] encryptedImage = AesSecretAgent.encrypt(
Files.readAllBytes(satelliteImage.toPath()),
sessionKey
);
// RSA加密AES密钥
byte[] encryptedKey = RsaCommunication.encryptWithPublicKey(
sessionKey.getEncoded(),
hqPublicKey
);
// 传输加密数据包
uploadToServer(encryptedImage, encryptedKey);
}
// 总部:接收解密
public void receiveClassifiedFile(byte[] encryptedImage, byte[] encryptedKey) {
// RSA解密获得AES密钥
byte[] decryptedKey = RsaCommunication.decryptWithPrivateKey(
encryptedKey,
hqPrivateKey
);
SecretKey sessionKey = new SecretKeySpec(decryptedKey, "AES");
// AES解密大文件
byte[] decryptedData = AesSecretAgent.decrypt(encryptedImage, sessionKey);
saveAsImage(decryptedData);
}
⚠ 第四章:反间谍技术 - 安全增强措施
防御 1:AndroidKeyStore 保险库
用系统级安全区存储密钥
ini
java
Copy
// 创建受KeyStore保护的RSA密钥
KeyPairGenerator keyGen = KeyPairGenerator.getInstance(
KeyProperties.KEY_ALGORITHM_RSA,
"AndroidKeyStore"
);
KeyGenParameterSpec spec = new KeyGenParameterSpec.Builder(
"HQ_KeyPair",
KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT
)
.setDigests(KeyProperties.DIGEST_SHA256)
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_RSA_PKCS1)
.build();
keyGen.initialize(spec);
KeyPair keyPair = keyGen.generateKeyPair(); // 密钥永不离开安全区
防御 2:初始化向量(IV)防破解
给AES加密增加随机性
ini
java
Copy
// 加密时生成随机IV
byte[] iv = new byte[16];
new SecureRandom().nextBytes(iv);
IvParameterSpec ivSpec = new IvParameterSpecSpec(iv);
cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);
// 需要将IV和密文一起传输
byte[] encrypted = cipher.doFinal(data);
ByteBuffer output = ByteBuffer.allocate(4 + iv.length + encrypted.length);
output.putInt(iv.length);
output.put(iv);
output.put(encrypted);
防御 3:消息认证码(HMAC)防篡改
验证情报完整性
ini
java
Copy
// 生成HMAC签名
Mac hmac = Mac.getInstance("HmacSHA256");
hmac.init(macKey); // 另个密钥
byte[] signature = hmac.doFinal(encryptedData);
// 验证时重新计算对比
byte[] recomputed = hmac.doFinal(receivedData);
if (!Arrays.equals(signature, recomputed)) {
throw new SecurityException("情报被篡改!");
}
🔐 特工安全手册
场景 | 解决方案 | 注意事项 |
---|---|---|
大文件加密 | AES-256-CBC | 配合随机IV |
密钥传递 | RSA-2048 | 私钥用AndroidKeyStore |
数据完整性验证 | HMAC-SHA256 | 使用独立密钥 |
长期存储敏感数据 | RSA加密 + AES本地存储 | 避免硬编码密钥 |
网络传输 | HTTPS + 应用层加密 | 双重保护 |
行动原则:
- AES 是主力特工,负责大量数据处理
- RSA 是联络员,负责密钥安全传递
- 永远验证对方身份(证书/数字签名)
- 在 Android 上优先使用
AndroidKeyStore
现在拿起你的加密工具,开始执行安全任务吧!记住:一次密钥泄露可能导致整个特工网络崩溃! 保持警惕,安全第一! 🕵️♂️🔐