加密和解密没有特殊字符

时间:2014-04-22 04:33:04

标签: asp.net encryption console-application

我想加密邮件ID。加密的邮件ID不应包含特殊字符。 我从控制台应用程序发送邮件。在控制台应用程序中,我编码邮件ID并将其附加到将执行我的点击计数的链接中。在Web应用程序中,我正在解码传递的邮件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);

2 个答案:

答案 0 :(得分:6)

根据尼普的提示,我得到了答案。

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

答案 1 :(得分:0)

你需要尝试不同的Algo相同的

尝试以下任何方法,看看它是否适合您?

当您使用控制台应用程序时,这不会让您工作,但可以尝试其他应用程序。     string EncryptedText = FormsAuthentication.HashPasswordForStoringInConfigFile(&#34; YourPlainText&#34;,&#34; MD5&#34;);

或者,您可以使用以下加密和解密算法:

using System.IO;
using System.Text;
using System.Security.Cryptography;

/// <summary>
/// Summary description for Pass
/// </summary>
public class CryptoSystem
{
    public string plainText;
    public string passPhrase = "Pas5pr@se";
    public string saltValue = "s@1tValue";
    public string hashAlgorithm = "MD5";
    public int passwordIterations = 2;
    public string initVector = "@1B2c3D4e5F6g7H8";
    public int keySize = 256;

    public string Encrypt(string plainText)
    {
        byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
        byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);
        byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
        PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, saltValueBytes, hashAlgorithm, passwordIterations);
        byte[] keyBytes = password.GetBytes(keySize / 8);
        RijndaelManaged symmetricKey = new RijndaelManaged();
        symmetricKey.Mode = CipherMode.CBC;
        ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes);
        MemoryStream memoryStream = new MemoryStream();
        CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write);
        cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
        cryptoStream.FlushFinalBlock();
        byte[] cipherTextBytes = memoryStream.ToArray();
        memoryStream.Close();
        cryptoStream.Close();
        string cipherText = Convert.ToBase64String(cipherTextBytes);
        return cipherText;
    }

    public string Decrypt(string cipherText)
    {
        byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
        byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);        
        byte[] cipherTextBytes = Convert.FromBase64String(cipherText);
        PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, saltValueBytes, hashAlgorithm, passwordIterations);
        byte[] keyBytes = password.GetBytes(keySize / 8);
        RijndaelManaged symmetricKey = new RijndaelManaged();
        symmetricKey.Mode = CipherMode.CBC;
        ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes);
        MemoryStream memoryStream = new MemoryStream(cipherTextBytes);
        CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
        byte[] plainTextBytes = new byte[cipherTextBytes.Length];
        int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
        memoryStream.Close();
        cryptoStream.Close();
        string plainText = Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
        return plainText;
    }
}

也尝试使用十六进制 http://www.string-functions.com/string-hex.aspx

要编码,请点击此链接 Convert string to hex-string in C#

byte[] ba = Encoding.Default.GetBytes("sample");
var hexString = BitConverter.ToString(ba);