生活随笔
收集整理的這篇文章主要介紹了
Android和.NET通用的AES算法 (转) 好东东 收藏一下
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
轉(zhuǎn)自:http://www.cnblogs.com/ahui/archive/2011/04/22/2025045.html
1.NET源代碼:
view sourceprint? | 003 | using System.Security.Cryptography; |
| 005 | namespace ConsoleApplicationDemo |
| 010 | ????public class AESHelper |
| 014 | ????????/// 密鑰(32位,不足在后面補(bǔ)0) |
| 015 | ????????/// </summary> |
| 016 | ????????private const string _passwd = "ihlih*0037JOHT*)(PIJY*(()JI^)IO%"; |
| 019 | ????????/// </summary> |
| 020 | ????????private static CipherMode _cipherMode = CipherMode.ECB; |
| 023 | ????????/// </summary> |
| 024 | ????????private static PaddingMode _paddingMode = PaddingMode.PKCS7; |
| 027 | ????????/// </summary> |
| 028 | ????????private static Encoding _encoding = Encoding.UTF8; |
| 033 | ????????/// 獲取32byte密鑰數(shù)據(jù) |
| 034 | ????????/// </summary> |
| 035 | ????????/// <param name="password">密碼</param> |
| 036 | ????????/// <returns></returns> |
| 037 | ????????private static byte[] GetKeyArray(string password) |
| 039 | ????????????if (password == null) |
| 041 | ????????????????password = string.Empty; |
| 044 | ????????????if (password.Length < 32) |
| 046 | ????????????????password = password.PadRight(32, '0'); |
| 048 | ????????????else if (password.Length > 32) |
| 050 | ????????????????password = password.Substring(0, 32); |
| 053 | ????????????return _encoding.GetBytes(password); |
| 057 | ????????/// 將字符數(shù)組轉(zhuǎn)換成字符串 |
| 058 | ????????/// </summary> |
| 059 | ????????/// <param name="inputData"></param> |
| 060 | ????????/// <returns></returns> |
| 061 | ????????private static string ConvertByteToString(byte[] inputData) |
| 063 | ????????????StringBuilder sb = new StringBuilder(inputData.Length * 2); |
| 064 | ????????????foreach (var b in inputData) |
| 066 | ????????????????sb.Append(b.ToString("X2")); |
| 068 | ????????????return sb.ToString(); |
| 072 | ????????/// 將字符串轉(zhuǎn)換成字符數(shù)組 |
| 073 | ????????/// </summary> |
| 074 | ????????/// <param name="inputString"></param> |
| 075 | ????????/// <returns></returns> |
| 076 | ????????private static byte[] ConvertStringToByte(string inputString) |
| 078 | ????????????if (inputString == null || inputString.Length < 2) |
| 080 | ????????????????throw new ArgumentException(); |
| 082 | ????????????int l = inputString.Length / 2; |
| 083 | ????????????byte[] result = new byte[l]; |
| 084 | ????????????for (int i = 0; i < l; ++i) |
| 086 | ????????????????result[i] = Convert.ToByte(inputString.Substring(2 * i, 2), 16); |
| 089 | ????????????return result; |
| 095 | ????????/// 加密字節(jié)數(shù)據(jù) |
| 096 | ????????/// </summary> |
| 097 | ????????/// <param name="inputData">要加密的字節(jié)數(shù)據(jù)</param> |
| 098 | ????????/// <param name="password">密碼</param> |
| 099 | ????????/// <returns></returns> |
| 100 | ????????public static byte[] Encrypt(byte[] inputData, string password) |
| 102 | ????????????AesCryptoServiceProvider aes = new AesCryptoServiceProvider(); |
| 103 | ????????????aes.Key = GetKeyArray(password); |
| 104 | ????????????aes.Mode = _cipherMode; |
| 105 | ????????????aes.Padding = _paddingMode; |
| 106 | ????????????ICryptoTransform transform = aes.CreateEncryptor(); |
| 107 | ????????????byte[] data = transform.TransformFinalBlock(inputData, 0, inputData.Length); |
| 108 | ????????????aes.Clear(); |
| 109 | ????????????return data; |
| 113 | ????????/// 加密字符串(加密為16進(jìn)制字符串) |
| 114 | ????????/// </summary> |
| 115 | ????????/// <param name="inputString">要加密的字符串</param> |
| 116 | ????????/// <param name="password">密碼</param> |
| 117 | ????????/// <returns></returns> |
| 118 | ????????public static string Encrypt(string inputString, string password) |
| 120 | ????????????byte[] toEncryptArray = _encoding.GetBytes(inputString); |
| 121 | ????????????byte[] result = Encrypt(toEncryptArray, password); |
| 122 | ????????????return ConvertByteToString(result); |
| 126 | ????????/// 字符串加密(加密為16進(jìn)制字符串) |
| 127 | ????????/// </summary> |
| 128 | ????????/// <param name="inputString">需要加密的字符串</param> |
| 129 | ????????/// <returns>加密后的字符串</returns> |
| 130 | ????????public static string EncryptString(string inputString) |
| 132 | ????????????return Encrypt(inputString, _passwd); |
| 138 | ????????/// 解密字節(jié)數(shù)組 |
| 139 | ????????/// </summary> |
| 140 | ????????/// <param name="inputData">要解密的字節(jié)數(shù)據(jù)</param> |
| 141 | ????????/// <param name="password">密碼</param> |
| 142 | ????????/// <returns></returns> |
| 143 | ????????public static byte[] Decrypt(byte[] inputData, string password) |
| 145 | ????????????AesCryptoServiceProvider aes = new AesCryptoServiceProvider(); |
| 146 | ????????????aes.Key = GetKeyArray(password); |
| 147 | ????????????aes.Mode = _cipherMode; |
| 148 | ????????????aes.Padding = _paddingMode; |
| 149 | ????????????ICryptoTransform transform = aes.CreateDecryptor(); |
| 150 | ????????????byte[] data = null; |
| 153 | ????????????????data = transform.TransformFinalBlock(inputData, 0, inputData.Length); |
| 157 | ????????????????return null; |
| 159 | ????????????aes.Clear(); |
| 160 | ????????????return data; |
| 164 | ????????/// 解密16進(jìn)制的字符串為字符串 |
| 165 | ????????/// </summary> |
| 166 | ????????/// <param name="inputString">要解密的字符串</param> |
| 167 | ????????/// <param name="password">密碼</param> |
| 168 | ????????/// <returns>字符串</returns> |
| 169 | ????????public static string Decrypt(string inputString, string password) |
| 171 | ????????????byte[] toDecryptArray = ConvertStringToByte(inputString); |
| 172 | ????????????string decryptString = _encoding.GetString(Decrypt(toDecryptArray, password)); |
| 173 | ????????????return decryptString; |
| 177 | ????????/// 解密16進(jìn)制的字符串為字符串 |
| 178 | ????????/// </summary> |
| 179 | ????????/// <param name="inputString">需要解密的字符串</param> |
| 180 | ????????/// <returns>解密后的字符串</returns> |
| 181 | ????????public static string DecryptString(string inputString) |
| 183 | ????????????return Decrypt(inputString, _passwd); |
2.Android代碼:
view sourceprint? | 001 | package com.google.test; |
| 003 | import java.io.UnsupportedEncodingException; |
| 004 | import javax.crypto.Cipher; |
| 005 | import javax.crypto.spec.SecretKeySpec; |
| 008 | public class AESHelper { |
| 011 | ????private static final String CipherMode = "AES/ECB/PKCS5Padding"; |
| 013 | ????/** 創(chuàng)建密鑰 **/ |
| 014 | ????private static SecretKeySpec createKey(String password) { |
| 015 | ????????byte[] data = null; |
| 016 | ????????if (password == null) { |
| 017 | ????????????password = ""; |
| 019 | ????????StringBuffer sb = new StringBuffer(32); |
| 020 | ????????sb.append(password); |
| 021 | ????????while (sb.length() < 32) { |
| 022 | ????????????sb.append("0"); |
| 024 | ????????if (sb.length() > 32) { |
| 025 | ????????????sb.setLength(32); |
| 029 | ????????????data = sb.toString().getBytes("UTF-8"); |
| 030 | ????????} catch (UnsupportedEncodingException e) { |
| 031 | ????????????e.printStackTrace(); |
| 033 | ????????return new SecretKeySpec(data, "AES"); |
| 036 | ????/** 加密字節(jié)數(shù)據(jù) **/ |
| 037 | ????public static byte[] encrypt(byte[] content, String password) { |
| 039 | ????????????SecretKeySpec key = createKey(password); |
| 040 | ????????????Cipher cipher = Cipher.getInstance(CipherMode); |
| 041 | ????????????cipher.init(Cipher.ENCRYPT_MODE, key); |
| 042 | ????????????byte[] result = cipher.doFinal(content); |
| 043 | ????????????return result; |
| 044 | ????????} catch (Exception e) { |
| 045 | ????????????e.printStackTrace(); |
| 050 | ????/** 加密(結(jié)果為16進(jìn)制字符串) **/ |
| 051 | ????public static String encrypt(String content, String password) { |
| 052 | ????????byte[] data = null; |
| 054 | ????????????data = content.getBytes("UTF-8"); |
| 055 | ????????} catch (Exception e) { |
| 056 | ????????????e.printStackTrace(); |
| 058 | ????????data = encrypt(data, password); |
| 059 | ????????String result = byte2hex(data); |
| 060 | ????????return result; |
| 063 | ????/** 解密字節(jié)數(shù)組 **/ |
| 064 | ????public static byte[] decrypt(byte[] content, String password) { |
| 066 | ????????????SecretKeySpec key = createKey(password); |
| 067 | ????????????Cipher cipher = Cipher.getInstance(CipherMode); |
| 068 | ????????????cipher.init(Cipher.DECRYPT_MODE, key); |
| 069 | ????????????byte[] result = cipher.doFinal(content); |
| 070 | ????????????return result; |
| 071 | ????????} catch (Exception e) { |
| 072 | ????????????e.printStackTrace(); |
| 077 | ????/** 解密16進(jìn)制的字符串為字符串 **/ |
| 078 | ????public static String decrypt(String content, String password) { |
| 079 | ????????byte[] data = null; |
| 081 | ????????????data = hex2byte(content); |
| 082 | ????????} catch (Exception e) { |
| 083 | ????????????e.printStackTrace(); |
| 085 | ????????data = decrypt(data, password); |
| 086 | ????????if (data == null) |
| 087 | ????????????return null; |
| 088 | ????????String result = null; |
| 090 | ????????????result = new String(data, "UTF-8"); |
| 091 | ????????} catch (UnsupportedEncodingException e) { |
| 092 | ????????????e.printStackTrace(); |
| 094 | ????????return result; |
| 097 | ????/** 字節(jié)數(shù)組轉(zhuǎn)成16進(jìn)制字符串 **/ |
| 098 | ????public static String byte2hex(byte[] b) { // 一個字節(jié)的數(shù), |
| 099 | ????????StringBuffer sb = new StringBuffer(b.length * 2); |
| 100 | ????????String tmp = ""; |
| 101 | ????????for (int n = 0; n < b.length; n++) { |
| 102 | ????????????// 整數(shù)轉(zhuǎn)成十六進(jìn)制表示 |
| 103 | ????????????tmp = (java.lang.Integer.toHexString(b[n] & 0XFF)); |
| 104 | ????????????if (tmp.length() == 1) { |
| 105 | ????????????????sb.append("0"); |
| 107 | ????????????sb.append(tmp); |
| 109 | ????????return sb.toString().toUpperCase(); // 轉(zhuǎn)成大寫 |
| 112 | ????/** 將hex字符串轉(zhuǎn)換成字節(jié)數(shù)組 **/ |
| 113 | ????private static byte[] hex2byte(String inputString) { |
| 114 | ????????if (inputString == null || inputString.length() < 2) { |
| 115 | ????????????return new byte[0]; |
| 117 | ????????inputString = inputString.toLowerCase(); |
| 118 | ????????int l = inputString.length() / 2; |
| 119 | ????????byte[] result = new byte[l]; |
| 120 | ????????for (int i = 0; i < l; ++i) { |
| 121 | ????????????String tmp = inputString.substring(2 * i, 2 * i + 2); |
| 122 | ????????????result[i] = (byte) (Integer.parseInt(tmp, 16) & 0xFF); |
| 124 | ????????return result; |
轉(zhuǎn)載于:https://www.cnblogs.com/huanglong/archive/2011/06/27/2091328.html
總結(jié)
以上是生活随笔為你收集整理的Android和.NET通用的AES算法 (转) 好东东 收藏一下的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。