Note that there are some explanatory texts on larger screens.

plurals
  1. POBad Data cryptographicexception in xml using Linq in c#
    primarykey
    data
    text
    <p>My XML file:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;dskh&gt; &lt;khachhang maso="kh01"&gt; &lt;ten_kh&gt;thehung&lt;/ten_kh&gt; &lt;tuoi_kh&gt;15&lt;/tuoi_kh&gt; &lt;dchi_kh&gt;hochiminh&lt;/dchi_kh&gt; &lt;/khachhang&gt; &lt;khachhang maso="kh02"&gt; &lt;ten_kh&gt;hung&lt;/ten_kh&gt; &lt;tuoi_kh&gt;15&lt;/tuoi_kh&gt; &lt;dchi_kh&gt;hcm&lt;/dchi_kh&gt; &lt;/khachhang&gt; &lt;/dskh&gt; </code></pre> <p>My Encrypt and Decrypt code:</p> <pre><code>class Program { // Call this function to remove the key from memory after use for security. [System.Runtime.InteropServices.DllImport("KERNEL32.DLL", EntryPoint = "RtlZeroMemory")] public static extern bool ZeroMemory(ref string Destination, int Length); // Function to Generate a 64 bits Key. static string GenerateKey() { // Create an instance of Symetric Algorithm. Key and IV is generated automatically. DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create(); // Use the Automatically generated key for Encryption. return ASCIIEncoding.ASCII.GetString(desCrypto.Key); } static void EncryptFile(string sInputFilename, string sOutputFilename, string sKey) { FileStream fsInput = new FileStream(sInputFilename,FileMode.Open,FileAccess.Read); FileStream fsEncrypted = new FileStream(sOutputFilename,FileMode.Create,FileAccess.Write); DESCryptoServiceProvider DES = new DESCryptoServiceProvider(); DES.Key = Encoding.UTF8.GetBytes(sKey); DES.IV = Encoding.UTF8.GetBytes(sKey); ICryptoTransform desencrypt = DES.CreateEncryptor(); CryptoStream cryptostream = new CryptoStream(fsEncrypted,desencrypt,CryptoStreamMode.Write); byte[] bytearrayinput = new byte[fsInput.Length - 1]; fsInput.Read(bytearrayinput, 0, bytearrayinput.Length); cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length); } static void DecryptFile(string sInputFilename, string sOutputFilename, string sKey) { DESCryptoServiceProvider DES = new DESCryptoServiceProvider(); //A 64 bit key and IV is required for this provider. //Set secret key For DES algorithm. DES.Key = Encoding.UTF8.GetBytes(sKey); //Set initialization vector. DES.IV = Encoding.UTF8.GetBytes(sKey); //Create a file stream to read the encrypted file back. FileStream fsread = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read); //Create a DES decryptor from the DES instance. ICryptoTransform desdecrypt = DES.CreateDecryptor(); //Create crypto stream set to read and do a //DES decryption transform on incoming bytes. CryptoStream cryptostreamDecr = new CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read); //Print the contents of the decrypted file. StreamWriter fsDecrypted = new StreamWriter(sOutputFilename); fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd()); fsDecrypted.Flush(); fsDecrypted.Close(); } static void Main(string[] args) { // Must be 64 bits, 8 bytes. // Distribute this key to the user who will decrypt this file. string sSecretKey; // Get the key for the file to encrypt. sSecretKey = GenerateKey(); // For additional security pin the key. GCHandle gch = GCHandle.Alloc(sSecretKey, GCHandleType.Pinned); // Encrypt the file. EncryptFile(@"C:\xml_kh.xml", @"C:\xml_encry.xml", sSecretKey); //Decrypt the file. DecryptFile(@"C:\xml_encry.xml", @"C:\xml_decry.xml", sSecretKey); } } </code></pre> <p>When i excute Decrypt code, i get <strong>Cryptographicexception: Bad Data</strong> here:</p> <pre><code>//Print the contents of the decrypted file. StreamWriter fsDecrypted = new StreamWriter(sOutputFilename); fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd()); fsDecrypted.Flush(); fsDecrypted.Close(); </code></pre> <p>Please help me!!!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload