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

2023-09-11 20:16:28 作者:若雪

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

我已经在使用AES从本教程中取得成功做字符串加密http://www.androidsnippets.com/encryptdecrypt-strings.但我没有想法如何与图像做到这一点,不串。

这我怎么做字符串:

 公共静态字符串加密(字符串种子,字符串明文)抛出异常
{
    byte []的rawKey = getRawKey(seed.getBytes());
    byte []的结果=加密(rawKey,cleartext.getBytes());
    返回toHex(结果);
}


私有静态byte []的加密(byte []的原料,byte []的明确的)抛出异常
{
    SecretKeySpec skeySpec =新SecretKeySpec(原AES);
    密密码= Cipher.getInstance(AES);
    cipher.init(Cipher.ENCRYPT_MODE,skeySpec);
    byte []的加密= cipher.doFinal(清晰);
    返回加密;
}
 

任何人都可以帮我举个例子code如何加密的图片与AES?

也许它必须使用I / O的文件流,但我没有想法如何实现这一code。林初学在Android中,遗憾的英语不好和小白的问题。希望有人能帮助,谢谢:)

编辑:

我尝试使用的FileOutputStream,它不包含任何错误,但该文件没有加密。 这里是我的code:

**

的编辑:基里尔解决方案code工作:)的

**

因为这个工作文件,编辑我的问题的文件。谢谢

解决方案

编辑:如果您需要输入密码用户输入务必阅读:http://stackoverflow.com/a/3452620/1188357

安全升级,Android将默认启用加密流量

您应该看一看: CipherInputStream和CipherOutputStream.它们被用来加密和解密字节流。

编辑:所以在这里你去

我有一个名为明文。该文件包含:

 我是一个清晰的文本。
你好吗?
棒极了!
 

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

 静态无效加密()抛出IOException异常,抛出:NoSuchAlgorithmException,NoSuchPaddingException,InvalidKeyException将{
    //这里你读了明文。
    的FileInputStream FIS =新的FileInputStream(数据/明文);
    //这个流写入的密文。该流将通过另一个流包裹。
    FileOutputStream中FOS =新的FileOutputStream(数据/加密);

    //长度是16字节
    //当取得用户输入小心! http://stackoverflow.com/a/3452620/1188357
    SecretKeySpec SKS =新SecretKeySpec(MyDifficultPassw.getBytes(),AES);
    //创建密码
    密密码= Cipher.getInstance(AES);
    cipher.init(Cipher.ENCRYPT_MODE,SKS);
    //裹输出流
    CipherOutputStream COS =新CipherOutputStream(FOS,密码);
    //写入字节
    INT B:
    byte []的D​​ =新的字节[8]。
    而((二= fis.read(四))!=  -  1){
        cos.write(D,0,B);
    }
    //刷新并关闭流。
    cos.flush();
    cos.close();
    fis.close();
}
 

在执行该功能,应该有一个文件名加密。该文件包含加密的字符。

有关解密你的解密功能:

 静态无效解密()抛出IOException异常,抛出:NoSuchAlgorithmException,NoSuchPaddingException,InvalidKeyException将{
    的FileInputStream FIS =新的FileInputStream(数据/加密);

    FileOutputStream中FOS =新的FileOutputStream(数据/解密);
    SecretKeySpec SKS =新SecretKeySpec(MyDifficultPassw.getBytes(),AES);
    密密码= Cipher.getInstance(AES);
    cipher.init(Cipher.DECRYPT_MODE,SKS);
    CipherInputStream顺=新CipherInputStream(FIS,密码);
    INT B:
    byte []的D​​ =新的字节[8]。
    而((二= cis.read(四))!=  -  1){
        fos.write(D,0,B);
    }
    fos.flush();
    fos.close();
    cis.close();
}
 

解密的执行后,应该有一个文件名为解密。该文件包含了自由文本。

编辑:你写你是小白,但根据用例的加密,你可以做很多的伤害,如果你不这样做是正确的方式。了解你的工具!

的CipherOutputStream Oracle文档用法:

SecretKeySpec skeySpec =新SecretKeySpec(y.getBytes(),AES);

 的FileInputStream FIS;
FileOutputStream中FOS;
CipherOutputStream COS;
//文件你正在阅读
FIS =新的FileInputStream(/ tmp目录/ a.txt中);
//文件输出
FOS =新的FileOutputStream(/ tmp目录/ b.txt);

//此处,该文件​​是加密的。该cipher1必须创建。
//密钥长度为128,192或256位=>即16字节
SecretKeySpec skeySpec =新SecretKeySpec(MyDifficultPassw.getBytes(),AES);
密cipher1 = Cipher.getInstance(AES);
cipher1.init(Cipher.ENCRYPT_MODE,skeySpec);
COS =新CipherOutputStream(FOS,cipher1);
//在这里,您从FIS文件读写COS。
byte []的B =新的字节[8]。
INT I = fis.read(B);
而(ⅰ!=  -  1){
    cos.write(B,0,I);
    I = fis.read(B);
}
cos.flush();
 

因此​​,加密应该工作。当你相反的过程,你应该能够读取解密的字节数。

I want to encrypt image from the sd card and store it again in SD card again using AES. The main idea is the application browse an image, then encrypt it when i push a button, then store it in sd card. so my image would be secure.

i already succeed do string encryption using AES from this tutorial http://www.androidsnippets.com/encryptdecrypt-strings. but i dont have idea how to do this with an image, not string.

This how i do with string :

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;
}

anyone can help me give example code how to encrypt an image with AES?

maybe it must using i/o file stream but i dont have idea how to implement with this code. Im beginner in android, sorry for bad english and noob question. Hope anyone can help, thank you :)

Edited:

i try using FileOutputStream, it contain no error but the file not encrypted. here is my code:

**

edited: Kiril solution code works :)

**

since this work to file, i edit my question to file. thanks

解决方案

EDIT: If you take user input for the password make sure to read: http://stackoverflow.com/a/3452620/1188357

You should take a look at: CipherInputStream and CipherOutputStream. They are used to encrypt and decrypt byte streams.

EDIT: So here you go!

I have a file named cleartext. The file contains:

Hi, I'm a clear text.
How are you?
That's awesome!

Now, you have an encrypt() function:

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!!! http://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();
}

After you execute this function, there should be a file names encrypted. The file contains the encrypted characters.

For decryption you have the decrypt function:

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();
}

After the execution of decrypt, there should be a file named decrypted. This file contains the free text.

Edit: You write you're a "noob" but depending on the use-case of encryption you could do a lot of harm if you're not doing it the right way. Know your tools!

Usage of CipherOutputStream Oracle documentation:

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();

Thus, the encryption should work. When you reverse the process, you should be able to read the decrypted bytes.