나는이 코드를 rijndael로 암호화/해독하는 데 사용했습니다. 암호화 된 문자열을 건 드리면 CryptographicException을 던집니다. http://www.codeproject.com/Articles/5719/Simple-encrypting-and-decrypting-data-in-C
public string Encrypt(string clearText, string Password)
{
//Convert text to bytes
byte[] clearBytes =
System.Text.Encoding.Unicode.GetBytes(clearText);
//We will derieve our Key and Vectore based on following
//password and a random salt value, 13 bytes in size.
PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,
new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d,
0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76});
byte[] encryptedData = Encrypt(clearBytes,
pdb.GetBytes(32), pdb.GetBytes(16));
return Convert.ToBase64String(encryptedData);
}
//Call following function to decrypt data
public string Decrypt(string cipherText, string Password)
{
//Convert base 64 text to bytes
byte[] cipherBytes = Convert.FromBase64String(cipherText);
//We will derieve our Key and Vectore based on following
//password and a random salt value, 13 bytes in size.
PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,
new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65,
0x64, 0x76, 0x65, 0x64, 0x65, 0x76});
byte[] decryptedData = Decrypt(cipherBytes,
pdb.GetBytes(32), pdb.GetBytes(16));
//Converting unicode string from decrypted data
return Encoding.Unicode.GetString(decryptedData);
}
public byte[] Encrypt(byte[] clearData, byte[] Key, byte[] IV)
{
byte[] encryptedData;
//Create stream for encryption
using (MemoryStream ms = new MemoryStream())
{
//Create Rijndael object with key and vector
using (Rijndael alg = Rijndael.Create())
{
alg.Key = Key;
alg.IV = IV;
//Forming cryptostream to link with data stream.
using (CryptoStream cs = new CryptoStream(ms,
alg.CreateEncryptor(), CryptoStreamMode.Write))
{
//Write all data to stream.
cs.Write(clearData, 0, clearData.Length);
}
encryptedData = ms.ToArray();
}
}
return encryptedData;
}
public byte[] Decrypt(byte[] cipherData, byte[] Key, byte[] IV)
{
byte[] decryptedData;
//Create stream for decryption
using (MemoryStream ms = new MemoryStream())
{
//Create Rijndael object with key and vector
using (Rijndael alg = Rijndael.Create())
{
alg.Key = Key;
alg.IV = IV;
//Forming cryptostream to link with data stream.
using (CryptoStream cs = new CryptoStream(ms,
alg.CreateDecryptor(), CryptoStreamMode.Write))
{
//Write all data to stream.
cs.Write(cipherData, 0, cipherData.Length);
}
decryptedData = ms.ToArray();
}
}
return decryptedData;
}
"인코딩"의 사용/"디코드"- 나는 분명히 할 수있다 : 우리는 서명과 함께 아마도/여기에 "해독" "암호화"에 대해 얘기하고 ? 대부분의 암호화는 raw 바이트 스트림을 출력하지만, 문자열로 표현하기 위해 base-64로 인코딩 할 수 있습니다 (비트가 더 클 것입니다) –
yes, encrypt 및 checksumming (문자열이 변경되지 않았 음을 알고 있습니다) . Base64 bin-to-text는 문제가 없습니다. –
나는 당신이 암호화와 암호화를 혼란스럽게 생각한다고 생각합니다. 암호화가 필요합니다. 좋은 답변을 반복하기보다는 http://stackoverflow.com/questions/202011/encrypt-decrypt-string-in-net 및 http://stackoverflow.com/questions/165808/simple-2-way-encryption- for-c-sharp – Kami