【问题标题】:Encryption and decryption without special character无特殊字符的加解密
【发布时间】:2014-06-06 06:52:50
【问题描述】:

我想加密邮件 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);

【问题讨论】:

    标签: asp.net encryption console-application


    【解决方案1】:

    在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

    【讨论】:

    • 但它会生成很长的加密字符串。我想要没有特殊字符的小加密字符串
    【解决方案2】:

    你需要尝试不同的算法来实现相同的

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

    当您使用控制台应用程序时,这对您不起作用,但可以尝试其他应用程序。 string EncryptedText = FormsAuthentication.HashPasswordForStoringInConfigFile("YourPlainText", "MD5");

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

    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);
    

    【讨论】:

    • 我之前试过这个。它产生带有特殊字符的编码,例如 +,=..
    • 如果Base64编码不行,那么十六进制编码呢?
    • 更新了我的答案@Ami,看看对你有没有帮助
    猜你喜欢
    • 2012-12-21
    • 1970-01-01
    • 2016-04-22
    • 1970-01-01
    • 2017-09-09
    • 1970-01-01
    • 1970-01-01
    • 2017-01-19
    • 1970-01-01
    相关资源
    最近更新 更多