{"id":47,"date":"2008-08-18T09:34:57","date_gmt":"2008-08-18T16:34:57","guid":{"rendered":"http:\/\/cknotes.com\/?p=47"},"modified":"2008-08-18T09:34:57","modified_gmt":"2008-08-18T16:34:57","slug":"c-encryptingdecrypting-with-stream","status":"publish","type":"post","link":"https:\/\/cknotes.com\/c-encryptingdecrypting-with-stream\/","title":{"rendered":"C# Encrypting\/Decrypting with Stream"},"content":{"rendered":"<p>This C# example demonstrates how to use Chilkat.Crypt2 to encrypt and decrypting using the .NET FileStream class:<\/p>\n<pre>\r\n    Chilkat.Crypt2 crypt = new Chilkat.Crypt2();\r\n    bool success = crypt.UnlockComponent(&quot;Anything for 30-day trial&quot;);\r\n    if (!success)\r\n    {\r\n        MessageBox.Show(crypt.LastErrorText);\r\n        return;\r\n    }\r\n\r\n    crypt.CryptAlgorithm = &quot;aes&quot;;\r\n    crypt.CipherMode = &quot;cbc&quot;;\r\n    crypt.KeyLength = 128;\r\n\r\n    crypt.SetEncodedIV(&quot;0000000000000000&quot;, &quot;hex&quot;);\r\n    crypt.SetEncodedKey(&quot;abcdefghijklmnop&quot;, &quot;ascii&quot;);\r\n\r\n    \/\/ Open input and output files as file streams...\r\n    FileStream fsIn = File.OpenRead(&quot;hamlet.xml&quot;);\r\n    FileStream fsOut = File.Create(&quot;encrypted.dat&quot;);\r\n\r\n    crypt.FirstChunk = true;\r\n    crypt.LastChunk = false;\r\n    byte [] encryptedChunk;\r\n\r\n    \/\/ Encrypt the stream in 1024 byte chunks.\r\n    byte[] b = new byte[1024];\r\n\r\n    int n;\r\n    while ((n = fsIn.Read(b, 0, b.Length)) > 0)\r\n    {\r\n        if (n &lt; b.Length)\r\n        {\r\n            \/\/ Don't encrypt the full 1024 bytes, only the amount read...\r\n            byte[] tmp = new byte[n];\r\n            int i;\r\n            for (i = 0; i &lt; n; i++) tmp[i] = b[i];\r\n            encryptedChunk = crypt.EncryptBytes(tmp);\r\n        }\r\n        else\r\n        {\r\n            encryptedChunk = crypt.EncryptBytes(b);\r\n        }\r\n\r\n        fsOut.Write(encryptedChunk, 0, encryptedChunk.Length);\r\n        crypt.FirstChunk = false;\r\n    }\r\n    fsIn.Close();\r\n\r\n    \/\/ Flush any remaining encrypted data.\r\n    crypt.LastChunk = true;\r\n    byte[] empty = { };\r\n    encryptedChunk = crypt.EncryptBytes(empty);\r\n    if (encryptedChunk.Length > 0)\r\n    {\r\n        fsOut.Write(encryptedChunk, 0, encryptedChunk.Length);\r\n    }\r\n    fsOut.Close();\r\n\r\n\r\n    \/\/ Now decrypt in chunks.  The decryptor must know when the last\r\n    \/\/ block is being processed so it may unpad it correctly.\r\n\r\n    System.IO.FileInfo fi = new System.IO.FileInfo(&quot;encrypted.dat&quot;);\r\n    long fileSize = fi.Length;\r\n    int numChunks = (int)fileSize \/ b.Length;\r\n\r\n    fsIn = File.OpenRead(&quot;encrypted.dat&quot;);\r\n    fsOut = File.Create(&quot;decrypted.xml&quot;);\r\n\r\n    crypt.FirstChunk = true;\r\n    crypt.LastChunk = false;\r\n    byte[] decryptedChunk;\r\n\r\n    int idx;\r\n    for (idx = 0; idx &lt;= numChunks; idx++)\r\n    {\r\n        n = fsIn.Read(b, 0, b.Length);\r\n\r\n        \/\/ Is this the last chunk?\r\n        if (idx == numChunks)\r\n        {\r\n            \/\/ Decrypt only the amount read...\r\n            byte[] lastBlock = new byte[n];\r\n            int i;\r\n            for (i = 0; i &lt; n; i++) lastBlock[i] = b[i];\r\n            crypt.LastChunk = true;\r\n            decryptedChunk = crypt.DecryptBytes(lastBlock);\r\n        }\r\n        else\r\n        {\r\n            decryptedChunk = crypt.DecryptBytes(b);\r\n        }\r\n\r\n        fsOut.Write(decryptedChunk, 0, decryptedChunk.Length);\r\n        crypt.FirstChunk = false;\r\n    }\r\n    fsIn.Close();\r\n    fsOut.Close();\r\n\r\n    MessageBox.Show(&quot;Finished!&quot;);\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This C# example demonstrates how to use Chilkat.Crypt2 to encrypt and decrypting using the .NET FileStream class: Chilkat.Crypt2 crypt = new Chilkat.Crypt2(); bool success = crypt.UnlockComponent(&quot;Anything for 30-day trial&quot;); if (!success) { MessageBox.Show(crypt.LastErrorText); return; } crypt.CryptAlgorithm = &quot;aes&quot;; crypt.CipherMode = &quot;cbc&quot;; crypt.KeyLength = 128; crypt.SetEncodedIV(&quot;0000000000000000&quot;, &quot;hex&quot;); crypt.SetEncodedKey(&quot;abcdefghijklmnop&quot;, &quot;ascii&quot;); \/\/ Open input and output files as [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[470,448,431,469,98],"class_list":["post-47","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-aes","tag-c","tag-encryption","tag-stream","tag-streaming"],"_links":{"self":[{"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/posts\/47","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/comments?post=47"}],"version-history":[{"count":0,"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/posts\/47\/revisions"}],"wp:attachment":[{"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/media?parent=47"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/categories?post=47"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cknotes.com\/wp-json\/wp\/v2\/tags?post=47"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}