Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
2.09/5 (3 votes)
See more:
Hi guys..
I want to encrypt mail id. The encrypted mail id should not contain special characters.
I have used this:

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


This is producing mail id with special characters.

Please help me.
Posted

Encryption always produces "special characters" because it converts the input (from text or whatever it was) to a collection of bytes - which aren't characters, they are eight bit values which may contain valid characters but are more likely not to.

Try converting the byte array to Base64[^] - that will contain only "readable" characters.
 
Share this answer
 
I got my ans from here:http://www.nullskull.com/faq/834/convert-string-to-hex-and-hex-to-string-in-net.aspx[^]

a) Convert String to Hex
C#
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) Convert Hex to String
C#
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);
    }

Sample usage code
C#
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");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900