如何在Android中使用AES加密SD卡中的文件?

use*_*273 35 encryption android file aes

我想从SD卡加密图像并使用AES再次将其存储在SD卡中.主要思想是应用程序浏览图像,然后在按下按钮时对其进行加密,然后将其存储在SD卡中.所以我的形象是安全的.

我已经成功使用本教程http://www.androidsnippets.com/encryptdecrypt-strings中的 AES进行字符串加密,但我不知道如何使用图像而不是字符串来完成此操作.

这是我用字符串做的方式:

public static String encrypt(String seed, String cleartext) throws Exception  
{
    byte[] rawKey = getRawKey(seed.getBytes());
    byte[] result = encrypt(rawKey, cleartext.getBytes()); 
    return toHex(result);
}

private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception 
{
    SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
    byte[] encrypted = cipher.doFinal(clear);
    return encrypted;
}
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮我提供示例代码如何使用AES 加密图像

也许它必须使用I/O文件流,但我不知道如何使用此代码实现.

Kir*_*ril 66

如果您输入密码的用户输入,请务必阅读此答案.

你应该看看: CipherInputStreamCipherOutputStream.它们用于加密和解密字节流.

我有一个名为的文件cleartext.该文件包含:

Hi, I'm a clear text.
How are you?
That's awesome!
Run Code Online (Sandbox Code Playgroud)

现在,你有一个encrypt()功能:

static void encrypt() throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
    // Here you read the cleartext.
    FileInputStream fis = new FileInputStream("data/cleartext");
    // This stream write the encrypted text. This stream will be wrapped by another stream.
    FileOutputStream fos = new FileOutputStream("data/encrypted");

    // Length is 16 byte
    // Careful when taking user input!!! https://stackoverflow.com/a/3452620/1188357
    SecretKeySpec sks = new SecretKeySpec("MyDifficultPassw".getBytes(), "AES");
    // Create cipher
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, sks);
    // Wrap the output stream
    CipherOutputStream cos = new CipherOutputStream(fos, cipher);
    // Write bytes
    int b;
    byte[] d = new byte[8];
    while((b = fis.read(d)) != -1) {
        cos.write(d, 0, b);
    }
    // Flush and close streams.
    cos.flush();
    cos.close();
    fis.close();
}
Run Code Online (Sandbox Code Playgroud)

执行此功能后,应该有一个文件名encrypted.该文件包含加密的字符.

对于解密,您有以下decrypt功能:

static void decrypt() throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
    FileInputStream fis = new FileInputStream("data/encrypted");

    FileOutputStream fos = new FileOutputStream("data/decrypted");
    SecretKeySpec sks = new SecretKeySpec("MyDifficultPassw".getBytes(), "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, sks);
    CipherInputStream cis = new CipherInputStream(fis, cipher);
    int b;
    byte[] d = new byte[8];
    while((b = cis.read(d)) != -1) {
        fos.write(d, 0, b);
    }
    fos.flush();
    fos.close();
    cis.close();
}
Run Code Online (Sandbox Code Playgroud)

执行decrypt后,应该有一个名为的文件decrypted.该文件包含自由文本.

你写的是一个"菜鸟",但根据加密的使用情况,如果你没有以正确的方式做到这一点,你可能会造成很大的伤害.了解你的工具!

使用CipherOutputStream Oracle文档:

SecretKeySpec skeySpec = new SecretKeySpec(y.getBytes(), "AES");

FileInputStream fis;
FileOutputStream fos;
CipherOutputStream cos;
// File you are reading from
fis = new FileInputStream("/tmp/a.txt");
// File output
fos = new FileOutputStream("/tmp/b.txt");

// Here the file is encrypted. The cipher1 has to be created.
// Key Length should be 128, 192 or 256 bit => i.e. 16 byte
SecretKeySpec skeySpec = new SecretKeySpec("MyDifficultPassw".getBytes(), "AES"); 
Cipher cipher1 = Cipher.getInstance("AES");  
cipher1.init(Cipher.ENCRYPT_MODE, skeySpec);
cos = new CipherOutputStream(fos, cipher1);
// Here you read from the file in fis and write to cos.
byte[] b = new byte[8];
int i = fis.read(b);
while (i != -1) {
    cos.write(b, 0, i);
    i = fis.read(b);
}
cos.flush();
Run Code Online (Sandbox Code Playgroud)

因此,加密应该工作.当您反转该过程时,您应该能够读取解密的字节.

  • 编辑:我尝试在Android清单中编辑权限(android.permission.WRITE_EXTERNAL_STORAGE),加密/解密工作.谢谢Kiril :) (4认同)
  • 一般建议:**始终使用完全限定的密码字符串。** `Cipher.getInstance("AES");` 可能会导致不同的密码,具体取决于默认的安全提供程序。它很可能会导致“AES/ECB/PKCS5Padding”,但并非必须如此。如果它发生变化,您将失去不同 JVM 之间的兼容性。 (2认同)