我想使用一种算法加密和解密C#中的字符串,该算法将我引导到相同的加密字符串。例如,如果我加密一个字符串 122ujhdheiwe ,结果是 uoi8asdf8asdf ,如果我加密相同的字符串 122ujhdheiwe ,它再次引导我 uoi8asdf8asdf 字符串。我可以使用哪种加密算法以及如何使用?
答案 0 :(得分:1)
rot13(凯撒)可能吗?它将所有字符移动偏移量13.因此,应用它两次,您将再次获得纯文本。
答案 1 :(得分:1)
我在这里有一个简单的解决方案:
http://remy.supertext.ch/2011/01/simple-c-encryption-and-decryption/
基本上它的工作原理如下:
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] Key = { 12, 13, 14, 15, 16, 17, 18, 19 };
byte[] IV = { 12, 13, 14, 15, 16, 17, 18, 19 };
ICryptoTransform encryptor = des.CreateEncryptor(Key, IV);
byte[] IDToBytes = ASCIIEncoding.ASCII.GetBytes(source);
byte[] encryptedID = encryptor.TransformFinalBlock(IDToBytes, 0, IDToBytes.Length);
return Convert.ToBase64String(encryptedID);
然后反过来。
答案 2 :(得分:1)
如果您想要一个简单的解决方案,可以使用ProtectedData
类:
using System;
using System.Security.Cryptography;
using System.Text;
private void example()
{
string data = "122ujhdheiwe";
// Encrypt
UnicodeEncoding unicodeEncoding = new UnicodeEncoding();
byte[] secret = ProtectedData.Protect(unicodeEncoding.GetBytes(data), null, DataProtectionScope.CurrentUser);
Console.WriteLine(BitConverter.ToString(secret));
// If you need it as a printable string, you can convert the binary to Base64
string base64 = Convert.ToBase64String(secret);
Console.WriteLine(base64);
// Back to binary...
byte[] backagain = Convert.FromBase64String(base64);
// Decrypt
byte[] clearbytes = ProtectedData.Unprotect(backagain, null, DataProtectionScope.CurrentUser);
string roundtripped = unicodeEncoding.GetString(clearbytes);
Console.WriteLine(roundtripped);
}
如果您希望加密数据看起来与原始数据非常相似,如问题中的示例(122ujhdheiwe ==> uoi8asdf8asdf),那么您要寻找的是格式保留加密 - {{3我没有例子。
编辑:我刚刚注意到你在问题中写道,你希望能够再次加密相同的字符串并获得相同的加密结果,在这种情况下,ProtectedData将无法工作,因为加密中使用的密钥将会改变随着时间的推移。答案 3 :(得分:1)
我编写一个需要源代码的演示程序?
我使用此命名空间using System.Security.Cryptography;
还有两个
public string encryptus(string x, string encrypt)//function
{
try
{
string y = x;
byte[] etext = UTF8Encoding.UTF8.GetBytes(y);
string Code = encrypt;
MD5CryptoServiceProvider mdhash = new MD5CryptoServiceProvider();
byte[] keyarray = mdhash.ComputeHash(UTF8Encoding.UTF8.GetBytes(Code));
TripleDESCryptoServiceProvider tds = new TripleDESCryptoServiceProvider();
tds.Key = keyarray;
tds.Mode = CipherMode.ECB;
tds.Padding = PaddingMode.PKCS7;
ICryptoTransform itransform = tds.CreateEncryptor();
byte[] result = itransform.TransformFinalBlock(etext, 0, etext.Length);
string encryptresult = Convert.ToBase64String(result);
return encryptresult.ToString();
}
catch (Exception ex)
{
return ex.Message;
}
}
public string dencryptus(string x, string keyai)
{
try
{
string y = x.Replace("\0", null);
byte[] etext = Convert.FromBase64String(y);
string key = keyai;
MD5CryptoServiceProvider mdhash = new MD5CryptoServiceProvider();
byte[] keyarray = mdhash.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
TripleDESCryptoServiceProvider tds = new TripleDESCryptoServiceProvider();
tds.Key = keyarray;
tds.Mode = CipherMode.ECB;
tds.Padding = PaddingMode.PKCS7;
ICryptoTransform itransform = tds.CreateDecryptor();
byte[] result = itransform.TransformFinalBlock(etext, 0, etext.Length);
string dencryptresult = UTF8Encoding.UTF8.GetString(result);
return dencryptresult.ToString();
}
catch (Exception ex)
{
return ex.Message;
}
}
并像这样使用它们:
Encrypted.Text = encryptus(Message.Text, EncryptCode.Text.ToString());
Decrypted.Text = dencryptus(Message.Text, EncryptCode.Text.ToString());
答案 4 :(得分:0)
这个答案包含我准备使用Crypto Class Triple Des
.NET: what are my options for decrypting a password in my project .setting file
只需创建新的类库,复制/粘贴此代码,然后使用它:)