如何在Android中使用AES从SD卡加密文件? [英] How to encrypt file from SD card using AES in Android?

查看:148
本文介绍了如何在Android中使用AES从SD卡加密文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



这是我用一个字符串来做的:

  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异常
{
SecretKeySpec skeySpec = new SecretKeySpec(raw, AES);
密码密码= Cipher.getInstance(AES);
cipher.init(Cipher.ENCRYPT_MODE,skeySpec);
byte [] encrypted = cipher.doFinal(clear);
返回加密;
}

任何人都可以帮助我给出示例代码如何加密 / strong>使用AES?



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

解决方案

如果您为用户输入密码,请务必阅读这个答案



你应该看看:
CipherInputStream CipherOutputStream 。我们有一个名为 cleartext 的文件。该文件包含:


 您好,我是一个明文。 
你好吗
这太棒了!


现在,您有一个 encrypt )函数:

  static void encrypt()throws IOException,NoSuchAlgorithmException,NoSuchPaddingException,InvalidKeyException {
//在这里你阅读明文。
FileInputStream fis = new FileInputStream(data / cleartext);
//这个流写入加密的文本。此流将被另一个流包裹。
FileOutputStream fos = new FileOutputStream(data / encrypted);

//长度为16字节
//当用户输入时请小心! https://stackoverflow.com/a/3452620/1188357
SecretKeySpec sks = new SecretKeySpec(MyDifficultPassw.getBytes(),AES);
//创建密码
密码密码= Cipher.getInstance(AES);
cipher.init(Cipher.ENCRYPT_MODE,sks);
//包装输出流
CipherOutputStream cos = new CipherOutputStream(fos,cipher);
//写入字节
int b;
byte [] d = new byte [8];
while((b = fis.read(d))!= -1){
cos.write(d,0,b);
}
//刷新并关闭流。
cos.flush();
cos.close();
fis.close();
}

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



对于解密,您有 decrypt 函数:

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

FileOutputStream fos = new FileOutputStream(data / decryptpted);
SecretKeySpec sks = new SecretKeySpec(MyDifficultPassw.getBytes(),AES);
密码密码= 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();
}

执行解密后,应该有一个名为解密。这个文件包含免费文本。



你写你是一个noob,但是根据加密的用例,你可以做很多的伤害,不要做正确的方法。知道你的工具!



CipherOutputStream的使用 Oracle文档

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

FileInputStream fis;
FileOutputStream fos;
CipherOutputStream cos;
//从
读取的文件fis = new FileInputStream(/ tmp / a.txt);
//文件输出
fos = new FileOutputStream(/ tmp / b.txt);

//这里文件被加密。必须创建cipher1。
//密钥长度应为128,192或256位=>即16字节
SecretKeySpec skeySpec = new SecretKeySpec(MyDifficultPassw.getBytes(),AES);
密码cipher1 = Cipher.getInstance(AES);
cipher1.init(Cipher.ENCRYPT_MODE,skeySpec);
cos = new CipherOutputStream(fos,cipher1);
//这里你从fis文件中读取并写入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();

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


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 don't have idea how to do this with an image, not string.

This is how I do it with a 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;
}

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

maybe it must using I/O file stream but I don't have an idea how to implement with this code.

解决方案

If you take user input for the password make sure to read this answer.

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

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

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.

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.

这篇关于如何在Android中使用AES从SD卡加密文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆