需要一个加密/解密方法没有一个“/”在加密的串 [英] Need an Encryption/decryption method does not have a '/' in the encrypted string
问题描述
我需要进行加密和解密的字符串值,如电子邮件地址和数值,但加密的字符串不应该在它有一个'/',因为我使用了一个网址,并使用'/'作为分隔符来获得一些值。
我目前使用下面的方法:
字符串的密码=Pas5pr @ SE //可以是任何字符串
字符串saltValue =S @ 1tValue //可以是任何字符串
的HashAlgorithm字符串=SHA1; //可以是MD5
INT passwordIterations = 2; //可以是任何数量
字符串initVector =@ 1B2c3D4e5F6g7H8 //必须是16个字节
INT密钥大小= 256; //可能是192或128 公共字符串加密(字符串明文)
{
字节[] = initVectorBytes Encoding.ASCII.GetBytes(initVector);
字节[] = saltValueBytes Encoding.ASCII.GetBytes(saltValue);
字节[] = plainTextBytes Encoding.UTF8.GetBytes(明文);
PasswordDeriveBytes密码=新PasswordDeriveBytes(密码,saltValueBytes,的HashAlgorithm,passwordIterations);
字节[] = keyBytes password.GetBytes(密钥/ 8);
RijndaelManaged的symmetricKey =新RijndaelManaged的();
symmetricKey.Mode = CipherMode.CBC;
ICryptoTransform的加密= symmetricKey.CreateEncryptor(keyBytes,initVectorBytes);
MemoryStream的MemoryStream的=新的MemoryStream();
CryptoStream的CryptoStream的=新的CryptoStream(MemoryStream的,加密,CryptoStreamMode.Write);
cryptoStream.Write(plainTextBytes,0,plainTextBytes.Length);
cryptoStream.FlushFinalBlock();
字节[] = cipherTextBytes memoryStream.ToArray();
memoryStream.Close();
cryptoStream.Close();
串密文= Convert.ToBase64String(cipherTextBytes);
返回密文;
}
如果你正在做它传递的只是URL,我建议你生成任何加密字符串(如果它没有物质 /
或没有),然后执行:
VAR消毒= HttpUtility.UrlEn code(encryptedString);
正如你所看到的, /
变成%2F
。然后,你可以简单地做:
VAR encryptedString = HttpUtility.UrlDe code(消毒)
和你将再次得到相同的字符串。
编辑: HttpUtility
是的System.Web
组装
I need to encrypt and decrypt string values such as Email address and numeric values, but the encrypted string should not have a '/' in it because I am using that in a URL and using '/' for a separator to get some values.
I am currently using following method:
string passPhrase = "Pas5pr@se"; // can be any string
string saltValue = "s@1tValue"; // can be any string
string hashAlgorithm = "SHA1"; // can be "MD5"
int passwordIterations = 2; // can be any number
string initVector = "@1B2c3D4e5F6g7H8"; // must be 16 bytes
int keySize = 256; // can be 192 or 128
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;
}
If you're doing it for passing in the URL only, I suggest you generating any encrypted string (no matter if it has /
or not), and do:
var sanitized = HttpUtility.UrlEncode(encryptedString);
As you can see, /
becomes %2f
. Then you can simply do:
var encryptedString = HttpUtility.UrlDecode(sanitized)
and you will get the same string again.
Edit: HttpUtility
is in the System.Web
assembly.
这篇关于需要一个加密/解密方法没有一个“/”在加密的串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!