未解决的加密解密编码问题?
using System;
using System.Security.Cryptography;
using System.Text;
using System.IO;
class TrippleDESCSPSample
{
static void Main ()
{
try
{
// Create a new TripleDESCryptoServiceProvider object
// to generate a key and initialization vector (IV).
//TripleDESCryptoServiceProvider tDESalg = new TripleDESCryptoServiceProvider();
string s1 = "!@#$1234asdf";
string s2 = "!@#$";
byte[] b1 = Encoding.Unicode.GetBytes(s1);
byte[] b2 = Encoding.Unicode.GetBytes(s2);
// Create a string to encrypt.
// string sData = "Here is some data to encrypt.";
string sData = "Here is some data to encrypt.";
// Encrypt the string to an in-memory buffer.
//byte[] Data = EncryptTextToMemory(sData, tDESalg.Key, tDESalg.IV);
byte[] Data = EncryptTextToMemory(sData, b1, b2);
Console.WriteLine("byte[] Data = EncryptTextToMemory(sData, b1, b2):");
Print(Data);
Print2(Data);
string s = new UnicodeEncoding().GetString(Data);
Console.WriteLine(s);
byte[] Data1 = new UnicodeEncoding().GetBytes(s);
Console.WriteLine("byte[] Data1 = new UnicodeEncoding().GetBytes(s):");
Print(Data1);
Print2(Data1);
// Decrypt the buffer back to a string.
//string Final = DecryptTextFromMemory(Data, tDESalg.Key, tDESalg.IV);
string Final = DecryptTextFromMemory(Data, b1, b2);
// Display the decrypted string to the console.
Console.WriteLine(Final);
Console.ReadLine();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.ReadLine();
}
}
public static byte[] EncryptTextToMemory(string Data, byte[] Key, byte[] IV)
{
try
{
// Create a MemoryStream.
MemoryStream mStream = new MemoryStream();
TripleDESCryptoServiceProvider TDES = new TripleDESCryptoServiceProvider();
// Create a CryptoStream using the MemoryStream
// and the passed key and initialization vector (IV).
CryptoStream cStream = new CryptoStream(mStream,
TDES.CreateEncryptor(Key, IV),
CryptoStreamMode.Write);
// Convert the passed string to a byte array.
byte[] toEncrypt = new UnicodeEncoding().GetBytes(Data);
// Write the byte array to the crypto stream and flush it.
cStream.Write(toEncrypt, 0, toEncrypt.Length);
cStream.FlushFinalBlock();
Console.WriteLine("toEncrypt.Length:{0}", toEncrypt.Length);
// Get an array of bytes from the
// MemoryStream that holds the
// encrypted data.
byte[] ret = mStream.ToArray();
Console.WriteLine("the Length of mStream.ToArray:{0}", ret.Length);
// Close the streams.
cStream.Close();
mStream.Close();
// Return the encrypted buffer.
return ret;
}
catch (CryptographicException e)
{
Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
return null;
}
}
public static string DecryptTextFromMemory(byte[] Data, byte[] Key, byte[] IV)
{
try
{
// Create a new MemoryStream using the passed
// array of encrypted data.
MemoryStream msDecrypt = new MemoryStream(Data);
TripleDESCryptoServiceProvider TDES = new TripleDESCryptoServiceProvider();
// Create a CryptoStream using the MemoryStream
// and the passed key and initialization vector (IV).
CryptoStream csDecrypt = new CryptoStream(msDecrypt,
TDES.CreateDecryptor(Key, IV),
CryptoStreamMode.Read);
Console.WriteLine("Data.Length:{0}", Data.Length);
// Create buffer to hold the decrypted data.
byte[] fromEncrypt = new byte[Data.Length];
// Read the decrypted data out of the crypto stream
// and place it into the temporary buffer.
csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);
Console.WriteLine("fromEncrypt.Length:{0}", fromEncrypt.Length);
//Convert the buffer into a string and return it.
return new UnicodeEncoding().GetString(fromEncrypt);
}
catch (CryptographicException e)
{
Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
return null;
}
}
public static void Print(byte[] bytes)
{
if ((bytes == null) || (bytes.Length == 0))
Console.WriteLine("<none>");
else
{
Console.WriteLine("using for (int i = 0; i < bytes.Length; i++) and bytes[i] print the encoded bytes:");
for (int i = 0; i < bytes.Length; i++)
Console.Write("{0:X2} ", bytes[i]);
Console.WriteLine();
}
}
public static void Print2(byte[] bytes)
{
Console.WriteLine("using foreach (Byte b in bytes) to print the encoded bytes:");
foreach (Byte b in bytes)
{
Console.Write("[{0}]", b);
}
Console.WriteLine();
}
}
测试字符串: sData = "字是学习五笔字型的一个重要环节。";