2017-02-01 62 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

我只是解密文件並保存它,並且不需要解密數據 –

相關問題