本文介绍了一个简单的方法来加密和用钥匙解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找一种简单的方法来加密和解密使用固定的密钥(这将存储在我的配置)的一些数据,并将结果作为字符串(base16或十六进制)。
类似
字符串的myString =Hello World的;
字符串的myKey =k2k3aij3h;
串ENC =新算法()加密(MyString的,的myKey)。
串DEC =新Alrorithm()解密(ENC,的myKey);
解决方案
试试这个
私人常量字符串initVector =tu89geji340t89u2;
私人const int的密钥长度= 256;
公共静态字符串加密(字符串文本,字符串键)
{
字节[] = initVectorBytes Encoding.UTF8.GetBytes(initVector);
字节[] = plainTextBytes Encoding.UTF8.GetBytes(文本);
PasswordDeriveBytes密码=新PasswordDeriveBytes(密钥,NULL);
字节[] = 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();
字节[] =加密memoryStream.ToArray();
memoryStream.Close();
cryptoStream.Close();
返回Convert.ToBase64String(加密);
}
公共静态字符串解密(字符串EncryptedText,串键)
{
字节[] = initVectorBytes Encoding.ASCII.GetBytes(initVector);
字节[] = DeEncryptedText Convert.FromBase64String(EncryptedText);
PasswordDeriveBytes密码=新PasswordDeriveBytes(密钥,NULL);
字节[] = keyBytes password.GetBytes(密钥/ 8);
RijndaelManaged的symmetricKey =新RijndaelManaged的();
symmetricKey.Mode = CipherMode.CBC;
ICryptoTransform的解密= symmetricKey.CreateDecryptor(keyBytes,initVectorBytes);
的MemoryStream MemoryStream的=新的MemoryStream(DeEncryptedText);
的CryptoStream的CryptoStream =新的CryptoStream(MemoryStream的,解密,CryptoStreamMode.Read);
字节[] = plainTextBytes新的字节[DeEncryptedText.Length]
INT decryptedByteCount = cryptoStream.Read(plainTextBytes,0,plainTextBytes.Length);
memoryStream.Close();
cryptoStream.Close();
返回Encoding.UTF8.GetString(plainTextBytes,0,decryptedByteCount);
}
它的
I'm looking for an easy way to encrypt and decrypt some data using a fixed key (which will be stored in my configurations) and store the result as string (base16 or hex).
Something like
string myString = "hello world";
string myKey = "k2k3aij3h";
string enc = new Algorithm().Encrypt(myString, myKey);
string dec = new Alrorithm().Decrypt(enc, myKey);
解决方案
try this
private const string initVector = "tu89geji340t89u2";
private const int keysize = 256;
public static string Encrypt(string Text, string Key)
{
byte[] initVectorBytes = Encoding.UTF8.GetBytes(initVector);
byte[] plainTextBytes = Encoding.UTF8.GetBytes(Text);
PasswordDeriveBytes password = new PasswordDeriveBytes(Key, null);
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[] Encrypted = memoryStream.ToArray();
memoryStream.Close();
cryptoStream.Close();
return Convert.ToBase64String(Encrypted);
}
public static string Decrypt(string EncryptedText, string Key)
{
byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
byte[] DeEncryptedText = Convert.FromBase64String(EncryptedText);
PasswordDeriveBytes password = new PasswordDeriveBytes(Key, null);
byte[] keyBytes = password.GetBytes(keysize / 8);
RijndaelManaged symmetricKey = new RijndaelManaged();
symmetricKey.Mode = CipherMode.CBC;
ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes);
MemoryStream memoryStream = new MemoryStream(DeEncryptedText);
CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
byte[] plainTextBytes = new byte[DeEncryptedText.Length];
int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
memoryStream.Close();
cryptoStream.Close();
return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
}
it's from here
这篇关于一个简单的方法来加密和用钥匙解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!