In Android or other projects often download and upload files, in order to secure these files we and the server unified encryption key, you can encrypt and decrypt files.
Code:
/**
* File files are encrypted and decrypted
*
* @param FILEURL
* File path
* @param key
* Password
* @throws Exception
*/
public static Boolean Decryptorencrypt (String fileUrl) {
try {
File File = new file (FILEURL);
if (!file.exists ()) {
return false;
}
FileInputStream fileforinput = new FileInputStream (file);
byte[] bytes = new byte[fileforinput.available ()];
Fileforinput.read (bytes);
String stren = Commonconfig.decrypt_key;
int nkeylen = Stren.length ();
int nIndex = 0;
FileOutputStream out = new FileOutputStream (FILEURL);
for (int i = 0; i < bytes.length; i++) {
Bytes[i] = (byte) (Bytes[i] ^ stren.getbytes () [NIndex]);
nindex++;
if (NIndex >= nkeylen) {
NIndex = 0;
}
}
Out.write (bytes, 0, bytes.length);
Out.flush ();
Fileforinput.close ();
Out.close ();
} catch (Exception e) {
E.printstacktrace ();
return false;
}
return true;
}
Simple custom encryption and decryption of Android files