[Android]網路資料的簡單加密,android加密

來源:互聯網
上載者:User

[Android]網路資料的簡單加密,android加密
原理:最簡單的資料加密就是採用Base64,雖然會帶有資料冗餘,但是寫法簡單,不用做過多的修改,其實也可以採用異或加密。代碼:這裡就直接上代碼吧,寫法很簡單。

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 = (HttpURLConnection) mURL.openConnection();if (conn.getResponseCode() == 200) {// 建立串連InputStream is = conn.getInputStream();byte[] buffer = new byte[1024];// 迴圈擷取資料int len = 0;while ((len = is.read(buffer)) != -1) {bos.write(buffer, 0, len);}// 釋放資源bos.close();fos.close();is.close();conn.disconnect();bos = null;fos = null;is = null;conn = null;tmpFIle.renameTo(cacheFile);System.out.println("下載完成");}} 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 fis = new FileInputStream(cacheFile);Base64InputStream bis = new Base64InputStream(fis, 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();fis.close();bos = null;bis = null;fis = null;} catch (IOException e) {e.printStackTrace();return "";}return result;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.