C#AES对称加密,不加盐 对比(Java)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.IO;
using System.Net;
using System.Collections;
using System.ComponentModel;
using System.Web;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
using System.Xml.Linq;
using System.Configuration;
namespace ShareAll
{
/// <summary>
/// 本函数使用AES方法提供了两对函数,一对 ENcrypt 和 Decrypt 不使用私钥。
/// 另一对 AesENcrypt 和 AesDecrypt 使用私钥。
/// </summary>
public class Share
{
public static void SetCertificatePolicy()
{
ServicePointManager.ServerCertificateValidationCallback += RemoteCertificateValidate;
}
private static bool RemoteCertificateValidate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors error)
{
// trust any certificate!!!
System.Console.WriteLine("Warning, trust any certificate");
return true;
}
/// <summary>
/// 用种子获取密钥字节
/// </summary>
/// <param name="strKey">密钥种子</param>
/// <param name="encoding">编码格式</param>
/// <param name="nLen">密钥长度(一般为16,不清楚时不要随意动)</param>
/// <returns></returns>
public static byte[] GetKeyBySeed(string strKey, Encoding encoding, int nLen = 16)
{
byte[] bySeed = encoding.GetBytes(strKey);
byte[] byKeyArray = null;
using (var st = new SHA1CryptoServiceProvider())
{
using (var nd = new SHA1CryptoServiceProvider())
{
var rd = nd.ComputeHash(st.ComputeHash(bySeed));
byKeyArray = rd.Take(nLen).ToArray();
}
}
return byKeyArray;
}
/// <summary>
/// 加密 参数:string
/// </summary>
/// <param name="strCon">加密内容</param>
/// <param name="byteKey">密钥字节数组</param>
/// <param name="strIv">向量(注意目前只研究支持16位长度)</param>
/// <param name="encoding">编码方式</param>
/// <returns>string:密文</returns>
public static string Encrypt(string strCon, string sKey, Encoding encoding)
{
try
{
byte[] byteKey = GetKeyBySeed(sKey,Encoding.UTF8);
if (string.IsNullOrWhiteSpace(strCon))
{
return null;
}
byte[] byCon = encoding.GetBytes(strCon);
var rm = new RijndaelManaged
{
Key = byteKey,
Mode = CipherMode.ECB,
Padding = PaddingMode.PKCS7
};
ICryptoTransform cTransform = rm.CreateEncryptor();
byte[] resultArray = cTransform.TransformFinalBlock(byCon, 0, byCon.Length);
return ByteArrayToHexString(resultArray);
}
catch
{
return "";
}
}
public static string Decrypt(string strCon, string sKey, Encoding encoding)
{
try
{
byte[] byteKey = GetKeyBySeed(sKey, Encoding.UTF8);
if (string.IsNullOrWhiteSpace(strCon))
{
return null;
}
byte[] byCon = HexStrToByte(strCon);
var rm = new RijndaelManaged
{
Key = byteKey,
Mode = CipherMode.ECB,
Padding = PaddingMode.PKCS7
};
ICryptoTransform cTransform = rm.CreateDecryptor();
byte[] resultArray = cTransform.TransformFinalBlock(byCon, 0, byCon.Length);
return encoding.GetString(resultArray);
}
catch (Exception ex)
{
return "";
}
}
/// <summary>
///
/// </summary>
/// <param name="hexString"></param>
/// <returns></returns>
public static byte[] HexStrToByte(string hexString)
{
hexString = hexString.Replace(" ", "");
if ((hexString.Length % 2) != 0)
hexString += " ";
byte[] returnBytes = new byte[hexString.Length / 2];
for (int i = 0; i < returnBytes.Length; i++)
returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
return returnBytes;
}
/// <summary>
/// 转化为16进制
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static string ByteArrayToHexString(byte[] data)
{
StringBuilder sb = new StringBuilder(data.Length * 3);
foreach (byte b in data)
{
sb.Append(Convert.ToString(b, 16).PadLeft(2, '0'));
}
return sb.ToString().ToUpper();
}
public static string MD5(string Text)
{
byte[] buffer = Encoding.Default.GetBytes(Text);
try
{
MD5CryptoServiceProvider check;
check = new MD5CryptoServiceProvider();
byte[] somme = check.ComputeHash(buffer);
string ret = "";
foreach (byte a in somme)
{
if (a < 16)
ret += "0" + a.ToString("X");
else
ret += a.ToString("X");
}
return ret.ToLower();
}
catch
{
throw;
}
}
public static void WriteLogs(string fileName, string type, string content)
{
string path = AppDomain.CurrentDomain.BaseDirectory;
if (!string.IsNullOrEmpty(path))
{
path = AppDomain.CurrentDomain.BaseDirectory + fileName;
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
//path = path + "\\" + DateTime.Now.ToString("yyyyMMdd");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
path = path +"\\"+ DateTime.Now.ToString("yyyyMMdd")+ ".txt";
if (!File.Exists(path))
{
FileStream fs = File.Create(path);
fs.Close();
}
if (File.Exists(path))
{
StreamWriter sw = new StreamWriter(path, true, System.Text.Encoding.Default);
sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + " >>> " + type + " >>> " + content);
sw.WriteLine("===================================================================================");
sw.Close();
}
}
}
}
}