我如何解密文件在Android中使用AES? [英] How do i decrypt a file in Android with AES?
问题描述
我有一个文件(MP3),我加密,我的本意是那么这个文件下载到Android设备和解密,但解密的过程中我得到一个IOException:
java.io.IOException异常:最后一个块解密不全
我知道在以下code明显的安全缺陷,我只是想获得它第一次工作。
任何帮助是AP preciated,我是一个相当有初来乍到的编码一般这样原谅我提前如果这是一个愚蠢的问题!
加密类(在Android中没有做过,效果):
公共类EncryptFile {
公共静态无效的主要(字符串ARGS []){
如果(args.length&所述; 1){
的System.out.println(用法:JAVA EncryptFile<文件名称>);
System.exit(-1);
}
尝试{
文件aesFile =新的文件(恩codedfile.enc);
FIS的FileInputStream;
FileOutputStream中FOS;
CipherInputStream顺;
//秘密密钥创建
字符串键=mysecretkey;
INT长度= key.length();
如果(长度GT; 16安培;&安培;长度= 16!){
键= key.substring(0,15);
}
如果(长度包括16安培;&放大器;长度= 16!){
的for(int i = 0; I< 16长;我++){
键=键+0;
}
}
SecretKeySpec SecretKey的=新SecretKeySpec(key.getBytes(),AES);
//密码的创建对象
密加密= Cipher.getInstance(AES / CBC / PKCS5Padding); 字节[] aByte = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
IvParameterSpec ivSpec =新IvParameterSpec(aByte); encrypt.init(Cipher.ENCRYPT_MODE,SecretKey的,ivSpec);
尝试{
FIS =新的FileInputStream(参数[0]);
顺=新CipherInputStream(FIS,加密);
//写入加密文件
FOS =新的FileOutputStream(aesFile);
字节[] B =新的字节[8];
INT I = cis.read(B);
而(ⅰ!= -1){
fos.write(B,0,I);
I = cis.read(B);
}
fos.flush();
fos.close();
cis.close();
fis.close();
}赶上(IOException异常错误){
的System.out.println(无法打开文件!);
System.exit(-1);
}
}赶上(例外五){
e.printStackTrace();
}
}
}
解密(在Android设备)
公共类DecryptFile {
公共静态文件的主要(字串ARGS [],文件encFile,上下文的背景下){
的for(int i = 0; I< args.length;我++){
Log.i(ARGS,ARGS [I]);
}
尝试{
文件aesFile =新的文件(参数[0]); aesFile = encFile;
Log.d(AESFILELENGTH,AES长度:+ aesFile.length());
文件aesFileBis =新的文件(context.getFilesDir(),ARGS [0]); FIS的FileInputStream;
FileOutputStream中FOS;
CipherInputStream顺; //秘密密钥创建
字符串键=mysecretkey;
INT长度= key.length();
如果(长度GT; 16安培;&安培;长度= 16!){
键= key.substring(0,15);
}
如果(长度包括16安培;&放大器;长度= 16!){
的for(int i = 0; I< 16长;我++){
键=键+0;
}
}
SecretKeySpec SecretKey的=新SecretKeySpec(key.getBytes(),AES);
//密码的创建对象
密解密= Cipher.getInstance(AES / CBC / PKCS5Padding); 字节[] aByte = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
IvParameterSpec ivSpec =新IvParameterSpec(aByte); decrypt.init(Cipher.DECRYPT_MODE,SecretKey的,ivSpec); //打开加密文件 FIS =新的FileInputStream(aesFile); 顺=新CipherInputStream(FIS,解密); //写入解密文件
FOS =新的FileOutputStream(aesFileBis);
尝试{ 字节[] =兆字节新的字节[8];
INT I = cis.read(兆字节);
Log.i(兆字节,兆字节我:+ I);
而(ⅰ!= -1){
fos.write(兆字节,0,I);
I = cis.read(兆字节);
}
}赶上(IOException异常五){
e.printStackTrace();
}
fos.flush();
fos.close();
cis.close();
fis.close();
返回aesFileBis;
}赶上(例外五){
e.printStackTrace();
}
返回null;
}
}
公共类MainActivity延伸活动{
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main); 按钮encryptButton =(按钮)findViewById(R.id.button1);
按钮DecryptButton =(按钮)findViewById(R.id.button2);
encryptButton.setOnClickListener(新OnClickListener(){ @覆盖
公共无效的onClick(视图v){
// TODO自动生成方法存根
尝试{
加密();
}赶上(InvalidKeyException将E){
// TODO自动生成catch块
e.printStackTrace();
}赶上(抛出:NoSuchAlgorithmException E){
// TODO自动生成catch块
e.printStackTrace();
}赶上(NoSuchPaddingException E){
// TODO自动生成catch块
e.printStackTrace();
}赶上(IOException异常五){
// TODO自动生成catch块
e.printStackTrace();
}
}
}); DecryptButton.setOnClickListener(新OnClickListener(){ @覆盖
公共无效的onClick(视图v){
// TODO自动生成方法存根
尝试{
解密();
}赶上(InvalidKeyException将E){
// TODO自动生成catch块
e.printStackTrace();
}赶上(抛出:NoSuchAlgorithmException E){
// TODO自动生成catch块
e.printStackTrace();
}赶上(NoSuchPaddingException E){
// TODO自动生成catch块
e.printStackTrace();
}赶上(IOException异常五){
// TODO自动生成catch块
e.printStackTrace();
}
}
}); } / **
*下面是两种加密功能和SD卡的文件夹解密文件。我们
*不能锁定文件夹,但是我们可以在Android中使用AES加密文件,它可能
* 帮你。
*
*引发IOException
* @throws抛出:NoSuchAlgorithmException
* @throws NoSuchPaddingException
*抛出:InvalidKeyException将
* / 静态无效的加密()抛出IOException异常,抛出:NoSuchAlgorithmException,
NoSuchPaddingException,InvalidKeyException将{
//这里你读了明文。
文件extStore = Environment.getExternalStorageDirectory();
FIS的FileInputStream =新的FileInputStream(extStore +/ sampleFile);
//这个流写入加密后的文本。这个流将被包裹
//另一个流。
FOS的FileOutputStream =新的FileOutputStream(extStore +/加密); //长度是16字节
SecretKeySpec SKS =新SecretKeySpec(MyDifficultPassw.getBytes()
AES);
//创建密码
密密码= Cipher.getInstance(AES);
cipher.init(Cipher.ENCRYPT_MODE,SKS);
//裹输出流
CipherOutputStream COS =新CipherOutputStream(FOS,密码);
//写入字节
INT B:
字节[] D =新的字节[8];
而((二= fis.read(D))!= - 1){
cos.write(D,0,B);
}
//刷新并关闭流。
cos.flush();
cos.close();
fis.close();
} 静态无效解密()抛出IOException异常,抛出:NoSuchAlgorithmException,
NoSuchPaddingException,InvalidKeyException将{ 文件extStore = Environment.getExternalStorageDirectory();
FIS的FileInputStream =新的FileInputStream(extStore +/加密); FOS的FileOutputStream =新的FileOutputStream(extStore +/解密);
SecretKeySpec SKS =新SecretKeySpec(MyDifficultPassw.getBytes()
AES);
密密码= Cipher.getInstance(AES);
cipher.init(Cipher.DECRYPT_MODE,SKS);
CipherInputStream顺=新CipherInputStream(FIS,密码);
INT B:
字节[] D =新的字节[8];
而((二= cis.read(D))!= - 1){
fos.write(D,0,B);
}
fos.flush();
fos.close();
cis.close();
}
}
I have a file (mp3) that i Encrypt, my intention is then to download this file to an android device and decrypt it, but during decryption i get an IOException:
java.io.IOException: last block incomplete in decryption
I am aware of the obvious security flaws in the following code, I'm just trying to get it to work first.
Any help on this is appreciated and I am quite a newcomer to coding in general so excuse me in advance if this is a stupid question!
Encryption class(Not done in Android, works):
public class EncryptFile {
public static void main(String args[]) {
if (args.length < 1) {
System.out.println("Usage: java EncryptFile <file name>");
System.exit(-1);
}
try {
File aesFile = new File("encodedfile.enc");
FileInputStream fis;
FileOutputStream fos;
CipherInputStream cis;
//Creation of Secret key
String key = "mysecretkey";
int length=key.length();
if(length>16 && length!=16){
key=key.substring(0, 15);
}
if(length<16 && length!=16){
for(int i=0;i<16-length;i++){
key=key+"0";
}
}
SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(),"AES");
//Creation of Cipher objects
Cipher encrypt =Cipher.getInstance("AES/CBC/PKCS5Padding");
byte[] aByte = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
IvParameterSpec ivSpec = new IvParameterSpec(aByte);
encrypt.init(Cipher.ENCRYPT_MODE, secretKey, ivSpec);
try {
fis = new FileInputStream(args[0]);
cis = new CipherInputStream(fis,encrypt);
// Write to the Encrypted file
fos = new FileOutputStream(aesFile);
byte[] b = new byte[8];
int i = cis.read(b);
while (i != -1) {
fos.write(b, 0, i);
i = cis.read(b);
}
fos.flush();
fos.close();
cis.close();
fis.close();
} catch(IOException err) {
System.out.println("Cannot open file!");
System.exit(-1);
}
} catch(Exception e){
e.printStackTrace();
}
}
}
Decryption (In Android device):
public class DecryptFile {
public static File main(String args[], File encFile, Context context) {
for (int i = 0; i < args.length; i++) {
Log.i("ARGS", args[i]);
}
try {
File aesFile = new File(args[0]);
aesFile= encFile;
Log.d("AESFILELENGTH", "aes length: " + aesFile.length());
File aesFileBis = new File(context.getFilesDir(), args[0]);
FileInputStream fis;
FileOutputStream fos;
CipherInputStream cis;
//Creation of Secret key
String key = "mysecretkey";
int length=key.length();
if(length>16 && length!=16){
key=key.substring(0, 15);
}
if(length<16 && length!=16){
for(int i=0;i<16-length;i++){
key=key+"0";
}
}
SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(),"AES");
//Creation of Cipher objects
Cipher decrypt =Cipher.getInstance("AES/CBC/PKCS5Padding");
byte[] aByte = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
IvParameterSpec ivSpec = new IvParameterSpec(aByte);
decrypt.init(Cipher.DECRYPT_MODE, secretKey, ivSpec);
// Open the Encrypted file
fis = new FileInputStream(aesFile);
cis = new CipherInputStream(fis, decrypt);
// Write to the Decrypted file
fos = new FileOutputStream(aesFileBis);
try {
byte[] mByte = new byte[8];
int i = cis.read(mByte);
Log.i("MBYTE", "mbyte i: " + i);
while (i != -1) {
fos.write(mByte, 0, i);
i = cis.read(mByte);
}
} catch (IOException e) {
e.printStackTrace();
}
fos.flush();
fos.close();
cis.close();
fis.close();
return aesFileBis;
} catch(Exception e){
e.printStackTrace();
}
return null;
}
}
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button encryptButton = (Button) findViewById(R.id.button1);
Button DecryptButton = (Button) findViewById(R.id.button2);
encryptButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
encrypt();
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
DecryptButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
decrypt();
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
/**
* Here is Both function for encrypt and decrypt file in Sdcard folder. we
* can not lock folder but we can encrypt file using AES in Android, it may
* help you.
*
* @throws IOException
* @throws NoSuchAlgorithmException
* @throws NoSuchPaddingException
* @throws InvalidKeyException
*/
static void encrypt() throws IOException, NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException {
// Here you read the cleartext.
File extStore = Environment.getExternalStorageDirectory();
FileInputStream fis = new FileInputStream(extStore + "/sampleFile");
// This stream write the encrypted text. This stream will be wrapped by
// another stream.
FileOutputStream fos = new FileOutputStream(extStore + "/encrypted");
// Length is 16 byte
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();
}
static void decrypt() throws IOException, NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException {
File extStore = Environment.getExternalStorageDirectory();
FileInputStream fis = new FileInputStream(extStore + "/encrypted");
FileOutputStream fos = new FileOutputStream(extStore + "/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();
}
}
这篇关于我如何解密文件在Android中使用AES?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!