gpt4 book ai didi

asp.net - 无特殊字符加解密

转载 作者:行者123 更新时间:2023-12-01 23:19:11 24 4
gpt4 key购买 nike

我想加密邮件 ID。加密的邮件 ID 不应包含特殊字符。
我从控制台应用程序发送邮件。在控制台应用程序中,我对邮件 ID 进行编码并将其附加到将执行我的点击计数的链接中。在网络应用程序中,我正在解码传递的邮件 ID。因此,如果加密的邮件 ID 包含特殊字符,则会干扰我的链接。

我正在使用以下内容:

    string EncryptedEmailId;
string EncryptionKey = "MAKV2SPBNI99212";
byte[] EmailIdEncrypt = Encoding.Unicode.GetBytes(InvEmail);

using (Aes encryptor = Aes.Create())
{
Rfc2898DeriveBytes pdbEncrypt = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
encryptor.Key = pdbEncrypt.GetBytes(32);
encryptor.IV = pdbEncrypt.GetBytes(16);
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
{
csEncrypt.Write(EmailIdEncrypt, 0, EmailIdEncrypt.Length);
csEncrypt.Close();
}
EncryptedEmailId = Convert.ToBase64String(msEncrypt.ToArray());
}
}
individualContent = individualContent.Replace("[MailId]", EncryptedEmailId);

最佳答案

在 Nipun 的提示下,我得到了答案。

a) 将字符串转换为十六进制

    public static string ConvertStringToHex(String input, System.Text.Encoding encoding)
{
Byte[] stringBytes = encoding.GetBytes(input);
StringBuilder sbBytes = new StringBuilder(stringBytes.Length * 2);
foreach (byte b in stringBytes)
{
sbBytes.AppendFormat("{0:X2}", b);
}
return sbBytes.ToString();
}

b) 将十六进制转换为字符串
     public static string ConvertHexToString(String hexInput, System.Text.Encoding encoding)
{
int numberChars = hexInput.Length;
byte[] bytes = new byte[numberChars / 2];
for (int i = 0; i < numberChars; i += 2)
{
bytes[i / 2] = Convert.ToByte(hexInput.Substring(i, 2), 16);
}
return encoding.GetString(bytes);
}

示例使用代码
    string testString = "MIKA@?&^";
string hex = ConvertStringToHex(testString, System.Text.Encoding.Unicode);
string normal = ConvertHexToString(hex, System.Text.Encoding.Unicode);
Debug.Assert(testString.CompareTo(normal) == 0, "They are not identical");

看看: http://www.nullskull.com/faq/834/convert-string-to-hex-and-hex-to-string-in-net.aspx

关于asp.net - 无特殊字符加解密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23210424/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com