我有一个PHP方案,将PDF文档加密为xxx, 存档,这一产出正通过一个C#方案阅读,该方案将这xxx号档案重新贴上PDF档案。
我的问题是,当我打开C# 加密的档案时,人民抵抗力量的读者告诉我,档案是腐败的。 当我在PHP的加密文本和C# I的加密中,档案被加密时,问题只是出现在人民抵抗力量的档案中,或者说出现在BINaire的档案中。
任何建议!
注:
- In PHP I use mcrypt extension Rijndael algorithm CBC PKCS7 padding (padding is done manually)
- In C# I use RijndaelManaged class to encrypt and decrypt data
http://www.ohchr.org。
Here is encryption method that I use in 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 ), " " );
$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();