如何在SD卡中的应用程序文件上进行加密和解密?这样我就可以保护SD卡上的文件,并且没有其他人能够访问该应用程序以外的文件而无需解密这些文件。Android文件密码学
有没有人可以给我任何好的示例源来实现Android应用程序的加密?
如何在SD卡中的应用程序文件上进行加密和解密?这样我就可以保护SD卡上的文件,并且没有其他人能够访问该应用程序以外的文件而无需解密这些文件。Android文件密码学
有没有人可以给我任何好的示例源来实现Android应用程序的加密?
我已经编写了这个程序,它将使用AES加密文件并解密相同的文件。这一定会帮助你。
FileInputStream fis = new FileInputStream(new File("D:/Shashank/Test123.txt"));
File outfile = new File("D:/Shashank/encTest1234.txt");
int read;
if(!outfile.exists())
outfile.createNewFile();
File decfile = new File("D:/Shashank/dec123.txt");
if(!decfile.exists())
decfile.createNewFile();
FileOutputStream fos = new FileOutputStream(outfile);
FileInputStream encfis = new FileInputStream(outfile);
FileOutputStream decfos = new FileOutputStream(decfile);
Cipher encipher = Cipher.getInstance("AES");
Cipher decipher = Cipher.getInstance("AES");
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecretKey skey = kgen.generateKey();
encipher.init(Cipher.ENCRYPT_MODE, skey);
CipherInputStream cis = new CipherInputStream(fis, encipher);
decipher.init(Cipher.DECRYPT_MODE, skey);
CipherOutputStream cos = new CipherOutputStream(decfos,decipher);
while((read = cis.read())!=-1)
{
fos.write((char)read);
fos.flush();
}
fos.close();
while((read=encfis.read())!=-1)
{
cos.write(read);
cos.flush();
}
cos.close();
您可以在写入文件的同时简单地加密文件的内容,然后在检索时解密文件。 SIMPLE EXAMPLE:提供的链接显示加密/解密字符串的示例代码。希望它有帮助
我发现了Android应用程序中的加密解决方案,通过它可以使应用程序数据在应用程序内部和应用程序外部形成安全,sdcard文件管理器将无法访问机密信息应用程序..
因为您可以使用两种可能的方法来确保应用程序文件的安全。
变化与其他扩展名的文件扩展名的格式,做到真正的文件将无法被用户
你能够用户对应用程序文件的内容的密码才能打开。
加密
在这里,我要发布一个示例源代码中的应用程序文件将与AES算法进行加密,它将不能够由用户在文件管理器,但一旦用户访问将进入应用程序它将解密与真实的形式和工作正常。
package com.filepermition.android;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class AndroidFilePermitionActivity extends Activity
{
Button btn_button;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn_button = (Button)findViewById(R.id.btn_button);
btn_button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try{
FileInputStream fis = new FileInputStream(
new File("/mnt/sdcard/testfile/file.wav"));
File outfile = new File("/mnt/sdcard/testfile/encTest1234.wav");
int read;
if(!outfile.exists())
outfile.createNewFile();
File decfile = new File("/mnt/sdcard/testfile/dec123.wav");
if(!decfile.exists())
decfile.createNewFile();
FileOutputStream fos = new FileOutputStream(outfile);
FileInputStream encfis = new FileInputStream(outfile);
FileOutputStream decfos = new FileOutputStream(decfile);
Cipher encipher = Cipher.getInstance("AES");
Cipher decipher = Cipher.getInstance("AES");
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecretKey skey = kgen.generateKey();
encipher.init(Cipher.ENCRYPT_MODE, skey);
CipherInputStream cis = new CipherInputStream(fis, encipher);
decipher.init(Cipher.DECRYPT_MODE, skey);
CipherOutputStream cos = new CipherOutputStream(decfos,decipher);
while((read = cis.read())!=-1)
{
fos.write((char)read);
fos.flush();
}
fos.close();
while((read=encfis.read())!=-1)
{
cos.write(read);
cos.flush();
}
cos.close();
}catch (Exception e) {
// TODO: handle exceptione
e.printStackTrace();
}
}
});
}
}
您好感谢这个这么多,我可以能够解决我的问题....非常感谢.... – 2012-02-06 10:04:19
这似乎使用ECB模式,无法初始化的RNG。我强烈建议任何人不要复制此代码并期望获得任何安全性。 – 2014-12-01 19:37:54