/// <summary> /// 解密 /// </summary> /// <param name="str">密串</param> /// <returns></returns> public string Decrypt(string str) { var parseKey = string.Empty; var ciphertext = GetParse(str, out parseKey); if (parseKey.IsNullOrWhiteSpace() || ciphertext.IsNullOrWhiteSpace()) { return(string.Empty); } ciphertext = ciphertext.Replace("_", "+").Replace("-", "/").Replace("*", "="); //随机6位密码+附加码 var aesKey = "{0}{1}".FormatStr(parseKey, _KeyAppendCode).MD5().ToLower(); //解密操作 var plaintext = AESEncrypt.AesDecrypt(ciphertext, secretKey: aesKey, vectorStr: _AESVector); return(plaintext); }
/// <summary> /// 加密 /// </summary> /// <param name="plaintext">明文</param> /// <param name="secretKey">秘钥</param> /// <returns></returns> public string Encrypt(string plaintext, out string secretKey) { secretKey = string.Empty; if (plaintext.IsNullOrWhiteSpace()) { return(string.Empty); } secretKey = Rand.Str(6);//"CWII7J"; if (secretKey.IsNullOrWhiteSpace() || secretKey.Count() != 6) { return(string.Empty); } var leftStr = secretKey.Substring(0, 3); var rightStr = secretKey.Substring(3, 3); //随机6位密码+附加码 var aesKey = "{0}{1}".FormatStr(secretKey, _KeyAppendCode).MD5().ToLower(); //加密操作 var ciphertext = AESEncrypt.AesEncrypt(plaintext, secretKey: aesKey, vectorStr: _AESVector).Replace("+", "_").Replace("/", "-").Replace("=", "*"); return("{0}{1}{2}".FormatStr(leftStr, ciphertext, rightStr)); }