加密和解密没有特殊字符 [英] Encryption and decryption without special character

查看:125
本文介绍了加密和解密没有特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好..

我想加密邮件ID。加密的邮件ID不应包含特殊字符。

我用过这个:



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

使用(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 );
使用(MemoryStream msEncrypt = new MemoryStream())
{
使用(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);





这是生成带有特殊字符的邮件ID。



请帮助我。

解决方案

加密总是产生特殊字符,因为它将输入(从文本或其他任何内容)转换为集合字节 - 不是字符,它们是8位值,可能包含有效字符,但更可能不包含。



尝试将字节数组转换为 Base64 [ ^ ] - 只包含可读字符。


<集团kquote>我从这里得到了我的答案: http://www.nullskull.com/faq/834/convert-string-to-hex-and-hex-to-string-in-net.aspx [ ^ ]



a)将字符串转换为Hex

  public   static   string  ConvertStringToHex( String 输入,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)将Hex转换为字符串

  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 它们不相同);


Hi guys..
I want to encrypt mail id. The encrypted mail id should not contain special characters.
I have used this:

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.

解决方案

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.


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

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

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

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


这篇关于加密和解密没有特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆