我有一个PHP程序,将PDF文件加密成.xxx文件,这个输出被C#程序读取,该程序将这个.xxx文件解密为PDF文件。输出无效
我的问题是,当我打开C#解密的文件时,PDF阅读器告诉我该文件已损坏..当我在PHP中加密纯文本并解密C#时,我得到了我加密的文件..所以问题只出现在PDF文件或者换句话说,它出现在BINARY文件
什么建议吗?!
注:
- 在PHP我使用mcrypt扩展Rijndael算法CBC PKCS7填充(填充是手动完成)
- 在C#我使用RijndaelManaged类来加密和解密数据
编辑:
这里是我在PHP中使用的加密方法:
function encrypt($key, $iv, $text) {
ini_set ('memory_limit', '-1');
$mcrypt_cipher = MCRYPT_RIJNDAEL_256;
$mcrypt_mode = MCRYPT_MODE_CBC;
$text=addpadding($text,mcrypt_get_block_size($mcrypt_cipher,'cbc'));
$encrypted = rtrim (mcrypt_encrypt ($mcrypt_cipher, $key, $text, $mcrypt_mode, $iv), "\0");
$encrypted = base64_encode ($encrypted);
return $encrypted;
}
这里是C#中的解密方法:
public static string DecryptString(string message, string KeyString, string IVString)
{
byte[] Key = Encoding.UTF8.GetBytes(KeyString);
byte[] IV = Encoding.UTF8.GetBytes(IVString);
string decrypted = null;
RijndaelManaged rj = new RijndaelManaged();
rj.BlockSize = 256;
rj.Key = Key;
rj.IV = IV;
rj.Mode = CipherMode.CBC;
rj.Padding = PaddingMode.PKCS7;
try
{
MemoryStream ms = new MemoryStream();
//Encoding enc = new UTF8Encoding();
byte[] messageBytes = Convert.FromBase64String(message);
using (CryptoStream cs = new CryptoStream(ms, rj.CreateDecryptor(Key, IV), CryptoStreamMode.Write))
{
//byte[] messageBytes = enc.GetBytes(message);
cs.Write(messageBytes, 0, messageBytes.Length);
cs.Close();
}
byte[] encoded = ms.ToArray();
decrypted = Encoding.UTF8.GetString(encoded);
ms.Close();
}
catch (Exception e)
{
MessageBox.Show("An error occurred:"+ e.Message);
}
finally
{
rj.Clear();
}
return decrypted;
}
,这里是我如何调用解密在C#中,我怎么写输出:
string Key = cryptography.MD5("X-Ware" + cryptography.MD5("123"));
string IV = cryptography.MD5("XWare");
string decrypted = cryptography.DecryptString(contents, Key, IV);
string outputFilename = cryptography.MD5(OFD.FileName) + ".tmp";
StreamWriter sw = new StreamWriter("C:\\Windows\\Temp\\" + outputFilename, false, Encoding.UTF8);
BinaryWriter bw = new BinaryWriter(sw.BaseStream, Encoding.UTF8);
//sw.Write(decrypted);
bw.Write(decrypted);
sw.Close();
bw.Close();
你可以粘贴你是怎么做到的编码/解码? – greut 2012-01-14 13:20:46
解密文件与原文件大小相同吗? – 2012-01-14 13:24:53
在Windows上我想? – 2012-01-14 13:25:50