背景:
CAPICOM的全名是Cryptographic API Component Object Model,是微软的Windows操作系统里的元件。为了支持密码编译功能,微软在 Windows API 之中加入了 Cryptographic API (CryptoAPI)[1],然而 CryptoAPI 的使用方法相当复杂,为了简化 CryptoAPI 的使用,微软即将它以 COM 的方式包装成 CAPICOM[2],让应用程序只需要利用 COM 调用方式,即可使用 CryptoAPI。前提:
MSDN中,提供了一个使用Cryptographic API进行加密和解密的例子,位置是:
security/example_c_program_decrypting_a_file.htm
security/example_c_program_encrypting_a_file.htm提问:
我使用security/example_c_program_encrypting_a_file.htm中的例子将一个文件加密,是否能提供CAPICOM的程序,将该文件解密呢?
CAPICOM的全名是Cryptographic API Component Object Model,是微软的Windows操作系统里的元件。为了支持密码编译功能,微软在 Windows API 之中加入了 Cryptographic API (CryptoAPI)[1],然而 CryptoAPI 的使用方法相当复杂,为了简化 CryptoAPI 的使用,微软即将它以 COM 的方式包装成 CAPICOM[2],让应用程序只需要利用 COM 调用方式,即可使用 CryptoAPI。前提:
MSDN中,提供了一个使用Cryptographic API进行加密和解密的例子,位置是:
security/example_c_program_decrypting_a_file.htm
security/example_c_program_encrypting_a_file.htm提问:
我使用security/example_c_program_encrypting_a_file.htm中的例子将一个文件加密,是否能提供CAPICOM的程序,将该文件解密呢?
如果用固定的密码加密,则用该密码可以解密。注:C#可以直接用System.Security.Cryptography命名空间下的类库而不必用C++程序。
使用CAPICOM也应该能解密CryptoAPI的文件,但我就是不知道脚本怎么写。
麻烦了。
using System.Security.Cryptography;
using System.Text;
using System.IO;class RijndaelSample
{ static void Main()
{
try
{
// Create a new Rijndael object to generate a key
// and initialization vector (IV).
Rijndael RijndaelAlg = Rijndael.Create(); // Create a string to encrypt.
string sData = "Here is some data to encrypt.";
string FileName = "CText.txt"; // Encrypt text to a file using the file name, key, and IV.
EncryptTextToFile(sData, FileName, RijndaelAlg.Key, RijndaelAlg.IV); // Decrypt the text from a file using the file name, key, and IV.
string Final = DecryptTextFromFile(FileName, RijndaelAlg.Key, RijndaelAlg.IV); // Display the decrypted string to the console.
Console.WriteLine(Final);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
} Console.ReadLine();
} public static void EncryptTextToFile(String Data, String FileName, byte[] Key, byte[] IV)
{
try
{
// Create or open the specified file.
FileStream fStream = File.Open(FileName, FileMode.OpenOrCreate); // Create a new Rijndael object.
Rijndael RijndaelAlg = Rijndael.Create(); // Create a CryptoStream using the FileStream
// and the passed key and initialization vector (IV).
CryptoStream cStream = new CryptoStream(fStream,
RijndaelAlg.CreateEncryptor(Key, IV),
CryptoStreamMode.Write); // Create a StreamWriter using the CryptoStream.
StreamWriter sWriter = new StreamWriter(cStream); try
{
// Write the data to the stream
// to encrypt it.
sWriter.WriteLine(Data);
}
catch (Exception e)
{
Console.WriteLine("An error occurred: {0}", e.Message);
}
finally
{
// Close the streams and
// close the file.
sWriter.Close();
cStream.Close();
fStream.Close();
}
}
catch (CryptographicException e)
{
Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
}
catch (UnauthorizedAccessException e)
{
Console.WriteLine("A file error occurred: {0}", e.Message);
} } public static string DecryptTextFromFile(String FileName, byte[] Key, byte[] IV)
{
try
{
// Create or open the specified file.
FileStream fStream = File.Open(FileName, FileMode.OpenOrCreate); // Create a new Rijndael object.
Rijndael RijndaelAlg = Rijndael.Create(); // Create a CryptoStream using the FileStream
// and the passed key and initialization vector (IV).
CryptoStream cStream = new CryptoStream(fStream,
RijndaelAlg.CreateDecryptor(Key, IV),
CryptoStreamMode.Read); // Create a StreamReader using the CryptoStream.
StreamReader sReader = new StreamReader(cStream); string val = null; try
{
// Read the data from the stream
// to decrypt it.
val = sReader.ReadLine();
}
catch (Exception e)
{
Console.WriteLine("An error occurred: {0}", e.Message);
}
finally
{ // Close the streams and
// close the file.
sReader.Close();
cStream.Close();
fStream.Close();
} // Return the string.
return val;
}
catch (CryptographicException e)
{
Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
return null;
}
catch (UnauthorizedAccessException e)
{
Console.WriteLine("A file error occurred: {0}", e.Message);
return null;
}
}
}
2 MSDN上C代码加解密之前首先将密码进行了MD5散列,使用该散列作为真正的密码进行加解密,而你给出的例子好像不是。
啥模拟器啊。