2017-02-01 34 views
0
static SymmetricAlgorithm encryption; 
    static string password = "SBC"; 
    static string salt = "ash"; 
public Decryption() 
    { 
     encryption = new RijndaelManaged(); 
     Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(password, Encoding.ASCII.GetBytes(salt)); 
     encryption.Key = key.GetBytes(encryption.KeySize/8); 
     encryption.IV = key.GetBytes(encryption.BlockSize/8); 
     encryption.Padding = PaddingMode.PKCS7; 
    } 
public void Decrypt(Stream inStream, Stream OutStream) 
    { 
     ICryptoTransform encryptor = encryption.CreateDecryptor(); 
     inStream.Position = 0;    
     CryptoStream encryptStream1 = new CryptoStream(OutStream, encryptor, CryptoStreamMode.Write); 
     CopyTo(inStream, encryptStream1); 
     encryptStream1.FlushFinalBlock(); 
     encryptStream1.Close(); 
     inStream.Close(); 
     OutStream.Close(); 
    } 
public void CopyTo(Stream input, Stream output) 
    { 
     // This method exists only in .NET 4 and higher 

     byte[] buffer = new byte[4 * 1024]; 
     int bytesRead; 

     while ((bytesRead = input.Read(buffer, 0, buffer.Length)) != 0) 
     { 
      output.Write(buffer, 0, bytesRead); 
     } 
    } 

在我的Windows窗体加载我只是创建一个线程,并调用函数来解密文件,这是线程功能填充是无效时解密文件

Thread objthreadhtml = new Thread(new ThreadStart(JsHtmlDecrypt)); 
      objthreadhtml.IsBackground = true; 
      objthreadhtml.Name = "HtmlJsDecrypt"; 
      objthreadhtml.Priority = ThreadPriority.Highest; 
      objthreadhtml.Start(); 

以下功能解密功能

public static void JsHtmlDecrypt() 
    { 
string startPathForHtml = Application.LocalUserAppDataPath.Replace("\\OfflineApplication\\OfflineApplication\\1.0.0.0", "").ToString() + "\\Apps\\Html\\"; 
var directoryPathForHtml = new DirectoryInfo(startPathForHtml); 
foreach (FileInfo fileForHtml in directoryPathForHtml.GetFiles()) 
     { 
      FileStream inFsForHtml = fileForHtml.OpenRead(); 
      FileInfo inforFHtml = new FileInfo(fileForHtml.FullName.Replace(fileForHtml.Extension, ".html")); 
      FileStream outFsForHtml = inforFHtml.Create(); 
      UnZipDecryptionEncryption.Decryption m_decryption1 = new Decryption(); 
      m_decryption1.Decrypt(inFsForHtml, outFsForHtml); 
      inFsForHtml.Close(); 
      outFsForHtml.Close(); 
      UnZipDecryptionEncryption.DeleteZipandFiles m_delete1 = new DeleteZipandFiles(); 
      m_delete1.DeleteFiles(fileForHtml.FullName);   
     } 
} 

在这里,我得到的错误填充是无效的线

encryptStream1.FlushFinalBlock(); 

请帮我这样的人如何解决这个我被困在里面。

回答

0

你“解密”功能正在试图做的,你想的正好相反:加密数据:

CryptoStream encryptStream1 = new CryptoStream(OutStream, encryptor, CryptoStreamMode.Write); 

你想要什么,我想,是解密它(我的代码使用字节数组作为输入/输出,所以你可能想要修改):

ICryptoTransform decryptor = encryption.CreateDecryptor(); 
// byte[] (cipherText) <-- encryted text 
MemoryStream memoryStream = new MemoryStream(cipherText); 
// here is the most important part: CryptoStreamMode.Read 
CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read); 

byte[] plainTextBytes = new byte[cipherText.Length]; 
int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length); 

memoryStream.Close(); 
cryptoStream.Close(); 

// my text uses UTF8 encoding, so to get the plain text as string: 
string result = Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount); 
+0

我只是解密文件并保存它,并且不需要解密数据 –

相关问题