网站优化

网站优化

Products

当前位置:首页 > 网站优化 >

阅读本文,轻松掌握Android AES加密,解锁安全编程技能!

GG网络技术分享 2025-11-13 00:16 5


java import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.security.NoSuchAlgorithmException;

public class AESExample {

public static void main {
    // 生成密钥
    SecretKey key = generateAESKey;
    // 加密文件
    String plainFilePath = "path/to/plainfile.txt";
    String encryptedFilePath = "path/to/encryptedfile.txt";
    encryptFile;
    // 解密文件
    String decryptedFilePath = "path/to/decryptedfile.txt";
    decryptFile;
}
private static SecretKey generateAESKey {
    KeyGenerator keyGenerator;
    try {
        keyGenerator = KeyGenerator.getInstance;
        keyGenerator.init; // 初始化密钥生成器,生成256位密钥
        return keyGenerator.generateKey;
    } catch  {
        throw new RuntimeException;
    }
}
private static void encryptFile {
    Cipher cipher;
    try {
        cipher = Cipher.getInstance;
        cipher.init;
        FileInputStream inputStream = new FileInputStream;
        FileOutputStream outputStream = new FileOutputStream;
        byte buffer = new byte;
        int len;
        while ) != -1) {
            outputStream.write);
        }
        outputStream.write);
        inputStream.close;
        outputStream.flush;
        outputStream.close;
    } catch  {
        throw new RuntimeException;
    }
}
private static void decryptFile {
    Cipher cipher;
    try {
        cipher = Cipher.getInstance;
        cipher.init;
        FileInputStream inputStream = new FileInputStream;
        FileOutputStream outputStream = new FileOutputStream;
        byte buffer = new byte;
        int len;
        while ) != -1) {
            outputStream.write);
        }
        outputStream.write);
        inputStream.close;
        outputStream.flush;
        outputStream.close;
    } catch  {
        throw new RuntimeException;
    }
}

}

这段代码实现了AES加密和优良密文件的功Neng。先说说生成一个256位的AES密钥,然后加密指定路径的文件,并将加密后的内容保存到另一个文件中。再说说用相同的密钥解密文件,将解密后的内容保存到另一个指定的文件中。请确保替换plainfile.txtencryptedfile.txt为实际的文件路径。

标签:

提交需求或反馈

Demand feedback