Orion's Studio.

CSharp加解密

2017/06/27

先是MD5加密

1
2
3
4
5
6
7
private static MD5 md5 = new MD5Cng();

public static string PasswordEncrypt(string aPassword)
{
byte[] bytes = md5.ComputeHash(Encoding.UTF8.GetBytes(aPassword+"doyoulikeme?"));
return Convert.ToBase64String(bytes);
}

加解密

加密

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public static string Encrypt(string aStr, string aAddition)
{
byte[] keyArray = Encoding.UTF8.GetBytes(aAddition);
JObject jobj = new JObject();
jobj["value"] = aStr;
jobj["time"] = DateTime.Now;
byte[] encryptArray = Encoding.UTF8.GetBytes(jobj.ToString());
RijndaelManaged rDel = new RijndaelManaged();
rDel.Key = keyArray;
rDel.Mode = CipherMode.ECB;
rDel.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = rDel.CreateEncryptor();
byte[] resultArray = cTransform.TransformFinalBlock(encryptArray, 0, encryptArray.Length);
string result = Convert.ToBase64String(resultArray);
return result;
}

解密

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public static string Decrypt(string aEncryptStr, string aAddition)
{
byte[] keyArray = Encoding.UTF8.GetBytes(aAddition);
byte[] toEncryptArray = Convert.FromBase64String(aEncryptStr);
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);
string result = Encoding.UTF8.GetString(resultArray);
JObject jobj = JObject.Parse(result);
return jobj["value"].ToString();
}
CATALOG
  1. 1. 先是MD5加密
  2. 2. 加解密
    1. 2.1. 加密
    2. 2.2. 解密