android aes文件加密,如何在Android中使用AES从SD卡加密文件?

你应该看看: CipherInputStream和CipherOutputStream。它们用于加密和解密字节流。

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

Hi, I'm a clear text.

How are you?

That's awesome!

现在,您有了一个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();

}

执行此功能后,应该有一个文件名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();

}

执行解密后,应该有一个名为的文件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();

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值