{"id":881,"date":"2014-09-13T09:41:58","date_gmt":"2014-09-13T09:41:58","guid":{"rendered":"http:\/\/www.gironsec.com\/blog\/?p=881"},"modified":"2014-09-14T08:48:47","modified_gmt":"2014-09-14T08:48:47","slug":"friday-night-coding","status":"publish","type":"post","link":"https:\/\/www.gironsec.com\/blog\/2014\/09\/friday-night-coding\/","title":{"rendered":"Friday night coding"},"content":{"rendered":"<p>This is how I spent my Friday night &#8211; I was sipping some delicious liquor and thinking about how crappy XOR encryption is unless the key length is high. Then I thought to myself &#8220;What if there was a dynamic xor key for each shift?&#8221;. Then I thought &#8220;I really should be out meeting the ladies instead of wasting away my Friday nights doing code and encryption&#8221;.<\/p>\n<p>This is the result. Maybe it would be stronger if the key was random? Then again, if it was random, how would you decode it? Brute force your own encryption? Bah.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #008800; font-weight: bold\">using<\/span> <span style=\"color: #0e84b5; font-weight: bold\">System<\/span>;\r\n<span style=\"color: #008800; font-weight: bold\">using<\/span> <span style=\"color: #0e84b5; font-weight: bold\">System.Collections.Generic<\/span>;\r\n<span style=\"color: #008800; font-weight: bold\">using<\/span> <span style=\"color: #0e84b5; font-weight: bold\">System.Text<\/span>;\r\n<span style=\"color: #008800; font-weight: bold\">using<\/span> <span style=\"color: #0e84b5; font-weight: bold\">System.IO<\/span>;\r\n\r\n\r\n<span style=\"color: #008800; font-weight: bold\">namespace<\/span> <span style=\"color: #0e84b5; font-weight: bold\">JoeCrypt<\/span>\r\n{\r\n    <span style=\"color: #008800; font-weight: bold\">class<\/span> <span style=\"color: #BB0066; font-weight: bold\">Program<\/span>\r\n    {\r\n        <span style=\"color: #008800; font-weight: bold\">static<\/span> <span style=\"color: #008800; font-weight: bold\">void<\/span> <span style=\"color: #0066BB; font-weight: bold\">Main<\/span>(<span style=\"color: #333399; font-weight: bold\">string<\/span>[] args)\r\n        {\r\n            <span style=\"color: #008800; font-weight: bold\">if<\/span> (args.Length != <span style=\"color: #6600EE; font-weight: bold\">2<\/span>)\r\n            {\r\n                Console.WriteLine(<span style=\"background-color: #fff0f0\">&quot;Usage is &quot;<\/span> + System.AppDomain.CurrentDomain.FriendlyName + <span style=\"background-color: #fff0f0\">&quot; [file-to-crypt] [seed]&quot;<\/span>);\r\n                <span style=\"color: #008800; font-weight: bold\">return<\/span>;\r\n            }\r\n\r\n            <span style=\"color: #333399; font-weight: bold\">int<\/span> modval = Convert.ToInt32(args[<span style=\"color: #6600EE; font-weight: bold\">1<\/span>]);\r\n            <span style=\"color: #008800; font-weight: bold\">if<\/span> (modval &lt; <span style=\"color: #6600EE; font-weight: bold\">1<\/span>)\r\n            {\r\n                Console.WriteLine(<span style=\"background-color: #fff0f0\">&quot;Seed must be larger than 1&quot;<\/span>);\r\n                <span style=\"color: #008800; font-weight: bold\">return<\/span>;\r\n            }\r\n            \r\n            <span style=\"color: #008800; font-weight: bold\">if<\/span>(!File.Exists(args[<span style=\"color: #6600EE; font-weight: bold\">0<\/span>]))\r\n            {\r\n                Console.WriteLine(<span style=\"background-color: #fff0f0\">&quot;Could not find file &quot;<\/span> + args[<span style=\"color: #6600EE; font-weight: bold\">1<\/span>]);\r\n                <span style=\"color: #008800; font-weight: bold\">return<\/span>;\r\n            }\r\n            \r\n            <span style=\"color: #333399; font-weight: bold\">byte<\/span>[] buffer1 = File.ReadAllBytes(args[<span style=\"color: #6600EE; font-weight: bold\">0<\/span>]);\r\n            <span style=\"color: #333399; font-weight: bold\">byte<\/span>[] resultBuffer = <span style=\"color: #008800; font-weight: bold\">new<\/span> <span style=\"color: #333399; font-weight: bold\">byte<\/span>[buffer1.Length];\r\n\r\n            <span style=\"color: #008800; font-weight: bold\">for<\/span> (<span style=\"color: #333399; font-weight: bold\">int<\/span> i = <span style=\"color: #6600EE; font-weight: bold\">0<\/span>; i &lt; buffer1.Length; i++)\r\n            {\r\n                resultBuffer[i] = (<span style=\"color: #333399; font-weight: bold\">byte<\/span>)(buffer1[i] ^ ((buffer1.Length - i) % modval));\r\n            }\r\n\r\n            File.WriteAllBytes(args[<span style=\"color: #6600EE; font-weight: bold\">1<\/span>] + <span style=\"background-color: #fff0f0\">&quot;_encrypted&quot;<\/span>, resultBuffer);\r\n            Console.WriteLine(<span style=\"background-color: #fff0f0\">&quot;Encrypted file saved to &quot;<\/span> + Directory.GetCurrentDirectory() + <span style=\"background-color: #fff0f0\">&quot;\\\\&quot;<\/span> + args[<span style=\"color: #6600EE; font-weight: bold\">0<\/span>] + <span style=\"background-color: #fff0f0\">&quot;_encrypted&quot;<\/span>);\r\n            <span style=\"color: #008800; font-weight: bold\">return<\/span>;\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<\/div>\n<p>Running the app against an already encrypted file will decrypt it assuming you put in the right seed (key?). That said, I don&#8217;t need to specify a separate method \/ class for decryption.<\/p>\n<p>Some optimization comes to mind on speeding this up ie; chunking and multiple threads. Maybe some other time. <\/p>\n<p><a href=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2014\/09\/1407086268646.jpg\"><img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2014\/09\/1407086268646.jpg\" alt=\"1407086268646\" width=\"451\" height=\"800\" class=\"alignnone size-full wp-image-882\" srcset=\"https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2014\/09\/1407086268646.jpg 451w, https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2014\/09\/1407086268646-169x300.jpg 169w\" sizes=\"(max-width: 451px) 100vw, 451px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is how I spent my Friday night &#8211; I was sipping some delicious liquor and thinking about how crappy XOR encryption is unless the key length is high. Then I thought to myself &#8220;What if there was a dynamic xor key for each shift?&#8221;. Then I thought &#8220;I really should be out meeting the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[],"_links":{"self":[{"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/posts\/881"}],"collection":[{"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/comments?post=881"}],"version-history":[{"count":3,"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/posts\/881\/revisions"}],"predecessor-version":[{"id":885,"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/posts\/881\/revisions\/885"}],"wp:attachment":[{"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/media?parent=881"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/categories?post=881"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/tags?post=881"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}