2

I'm hoping to figure out a simple search and replace in Notepad++ to slightly obfuscate text by littering it with random letters and numbers every second ("other") character, and then be able to reverse that again with another macro.

So:

banana

would become:

bma0ndaNn4aR

(b?a?n?a?n?a?)

...And then be able to undo this again by removing every other character with a backspace.

...

I found this method so far:

(?<=.)(?!$)

How to insert spaces between characters using Regex?

But as best I understand, this is not actually capturing anything so I can't use this to replace with expressions I've found for printing random letters and numbers, such as:

^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])$

I'm sure a tweak to that would work and then I could reverse it all by replacing the same search with \b.

Allan
  • 12,117
  • 3
  • 27
  • 51
LDAsh
  • 53
  • 1
  • 6
  • Using regex to search for and capture every character is the easy part. The difficult part is going to be using a search/replace to insert random characters. –  Feb 15 '19 at 00:45
  • btw, just search for a single character, and use that whole match in the replace expression as a backreference. I don't know notepad++'s syntax, but it would be something like substitute `.` with `\1C` where `C` is the random character. Regexes can be used to generate random strings that match, but i don't know of a simple search/replace system that can do that. (I don't know what that last regex expression is for, but I'm pretty sure it won't do anything like what you are hoping...) You may have to look into other options like an external script you can run on the file. –  Feb 15 '19 at 00:49
  • So basically with Notepad++ Regex is for searching, but replacing can use "extended" at most? Should I be looking into using a Python script? (should I edit this post or ask a new question???) – LDAsh Feb 15 '19 at 01:29
  • Yes, I think a python script would be the way to go for this use case. I'd also recommend reading up a bit on how regex matching works - it's a dark art and copy/pasting examples without understanding what each character does will end badly. good luck. :) –  Feb 15 '19 at 01:32

1 Answers1

1

There are better ways of doing but you can use the following python prototype as a starting point to create your own script:

import string
import random

inputText = 'banana'

#encoding 
obfuscatedText = ''.join([x + random.choice(string.ascii_letters+string.digits) for x in inputText])
print(obfuscatedText)

#decoding
originalText = ''.join([x for x in obfuscatedText][0:len(obfuscatedText)-1:2])
print(originalText)

Explanations:

Encoding:

  • [x for x in inputText] will generate an array of chars from the input string
  • random.choice(string.ascii_letters+string.digits) takes one character from the union of string.ascii_letters and string.digits
  • x + random.choice(string.ascii_letters+string.digits) create 2 char strings by concatenating each char of the input with the generated char.
  • The ''.join() operation will allow you to create a string from the char array

Decoding:

  • [x for x in obfuscatedText][0:len(obfuscatedText)-1:2] will allow you to get only the
  • char that are located at index 0,2,4,6,...
  • the ''.join() operation will regenerate a string from the char array

Execution:

$ python obfuscate.py 
biaLncaIn4aE
banana
Allan
  • 12,117
  • 3
  • 27
  • 51
  • Thanks! I was actually hoping for something I could put into Notepad++ Python plugin that would work on any text open in there. This seems to want the text put into the PY file which isn't very handy for the purpose, or is there a way to have "inputText" just handle whatever is open? – LDAsh Feb 15 '19 at 04:27
  • @LDAsh: what about changing this script to read a file and put it in the variable before doing the conversion? – Allan Feb 15 '19 at 05:17
  • @LDAsh: http://npppythonscript.sourceforge.net/docs/latest/ and http://npppythonscript.sourceforge.net/ to adapt my script to your notepad++, I do not have a Windows machine so I can't really test it sorry – Allan Feb 15 '19 at 05:19