日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android和.NET通用的AES算法 (转) 好东东 收藏一下

發(fā)布時間:2025/3/15 Android 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android和.NET通用的AES算法 (转) 好东东 收藏一下 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

轉(zhuǎn)自:http://www.cnblogs.com/ahui/archive/2011/04/22/2025045.html

1.NET源代碼:

view sourceprint?
001using System;
002using System.Text;
003using System.Security.Cryptography;
004??
005namespace 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密鑰數(shù)據(jù)
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????????/// 將字符數(shù)組轉(zhuǎn)換成字符串
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????????/// 將字符串轉(zhuǎn)換成字符數(shù)組
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????????/// 加密字節(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)
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????????/// 解密字節(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)
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?
001package com.google.test;
002??
003import java.io.UnsupportedEncodingException;
004import javax.crypto.Cipher;
005import javax.crypto.spec.SecretKeySpec;
006??
007/** AES對稱加密解密類 **/
008public class AESHelper {
009??
010????/** 算法/模式/填充 **/
011????private static final String CipherMode = "AES/ECB/PKCS5Padding";
012??
013????/** 創(chuàng)建密鑰 **/
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????/** 加密字節(jié)數(shù)據(jù) **/
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????/** 加密(結(jié)果為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????/** 解密字節(jié)數(shù)組 **/
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????/** 字節(jié)數(shù)組轉(zhuǎn)成16進制字符串 **/
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)成十六進制表示
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(); // 轉(zhuǎn)成大寫
110????}
111??
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];
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}

轉(zhuǎn)載于:https://www.cnblogs.com/huanglong/archive/2011/06/27/2091328.html

總結(jié)

以上是生活随笔為你收集整理的Android和.NET通用的AES算法 (转) 好东东 收藏一下的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。