C# 加密签名代码示例
using System;
using System.Security.Cryptography;
using System.Text;
namespace ConsoleApplication1 {
class Program {
static void Main(string[] args) {
string source1 = "{\"orderNo\":\"orderNo\", \"hisBillNo\":\"hisBillNo\", \"amount\": 0}";
string p = "31817498260286befc5a57a7edb7dff46d983354";
string eee = encode(source1, p);
Console.WriteLine("密钥: " + p);
Console.WriteLine("密文: " + eee);
Console.WriteLine("明文: " + decode(eee, p));
string encoded = "beddeb7e4d37443337e27a9890be230c19083b32707d527a9ce47afb88b398f57b0d0a202022696422203a2032303133312c0d0a202022736e22203a20223234303738393034222c0d0a202022637573746f6d4e6f22203a2022343132333236313939343130303332373530222c0d0a202022627573696e6573735479706522203a20223137222c0d0a202022627573696e6573734465736322203a2022e997a8e8af8ae98080e8b4b9222c0d0a202022627573696e657373526573756c7422203a202231222c0d0a202022627573696e657373526573756c744465736322203a202248495320e98080e8b4b9e68890e58a9f222c0d0a202022627573696e657373436f6e74656e7422203a20227b5c226172656149645c223a5c2230315c222c5c22686f73706974616c49645c223a5c22315c222c5c226f70657261746f724e6f5c223a5c223839303030382f3839303030385c222c5c226163636f756e7442616c616e63655c223a312e30307d222c0d0a202022686973427573696e6573734e6f22203a20223234303738393034222c0d0a202022706179657222203a202230303036323839313535222c0d0a20202263757272656e637922203a2022e4babae6b091e5b881222c0d0a202022616d6f756e7422203a20312e30302c0d0a202022626172636f646522203a206e756c6c2c0d0a2020227061795479706522203a202236222c0d0a2020227061794e616d6522203a2022343132333236313939343130303332373530222c0d0a2020227061794368616e6e656c22203a20223033222c0d0a20202270617954696d6522203a2022323032312d30332d31312031303a30393a3530222c0d0a2020226973526566756e6422203a202231222c0d0a202022726566756e64416d6f756e7422203a20312e30302c0d0a20202272656c61746564536e22203a2022363135343236313332353431313932313631222c0d0a202022726566756e6454696d6522203a2022323032312d30332d31312031303a30393a3530222c0d0a2020226578706972656454696d6522203a2022323032312d30332d31312031303a31343a3530222c0d0a202022706179526573756c7422203a20223032222c0d0a202022706179427573696e6573734e6f22203a2022222c0d0a2020227465726d53657269616c4e6f22203a206e756c6c2c0d0a20202263726561746554696d6522203a2022323032312d30332d31312031303a30393a3530222c0d0a2020226f70657261746f724e6f22203a20223839303030382f383930303038222c0d0a202022686f73706974616c496422203a202231222c0d0a20202261726561496422203a20223031222c0d0a202022726566756e6461626c65416d6f756e7422203a20302e30300d0a7d";
string decoded = decode(encoded, p);
Console.WriteLine("密钥: " + p);
Console.WriteLine("密文: " + encoded);
Console.WriteLine("明文: " + decoded);
Console.WriteLine(encode(decoded, p));
Console.ReadKey();
}
public static string md5(string str_md5_in) {
MD5 md5 = new MD5CryptoServiceProvider();
byte[] bytes_md5_in = new UTF8Encoding().GetBytes(str_md5_in);
byte[] bytes_md5_out = md5.ComputeHash(bytes_md5_in);
return encodeHex(bytes_md5_out);
}
//SHA1
public static string sha1(string str_sha1_in) {
SHA1 sha1 = new SHA1CryptoServiceProvider();
byte[] bytes_sha1_in = new UTF8Encoding().GetBytes(str_sha1_in);
byte[] bytes_sha1_out = sha1.ComputeHash(bytes_sha1_in);
return encodeHex(bytes_sha1_out);
}
public static string encodeHex(byte[] bytes) {
string s = BitConverter.ToString(bytes);
s = s.Replace("-", "").ToLower();
return s;
}
public static byte[] decodeHex(string hex) {
byte[] bytes = new byte[hex.Length / 2];
for (int i = 0; i < bytes.Length; i++) {
try {
bytes[i] = byte.Parse(hex.Substring(i * 2, 2),
System.Globalization.NumberStyles.HexNumber);
}
catch {
throw new Exception("不是有效的hex");
}
}
return bytes;
}
/**
* 解码
*
* @param source
* @param key
* @return
*/
public static string decode(string source, string key) {
string sign = source.Substring(32, 32);
string body = source.Substring(64);
string sign_ = md5(body + key);
if (sign != sign_) {
throw new Exception("验签失败");
}
byte[] bytes = decodeHex(body);
body = new UTF8Encoding().GetString(bytes);
return body;
}
/**
* 编码
*
* @param source
* @param key
* @return
*/
public static string encode(string source, string key) {
DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
long timeStamp = (long)(DateTime.Now - startTime).TotalMilliseconds;
try {
string mixture = md5(timeStamp + "");
string body = encodeHex(new UTF8Encoding().GetBytes(source));
string sign = md5(body + key);
return mixture + sign + body;
}
catch (Exception e) {
throw new Exception("编码失败");
}
}
}
}