How to make encryption and decryption on an application\'s files in sd-card? so that i may secure the files on the sdcard and no other person will be able to access outside of
You can simply encrypt the contents of the file while writing it to the file and then while retrieving decrypt it. SIMPLE EXAMPLE : link provided shows an example code for encrypt/Decrypt a string. Hope it helps
i found solution of the cryptography in android application, by which you are able to make secure the application data inside of the application and outside of application form sdcard file-manager will not able to access the confidential information of the application..
for that you can use two possible ways to make secure the application files.
change the file extension format with other extension so that the real file will not able to open by the user
you are able to user the Cryptography on the application file's contents.
Cryptography
here i am going to post one example source code in which the application file will encrypted with AES algorithm and it will not able to access by the user in file-manger but once user will come in application it will decryption with real form and working fine.
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();
}
}
});
}
}
I have written this program that will encrypt a file using AES and decrypt the same file. This will surely help you.
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();