🔐 加密特工行动:Android 中的 AES 与 RSA 秘密行动指南

​​故事背景​​:你是一名代号为 "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密钥...

// 现在总部也能解密核弹密码了!

​​行动守则​​:

  1. RSA 加密较慢,​只用于加密小数据(如AES密钥)​
  2. 2048-bit 是当前安全标准
  3. 私钥必须用 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

现在拿起你的加密工具,开始执行安全任务吧!记住:​​一次密钥泄露可能导致整个特工网络崩溃!​​ 保持警惕,安全第一! 🕵️♂️🔐

相关推荐
千里马学框架5 天前
一起学 Android 14:ShellTransition 屏幕旋转过程深度剖析
android·智能手机·性能优化·framework·性能·屏幕旋转·rotation
美狐美颜SDK开放平台5 天前
开发直播APP时如何接入视频美颜SDK?开发流程与注意事项
android·人工智能·计算机视觉·音视频·直播美颜sdk
AFinalStone5 天前
Android7 SystemUI源码解析(七)Keyguard锁屏模块深度解析
android·systemui
致远ccc5 天前
Google Play 上架前如何测试 App?多国家 Android 环境测试
android·app测试·googleplay·多国家应用测试
ttyyttemo5 天前
Kotlin 协程中的 Job 结构化并发与取消
android
sun0077005 天前
tbox 4g/5g切换,导致wan ip 改变,导致车机旧网络不可用。需要重启车机才行
android
其实防守也摸鱼5 天前
内网穿透与反向代理:原理、工具与实战指南
android·大数据·运维·安全·网络安全·自动化·渗透
AFinalStone5 天前
Android7 SystemUI 源码解析(四)NavigationBar 导航栏与 SystemBars
android·systemui
JMchen5 天前
属性动画原理与高级动画实现
android·kotlin·canvas
AFinalStone5 天前
Android7 SystemUI 源码解析(二)启动流程深度解析
android·systemui