C# Andriod AES 加密算法
android端:
package com.kingmed.http;
import java.io.UnsupportedEncodingException;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
public class AESHelper {
private static final String CipherMode =
"AES/ECB/PKCS5Padding";
private static SecretKeySpec createKey(String password) {
byte[] data = null;
if (password == null) {
password = "";
}
StringBuffer sb = new StringBuffer(32);
sb.append(password);
while (sb.length() < 32) {
sb.append("0");
}
if (sb.length() > 32) {
sb.setLength(32);
}
try {
data = sb.toString().getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return new SecretKeySpec(data, "AES");
}
public
static byte[] encrypt(byte[] content, String password) {
try {
SecretKeySpec key = createKey(password);
Cipher cipher = Cipher.getInstance(CipherMode);
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] result = cipher.doFinal(content);
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public
static String encrypt(String content, String password) {
byte[] data = null;
try {
data = content.getBytes("UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
data = encrypt(data, password);
String result = byte2hex(data);
return result;
}
public
static byte[] decrypt(byte[] content, String password) {
try {
SecretKeySpec key = createKey(password);
Cipher cipher = Cipher.getInstance(CipherMode);
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] result = cipher.doFinal(content);
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public
static String decrypt(String content, String password) {
byte[] data = null;
try {
data = hex2byte(content);
} catch (Exception e) {
e.printStackTrace();
}
data = decrypt(data, password);
if (data == null)
return null;
String result = null;
try {
result = new String(data, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return result;
}
public
static String byte2hex(byte[] b) { // 一个字节的数,
StringBuffer sb = new StringBuffer(b.length * 2);
String tmp = "";
for (int n = 0; n < b.length; n++) {
// 整数转成十六进制表示
tmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
if (tmp.length() == 1) {
sb.append("0");
}
sb.append(tmp);
}
return sb.toString().toUpperCase(); // 转成大写
}
private
static byte[] hex2byte(String inputString) {
if (inputString == null || inputString.length() < 2) {
return new byte[0];
}
inputString = inputString.toLowerCase();
int l = inputString.length() / 2;
byte[] result = new byte[l];
for (int i = 0; i < l; ++i) {
String tmp = inputString.substring(2 * i, 2 * i + 2);
result[i] = (byte) (Integer.parseInt(tmp, 16) & 0xFF);
}
return result;
}
}
.NET端:
using System;
using System.Text;
using System.Security.Cryptography;
///
///AESHelper 的摘要说明
///
///
/// AES对称加密解密类
///
public class AESHelper
{
#region
成员变量
///
///
密钥(32位,不足在后面补0)
///
private
const string _passwd = "ihlih*0037JOHT*)(PIJY*(()JI^)IO%";
///
///
运算模式
///
private
static CipherMode _cipherMode = CipherMode.ECB;
///
///
填充模式
///
private
static PaddingMode _paddingMode = PaddingMode.PKCS7;
///
///
字符串采用的编码
///
private
static Encoding _encoding = Encoding.UTF8;
#endregion
#region
辅助方法
///
///
获取32byte密钥数据
///
/// 密码
///
private
static byte[] GetKeyArray(string password)
{
if (password == null)
{
password = string.Empty;
}
if (password.Length < 32)
{
password = password.PadRight(32, '0');
}
else if (password.Length > 32)
{
password = password.Substring(0, 32);
}
return _encoding.GetBytes(password);
}
///
///
将字符数组转换成字符串
///
///
///
private
static string ConvertByteToString(byte[] inputData)
{
StringBuilder sb = new StringBuilder(inputData.Length * 2);
foreach (var b in inputData)
{
sb.Append(b.ToString("X2"));
}
return sb.ToString();
}
///
///
将字符串转换成字符数组
///
///
///
private
static byte[] ConvertStringToByte(string inputString)
{
if (inputString == null || inputString.Length < 2)
{
throw new ArgumentException();
}
int l = inputString.Length / 2;
byte[] result = new byte[l];
for (int i = 0; i < l; ++i)
{
result[i] = Convert.ToByte(inputString.Substring(2 * i, 2),
16);
}
return result;
}
#endregion
#region
加密
///
///
加密字节数据
///
///
要加密的字节数据
/// 密码
///
public
static byte[] Encrypt(byte[] inputData, string password)
{
AesCryptoServiceProvider aes = new
AesCryptoServiceProvider();
aes.Key = GetKeyArray(password);
aes.Mode = _cipherMode;
aes.Padding = _paddingMode;
ICryptoTransform transform = aes.CreateEncryptor();
byte[] data = transform.TransformFinalBlock(inputData, 0,
inputData.Length);
aes.Clear();
return data;
}
///
///
加密字符串(加密为16进制字符串)
///
///
要加密的字符串
/// 密码
///
public
static string Encrypt(string inputString, string password)
{
byte[] toEncryptArray = _encoding.GetBytes(inputString);
byte[] result = Encrypt(toEncryptArray, password);
return ConvertByteToString(result);
}
///
///
字符串加密(加密为16进制字符串)
///
///
需要加密的字符串
///
加密后的字符串
public
static string EncryptString(string inputString)
{
return Encrypt(inputString, _passwd);
}
#endregion
#region
解密
///
///
解密字节数组
///
///
要解密的字节数据
/// 密码
///
public
static byte[] Decrypt(byte[] inputData, string password)
{
AesCryptoServiceProvider aes = new
AesCryptoServiceProvider();
aes.Key = GetKeyArray(password);
aes.Mode = _cipherMode;
aes.Padding = _paddingMode;
ICryptoTransform transform = aes.CreateDecryptor();
byte[] data = null;
try
{
data = transform.TransformFinalBlock(inputData, 0,
inputData.Length);
}
catch
{
return null;
}
aes.Clear();
return data;
}
///
///
解密16进制的字符串为字符串
///
///
要解密的字符串
/// 密码
///
字符串
public
static string Decrypt(string inputString, string password)
{
byte[] toDecryptArray = ConvertStringToByte(inputString);
string decryptString = _encoding.GetString(Decrypt(toDecryptArray,
password));
return decryptString;
}
///
///
解密16进制的字符串为字符串
///
///
需要解密的字符串
///
解密后的字符串
public
static string DecryptString(string inputString)
{
return Decrypt(inputString, _passwd);
}
#endregion
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
C# Andriod AES 加密算法的更多相关文章
- AES对称加密算法原理
原著:James McCaffrey 翻译:小刀人 原文出处:MSDN Magazine November 2003 (Encrypt It) 本文的代码下载:msdnmag200311AES.exe ...
- AES对称加密算法原理(转载)
出处:http://www.2cto.com/Article/201112/113465.html 原著:James McCaffrey 翻译:小刀人 原文出处:MSDN Magazine Novem ...
- Java 加密 AES 对称加密算法
版权声明:本文为博主原创文章,未经博主允许不得转载. [AES] 一种对称加密算法,DES的取代者. 加密相关文章见:Java 加密解密 对称加密算法 非对称加密算法 MD5 BASE64 AES R ...
- DES、RC4、AES等加密算法优势及应用
[IT168 技术]1篇文章,1部小说被盗取,全靠维(si)权(bi)捍卫自己的原创权利.程序员捍卫自己珍贵的代码,全靠花式的加密算法.代码加密有多重要?程序员半年做出的产品,盗版者可能半天就能完全破 ...
- 使用openssl的aes各种加密算法
#include <stdio.h> #include <string.h> #include <sys/types.h> #include <sys/sta ...
- AES对称加密算法实现:Java,C#,Golang,Python
高级加密标准(Advanced Encryption Standard,简写AES),是一种用来替代DES的对称加密算法,相比DES,AES安全性更高,加密速度更快,因此被广泛使用. 理论上看,AES ...
- AES对称加密算法
package cn.jsonlu.passguard.utils; import org.apache.commons.codec.binary.Base64; import javax.crypt ...
- AES前后加密算法代码
首先下载aes.js加密工具类: 本文采用的是 AES/ECB/PKCS5Padding的加密方式进行加密的: js加密写法如下: <!DOCTYPE html> <html lan ...
- Java实现AES对称加密算法
Java代码实现 import java.security.SecureRandom; import javax.crypto.Cipher; import javax.crypto.KeyGener ...
随机推荐
- urllib库python2和python3具体区别
Python 2 name Python 3 name urllib.urlretrieve() urllib.request.urlretrieve() urllib.urlcleanup() ...
- 如果数据需要被多个应用程序消费的话,推荐使用 Kafka,如果数据只是面向 Hadoop 的,可以使用 Flume
https://www.ibm.com/developerworks/cn/opensource/os-cn-kafka/index.html Kafka 与 Flume 很多功能确实是重复的.以下是 ...
- Smarty入门学习
--------------------------------- 安装和设置 --------------------------------- require('../Smarty/Smarty. ...
- Hadoop实战-Flume之Sink Failover(十六)
a1.sources = r1 a1.sinks = k1 k2 a1.channels = c1 # Describe/configure the source a1.sources.r1.type ...
- python cookbook第三版学习笔记九:函数
接受任意数量参数的函数. 当传入函数的参数个数很多的时候,在函数定义的时候不需要为每一个参数定义一个变量,可以用*rest的方式来包含多余的参数. 如下面的代码,*rest包含了2,3,4这3个参数. ...
- jq实现批量图片上传
http://blog.csdn.net/lmj623565791/article/details/31513065 jq实现批量图片上传 http://blog.csdn.net/lmj623565 ...
- Java for LeetCode 122 Best Time to Buy and Sell Stock II
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- PAT 甲级 1065. A+B and C (64bit) (20) 【大数加法】
题目链接 https://www.patest.cn/contests/pat-a-practise/1065 思路 因为 a 和 b 都是 在 long long 范围内的 但是 a + b 可能会 ...
- Android NDK环境搭建
本文主要记录NDK环境在Ubuntu下的搭建. 下载NDK 在官网进行下载NDK https://developer.android.com/ndk/downloads/index.html 当前最新 ...
- BA优化PnP的思路
由之前的PnP,可以求出一个R,t,K又是已知的.而且空间点的世界坐标知道,第二个相机位姿的像素坐标也是知道的.就可以利用它们进行优化.首先确定变量为const vector<Point3f&g ...