Android和.NET通用的AES算法 好东东 收藏一下
16lz
2021-01-24
转自:http://www.cnblogs.com/ahui/archive/2011/04/22/2025045.html
1.NET源代码:
view sourceprint?001 | using System; |
002 | using System.Text; |
003 | using System.Security.Cryptography; |
004 |
|
005 | namespace ConsoleApplicationDemo |
006 | { |
007 |
/// <summary> |
008 |
/// AES对称加密解密类 |
009 |
/// </summary> |
010 |
public class AESHelper |
011 |
{ |
012 |
#region 成员变量 |
013 |
/// <summary> |
014 |
/// 密钥(32位,不足在后面补0) |
015 |
/// </summary> |
016 |
private const string _passwd = "ihlih*0037JOHT*)(PIJY*(()JI^)IO%" ; |
017 |
/// <summary> |
018 |
/// 运算模式 |
019 |
/// </summary> |
020 |
private static CipherMode _cipherMode = CipherMode.ECB; |
021 |
/// <summary> |
022 |
/// 填充模式 |
023 |
/// </summary> |
024 |
private static PaddingMode _paddingMode = PaddingMode.PKCS7; |
025 |
/// <summary> |
026 |
/// 字符串采用的编码 |
027 |
/// </summary> |
028 |
private static Encoding _encoding = Encoding.UTF8; |
029 |
#endregion |
030 |
|
031 |
#region 辅助方法 |
032 |
/// <summary> |
033 |
/// 获取32byte密钥数据 |
034 |
/// </summary> |
035 |
/// <param name="password">密码</param> |
036 |
/// <returns></returns> |
037 |
private static byte [] GetKeyArray( string password) |
038 |
{ |
039 |
if (password == null ) |
040 |
{ |
041 |
password = string .Empty; |
042 |
} |
043 |
|
044 |
if (password.Length < 32) |
045 |
{ |
046 |
password = password.PadRight(32, '0' ); |
047 |
} |
048 |
else if (password.Length > 32) |
049 |
{ |
050 |
password = password.Substring(0, 32); |
051 |
} |
052 |
|
053 |
return _encoding.GetBytes(password); |
054 |
} |
055 |
|
056 |
/// <summary> |
057 |
/// 将字符数组转换成字符串 |
058 |
/// </summary> |
059 |
/// <param name="inputData"></param> |
060 |
/// <returns></returns> |
061 |
private static string ConvertByteToString( byte [] inputData) |
062 |
{ |
063 |
StringBuilder sb = new StringBuilder(inputData.Length * 2); |
064 |
foreach (var b in inputData) |
065 |
{ |
066 |
sb.Append(b.ToString( "X2" )); |
067 |
} |
068 |
return sb.ToString(); |
069 |
} |
070 |
|
071 |
/// <summary> |
072 |
/// 将字符串转换成字符数组 |
073 |
/// </summary> |
074 |
/// <param name="inputString"></param> |
075 |
/// <returns></returns> |
076 |
private static byte [] ConvertStringToByte( string inputString) |
077 |
{ |
078 |
if (inputString == null || inputString.Length < 2) |
079 |
{ |
080 |
throw new ArgumentException(); |
081 |
} |
082 |
int l = inputString.Length / 2; |
083 |
byte [] result = new byte [l]; |
084 |
for ( int i = 0; i < l; ++i) |
085 |
{ |
086 |
result[i] = Convert.ToByte(inputString.Substring(2 * i, 2), 16); |
087 |
} |
088 |
|
089 |
return result; |
090 |
} |
091 |
#endregion |
092 |
|
093 |
#region 加密 |
094 |
/// <summary> |
095 |
/// 加密字节数据 |
096 |
/// </summary> |
097 |
/// <param name="inputData">要加密的字节数据</param> |
098 |
/// <param name="password">密码</param> |
099 |
/// <returns></returns> |
100 |
public static byte [] Encrypt( byte [] inputData, string password) |
101 |
{ |
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; |
110 |
} |
111 |
|
112 |
/// <summary> |
113 |
/// 加密字符串(加密为16进制字符串) |
114 |
/// </summary> |
115 |
/// <param name="inputString">要加密的字符串</param> |
116 |
/// <param name="password">密码</param> |
117 |
/// <returns></returns> |
118 |
public static string Encrypt( string inputString, string password) |
119 |
{ |
120 |
byte [] toEncryptArray = _encoding.GetBytes(inputString); |
121 |
byte [] result = Encrypt(toEncryptArray, password); |
122 |
return ConvertByteToString(result); |
123 |
} |
124 |
|
125 |
/// <summary> |
126 |
/// 字符串加密(加密为16进制字符串) |
127 |
/// </summary> |
128 |
/// <param name="inputString">需要加密的字符串</param> |
129 |
/// <returns>加密后的字符串</returns> |
130 |
public static string EncryptString( string inputString) |
131 |
{ |
132 |
return Encrypt(inputString, _passwd); |
133 |
} |
134 |
#endregion |
135 |
|
136 |
#region 解密 |
137 |
/// <summary> |
138 |
/// 解密字节数组 |
139 |
/// </summary> |
140 |
/// <param name="inputData">要解密的字节数据</param> |
141 |
/// <param name="password">密码</param> |
142 |
/// <returns></returns> |
143 |
public static byte [] Decrypt( byte [] inputData, string password) |
144 |
{ |
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 ; |
151 |
try |
152 |
{ |
153 |
data = transform.TransformFinalBlock(inputData, 0, inputData.Length); |
154 |
} |
155 |
catch |
156 |
{ |
157 |
return null ; |
158 |
} |
159 |
aes.Clear(); |
160 |
return data; |
161 |
} |
162 |
|
163 |
/// <summary> |
164 |
/// 解密16进制的字符串为字符串 |
165 |
/// </summary> |
166 |
/// <param name="inputString">要解密的字符串</param> |
167 |
/// <param name="password">密码</param> |
168 |
/// <returns>字符串</returns> |
169 |
public static string Decrypt( string inputString, string password) |
170 |
{ |
171 |
byte [] toDecryptArray = ConvertStringToByte(inputString); |
172 |
string decryptString = _encoding.GetString(Decrypt(toDecryptArray, password)); |
173 |
return decryptString; |
174 |
} |
175 |
|
176 |
/// <summary> |
177 |
/// 解密16进制的字符串为字符串 |
178 |
/// </summary> |
179 |
/// <param name="inputString">需要解密的字符串</param> |
180 |
/// <returns>解密后的字符串</returns> |
181 |
public static string DecryptString( string inputString) |
182 |
{ |
183 |
return Decrypt(inputString, _passwd); |
184 |
} |
185 |
#endregion |
186 |
} |
187 | } |
2.Android代码:
view sourceprint?001 | package com.google.test; |
002 |
|
003 | import java.io.UnsupportedEncodingException; |
004 | import javax.crypto.Cipher; |
005 | import javax.crypto.spec.SecretKeySpec; |
006 |
|
007 | /** AES对称加密解密类 **/ |
008 | public class AESHelper { |
009 |
|
010 |
/** 算法/模式/填充 **/ |
011 |
private static final String CipherMode = "AES/ECB/PKCS5Padding" ; |
012 |
|
013 |
/** 创建密钥 **/ |
014 |
private static SecretKeySpec createKey(String password) { |
015 |
byte [] data = null ; |
016 |
if (password == null ) { |
017 |
password = "" ; |
018 |
} |
019 |
StringBuffer sb = new StringBuffer( 32 ); |
020 |
sb.append(password); |
021 |
while (sb.length() < 32 ) { |
022 |
sb.append( "0" ); |
023 |
} |
024 |
if (sb.length() > 32 ) { |
025 |
sb.setLength( 32 ); |
026 |
} |
027 |
|
028 |
try { |
029 |
data = sb.toString().getBytes( "UTF-8" ); |
030 |
} catch (UnsupportedEncodingException e) { |
031 |
e.printStackTrace(); |
032 |
} |
033 |
return new SecretKeySpec(data, "AES" ); |
034 |
} |
035 |
|
036 |
/** 加密字节数据 **/ |
037 |
public static byte [] encrypt( byte [] content, String password) { |
038 |
try { |
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(); |
046 |
} |
047 |
return null ; |
048 |
} |
049 |
|
050 |
/** 加密(结果为16进制字符串) **/ |
051 |
public static String encrypt(String content, String password) { |
052 |
byte [] data = null ; |
053 |
try { |
054 |
data = content.getBytes( "UTF-8" ); |
055 |
} catch (Exception e) { |
056 |
e.printStackTrace(); |
057 |
} |
058 |
data = encrypt(data, password); |
059 |
String result = byte2hex(data); |
060 |
return result; |
061 |
} |
062 |
|
063 |
/** 解密字节数组 **/ |
064 |
public static byte [] decrypt( byte [] content, String password) { |
065 |
try { |
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(); |
073 |
} |
074 |
return null ; |
075 |
} |
076 |
|
077 |
/** 解密16进制的字符串为字符串 **/ |
078 |
public static String decrypt(String content, String password) { |
079 |
byte [] data = null ; |
080 |
try { |
081 |
data = hex2byte(content); |
082 |
} catch (Exception e) { |
083 |
e.printStackTrace(); |
084 |
} |
085 |
data = decrypt(data, password); |
086 |
if (data == null ) |
087 |
return null ; |
088 |
String result = null ; |
089 |
try { |
090 |
result = new String(data, "UTF-8" ); |
091 |
} catch (UnsupportedEncodingException e) { |
092 |
e.printStackTrace(); |
093 |
} |
094 |
return result; |
095 |
} |
096 |
|
097 |
/** 字节数组转成16进制字符串 **/ |
098 |
public static String byte2hex( byte [] b) { // 一个字节的数, |
099 |
StringBuffer sb = new StringBuffer(b.length * 2 ); |
100 |
String tmp = "" ; |
101 |
for ( int n = 0 ; n < b.length; n++) { |
102 |
// 整数转成十六进制表示 |
103 |
tmp = (java.lang.Integer.toHexString(b[n] & 0XFF )); |
104 |
if (tmp.length() == 1 ) { |
105 |
sb.append( "0" ); |
106 |
} |
107 |
sb.append(tmp); |
108 |
} |
109 |
return sb.toString().toUpperCase(); // 转成大写 |
110 |
} |
111 |
|
112 |
/** 将hex字符串转换成字节数组 **/ |
113 |
private static byte [] hex2byte(String inputString) { |
114 |
if (inputString == null || inputString.length() < 2 ) { |
115 |
return new byte [ 0 ]; |
116 |
} |
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 ); |
123 |
} |
124 |
return result; |
125 |
} |
126 | } |
更多相关文章
- Android(安卓)jni 编程(参数的传递,成员,方法的)相互访问
- android提取字符串中为字母的字符
- Android之获得内存剩余大小与总大小
- android从byte[]字节数组中播放mp3音乐
- Android Studio 批量打包(渠道包,最新基于Gradle 2.2.1、Android
- 彻底理解Android中的ThreadLocal
- PC客户端与Android服务端的Socket同步通信(USB)
- 9102年了,还不知道Android为什么卡?
- Android中Http传递参数json参数请求数据