Simple custom encryption and decryption of Android files

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.