如何对SD卡中的应用程序文件进行加密和解密?这样我就可以保护SD卡上的文件,其他人就不能在不解密这些文件的情况下访问该应用程序之外的内容。
有没有人可以给我一个很好的示例源码来实现android应用的加密?
发布于 2012-02-06 14:58:24
我已经写了这个程序,将加密文件使用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();发布于 2012-02-07 12:48:43
我找到了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();
}
}
});
}
}https://stackoverflow.com/questions/9156545
复制相似问题