{"id":271,"date":"2024-03-12T16:03:09","date_gmt":"2024-03-12T16:03:09","guid":{"rendered":"https:\/\/offensivepython.com\/?p=271"},"modified":"2024-03-18T15:06:33","modified_gmt":"2024-03-18T15:06:33","slug":"obfuscation-with-enumerate-and-xor","status":"publish","type":"post","link":"https:\/\/offensivepython.com\/index.php\/2024\/03\/12\/obfuscation-with-enumerate-and-xor\/","title":{"rendered":"Obfuscation with enumerate and XOR"},"content":{"rendered":"\t\t<div data-elementor-type=\"wp-post\" data-elementor-id=\"271\" class=\"elementor elementor-271\">\n\t\t\t\t<div class=\"elementor-element elementor-element-3c42c8af e-flex e-con-boxed e-con e-parent\" data-id=\"3c42c8af\" data-element_type=\"container\" data-e-type=\"container\">\n\t\t\t\t\t<div class=\"e-con-inner\">\n\t\t\t\t<div class=\"elementor-element elementor-element-b440f9e elementor-widget elementor-widget-text-editor\" data-id=\"b440f9e\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p><\/p>\n<p>\u00a0 \u00a0 \u00a0The enumerate function can be used to XOR characters of text with characters of a key. The example below shows how this can be done. In my example, my text length matches my key length.<\/p>\n<p><span style=\"text-decoration: underline;\">Example code:<\/span><\/p>\n<p><\/p>\n<p><strong>obfuscated = &#8220;&#8221;<\/strong><br \/><strong>clear_text = &#8220;&#8221;<\/strong><br \/><strong>text = &#8220;Something to obfuscate.&#8221;<\/strong><br \/><strong>key = &#8220;a key of some sort here&#8221;<\/strong><\/p>\n<p># Enumerate our text to get the position of each character and the character itself<strong><br \/>for index, char in enumerate(text):<\/strong><\/p>\n<p>\u00a0 \u00a0 # Use the ord function to obtain unicode codes for the text and key characters<strong><br \/><\/strong>\u00a0 \u00a0 # XOR those together, and then use the chr function to return the resultant character<br \/>\u00a0 \u00a0 # Keep appending characters to obfuscated variable<strong><br \/>\u00a0 \u00a0 obfuscated += chr(ord(char) ^ ord(key[index]))<\/strong><\/p>\n<p># Print a UTF-8 encoded result<br \/><strong>print(&#8220;Obfuscated text is:&#8221;, obfuscated.encode())<\/strong><\/p>\n<p># To undo it, enumerate the obfuscated text<br \/><strong>for index, char in enumerate(obfuscated):<\/strong><\/p>\n<p>\u00a0 \u00a0 # Do the same steps as before, except this time append to clear_text variable<br \/><strong>\u00a0 \u00a0 clear_text += chr(ord(char) ^ ord(key[index]))<\/strong><\/p>\n<p># Print our clear text to the screen<br \/><strong>print(&#8220;Original text was: &#8220;, clear_text)<\/strong><\/p>\n<p><span style=\"text-decoration: underline;\">Output:<\/span><\/p>\n<p>Obfuscated text is: b&#8217;2O\\x06\\x00\\rH\\x06\\x08GS\\x1b\\x02EO\\x11\\t\\x07\\x07C\\t\\x11\\x17K&#8217;<br \/>Original text was: Something to obfuscate.<\/p>\n<p><\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t","protected":false},"excerpt":{"rendered":"<p>\u00a0 \u00a0 \u00a0The enumerate function can be used to XOR characters of text with characters of a key. The example below shows how this can be done. In my example, my text length matches my key length. Example code: obfuscated = &#8220;&#8221;clear_text = &#8220;&#8221;text = &#8220;Something to obfuscate.&#8221;key = &#8220;a key of some sort here&#8221; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-container-style":"default","site-container-layout":"default","site-sidebar-layout":"default","site-transparent-header":"default","disable-article-header":"default","disable-site-header":"default","disable-site-footer":"default","disable-content-area-spacing":"default","footnotes":""},"categories":[16],"tags":[31],"class_list":["post-271","post","type-post","status-publish","format-standard","hentry","category-obfuscation","tag-xor"],"_links":{"self":[{"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/posts\/271","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/comments?post=271"}],"version-history":[{"count":31,"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/posts\/271\/revisions"}],"predecessor-version":[{"id":327,"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/posts\/271\/revisions\/327"}],"wp:attachment":[{"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/media?parent=271"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/categories?post=271"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/tags?post=271"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}