[Android] simple encryption of network data and android Encryption

Source: Internet
Author: User

[Android] simple encryption of network data and android Encryption
Principle: Base64 is the simplest method of data encryption. Although data redundancy occurs, it is easy to write and does not need to be modified. In fact, it can also adopt unique or encrypted data. Code: Let's go directly to the code here. It's easy to write.

Public static final boolean DownlaodAppFile (Context mContext, String url, String cacheName, boolean replace) {File root = mContext. getExternalCacheDir (); if (! Root. exists () {root. mkdir ();} String root_path = root. getAbsolutePath (); File cacheFile = new File (root_path + "/" + cacheName); if (cacheFile. exists () {if (! Replace) {return true ;}} File tmpFIle = new File (root_path + "/" + cacheName + ". tmp "); URL mURL = null; try {mURL = new URL (url);} catch (MalformedURLException e) {Log. I ("Finals", "URL error"); e. printStackTrace (); return false;} HttpURLConnection conn = null; FileOutputStream fos = null; Base64OutputStream bos = null; try {fos = new FileOutputStream (tmpFIle); bos = new Base64OutputStream (fos, 45); conn = (H TtpURLConnection) mURL. openConnection (); if (conn. getResponseCode () = 200) {// create a connection InputStream is = conn. getInputStream (); byte [] buffer = new byte [1024]; // cyclically retrieve data int len = 0; while (len = is. read (buffer ))! =-1) {bos. write (buffer, 0, len);} // release resource bos. close (); fos. close (); is. close (); conn. disconnect (); bos = null; fos = null; is = null; conn = null; tmpFIle. renameTo (cacheFile); System. out. println ("downloaded") ;}} catch (IOException e) {e. printStackTrace (); Log. I ("Finals", "Url connection error"); return false;} finally {if (fos! = Null) {try {fos. close ();} catch (IOException e) {e. printStackTrace () ;}}return true;} public static final String ReadAppFile (Context context, String cacheName) {File root = context. getExternalCacheDir (); String rootpath = root. getAbsolutePath (); File cacheFile = new File (rootpath + "/" + cacheName); if (! CacheFile. exists () {return "";} String result = ""; try {FileInputStream FCM = new FileInputStream (cacheFile); Base64InputStream bis = new Base64InputStream (FCM, 45 ); byteArrayOutputStream bos = new ByteArrayOutputStream (); byte [] buffer = new byte [512]; int len = 0; while (len = bis. read (buffer ))! =-1) {bos. write (buffer, 0, len);} result = bos. toString (); bos. close (); bis. close (); FCM. close (); bos = null; bis = null; FCM = null;} catch (IOException e) {e. printStackTrace (); return "";} return result ;}


Related Article

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.