android开发 java与c# 兼容AES加密
生活随笔
收集整理的這篇文章主要介紹了
android开发 java与c# 兼容AES加密
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
由于android客戶端采用的是AES加密,服務(wù)器用的是asp.net(c#),所以就造成了不一致的加密與解密問題,下面就貼出代碼,已經(jīng)試驗過。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Security.Cryptography; using System.IO;namespace BLL {public class AESHelper{/// <summary>/// 有密碼的AES加密 /// </summary>/// <param name="text">加密字符</param>/// <param name="password">加密的密碼</param>/// <param name="iv">密鑰</param>/// <returns></returns>public static string Encrypt(string toEncrypt, string key){byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);RijndaelManaged rDel = new RijndaelManaged();rDel.Key = keyArray;rDel.Mode = CipherMode.ECB;rDel.Padding = PaddingMode.PKCS7;ICryptoTransform cTransform = rDel.CreateEncryptor();byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);return Convert.ToBase64String(resultArray, 0, resultArray.Length);}/// <summary>/// AES解密/// </summary>/// <param name="text"></param>/// <param name="password"></param>/// <param name="iv"></param>/// <returns></returns>public static string Decrypt(string toDecrypt, string key){byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);RijndaelManaged rDel = new RijndaelManaged();rDel.Key = keyArray;rDel.Mode = CipherMode.ECB;rDel.Padding = PaddingMode.PKCS7;ICryptoTransform cTransform = rDel.CreateDecryptor();byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);return UTF8Encoding.UTF8.GetString(resultArray);}}}?
java
import android.util.Base64; import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; /*** AES加密器* @author Eric_Ni**/ public class AESEncryptor {// 加密public static String Encrypt(String sSrc, String sKey) throws Exception {if (sKey == null) {System.out.print("Key為空null");return null;}// 判斷Key是否為16位if (sKey.length() != 16) {System.out.print("Key長度不是16位");return null;}byte[] raw = sKey.getBytes("utf-8");SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");//"算法/模式/補碼方式" cipher.init(Cipher.ENCRYPT_MODE, skeySpec);byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));return Base64.encodeToString(encrypted,Base64.DEFAULT);//.encodeToString(encrypted,0);//此處使用BASE64做轉(zhuǎn)碼功能,同時能起到2次加密的作用。 }// 解密public static String Decrypt(String sSrc, String sKey) throws Exception {try {// 判斷Key是否正確if (sKey == null) {System.out.print("Key為空null");return null;}// 判斷Key是否為16位if (sKey.length() != 16) {System.out.print("Key長度不是16位");return null;}byte[] raw = sKey.getBytes("utf-8");SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");cipher.init(Cipher.DECRYPT_MODE, skeySpec);byte[] encrypted1 = Base64.decode(sSrc,Base64.DEFAULT);//先用base64解密try {byte[] original = cipher.doFinal(encrypted1);String originalString = new String(original,"utf-8");return originalString;} catch (Exception e) {System.out.println(e.toString());return null;}} catch (Exception ex) {System.out.println(ex.toString());return null;}}}?
我開始在網(wǎng)上看到別人用BASE64,引用的是:import org.apache.commons.codec.binary.Base64;
但是我下載過,又報錯Base64().encodeToString? nosuchMethod? 煩躁...??
后面改為引用import android.util.Base64;一樣可以用,注意 android端采用了靜態(tài)的方法,不需要new
?
測試數(shù)據(jù) android客戶端 待加密字符:13 經(jīng)加密后為:6U64XSoSkheXXhyQ3vxC3Q==
服務(wù)器端: 收到字符串:6U64XSoSkheXXhyQ3vxC3Q== 解密后:13
?
參考文檔:http://www.oschina.net/code/snippet_242957_9931?
總結(jié)
以上是生活随笔為你收集整理的android开发 java与c# 兼容AES加密的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java递归思想
- 下一篇: cocos2dx 3.3 Directo