public static string Decrypt(string key, string value) { EncrypEntity entity = new EncrypEntity(); if (!string.IsNullOrEmpty(key)) { entity.Key = key; } entity.Value = value; return(Decrypt(entity)); }
/// <summary> /// 解密 /// </summary> /// <param name="txt"></param> /// <returns></returns> public static string Decrypt(EncrypEntity entity) { string txt = entity.Value; int len = txt.Length; byte[] bs = new byte[len / 2]; // 16进制数组转换会byte数组 for (int i = 0; i < len / 2; i++) { bs[i] = (byte)(Convert.ToInt32(txt.Substring(i * 2, 2), 16)); } byte[] keys = entity.Encoding.GetBytes(entity.Key); // 密钥转换成字节数组 // 异或 for (int i = 0; i < bs.Length; i++) { bs[i] = (byte)(bs[i] ^ keys[i % keys.Length]); } // byte数组还原成字符串 return(entity.Encoding.GetString(bs)); }
/// <summary> /// 加密 /// </summary> /// <param name="txt"></param> /// <returns></returns> public static string Encrypt(EncrypEntity entity) { StringBuilder sb = new StringBuilder(); string encryptKey = entity.Key; Encoding encoding = entity.Encoding; byte[] bs = encoding.GetBytes(entity.Value); // 原字符串转换成字节数组 byte[] keys = encoding.GetBytes(encryptKey); // 密钥转换成字节数组 // 异或 for (int i = 0; i < bs.Length; i++) { bs[i] = (byte)(bs[i] ^ keys[i % keys.Length]); } // 编码成16进制数组 foreach (byte b in bs) { sb.AppendFormat("{0:X2}", b); } return(sb.ToString()); }
public static string Encrypt(string value) { EncrypEntity entity = new EncrypEntity(value); return(Encrypt(entity)); }