我正在使用以下加密方法来加密字符串
private static byte[] mKey = { };
private static byte[] mIV = { 89, 23, 13, 17, 69, 32, 02, 79 };
private static string mStringKey = "lkj@788*";
private static string Encrypt(string pText)
{
try
{
mKey = Encoding.UTF8.GetBytes(mStringKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
Byte[] byteArray = Encoding.UTF8.GetBytes(pText);
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream,
des.CreateEncryptor(mKey, mIV), CryptoStreamMode.Write);
cryptoStream.Write(byteArray, 0, byteArray.Length);
cryptoStream.FlushFinalBlock();
return Convert.ToBase64String(memoryStream.ToArray());
}
catch (Exception ex)
{
return string.Empty;
}
}
我想为具有这种格式“CustomerId-PurchasedItem-DiscountValue”的客户构建折扣代码,并分别加密这些(CustomerId、PurchasedItem、DiscountValue)字符串,并在它们之间添加“-”字符来组合加密值以构建折扣代码。同时在上面解密编码字符串,我将用“-”字符拆分它,并单独解码,但我担心在加密时如果它得到“-”字符,那么我的逻辑将失败..这种方法是否安全,或者任何人都可以建议我另一个技巧?或者有什么技巧可以将字符串加密为固定长度?