The Usual Tech Ramblings

Looping Directories, and Bad "Encryption"

I have a terrible memory, which is why I’ve started keeping this blog, so I can remind myself on how I’ve fixed stuff, and if somebody else finds it useful, then an added bonus. As I mentioned in my previous post, I mentioned that I was cleaning up our images server. This lead to a simple PHP script, which processed the output of dir /b /A-D /S, however, this produced a huge text file (1.2 GB to be more precise). So I decided to break it down.

This break down involved taking the directory structures, and scanning each directory individually. I did start to do this by hand, but soon realized it’d probably take me hours to complete. This is where scripts come in handy. They make repetitive tasks easy. So I set out reminding myself how to do a loop on directory structures in windows.

Again, this is something that’d be really easy for linux, using the find command, you could do:

find . -type d

Windows isn’t too bad either, it ends up being something like this:

for /f %n in ('dir /b /AD') do {somestuff here}

I’d already figured out my somestuff, which was dir /b /A-D /S dirname > \img_clean\dirname.txt. So combined together, I ended up with:

for /f %n in ('dir /b /AD') do dir /b /A-D /S %n > \img_clean\%n.txt

This produced 3000+ small text files, containing the contents of the folder, and it’s sub-folders.

And why is the subject also including Bad “Encryption”? Well, in my search to remind myself on how to do loops on directories in MS-DOS, I stumbled across an Experts-Exchange posting. I’ve used the site for years, but recently they changed their policy in that you cannot see any answers. This is a pain. They’ve decided to mask the answers with a blurred image. Thinking that this was about the extent they went to, I viewed the page source, and realized they’d encrypted the answers too. I say encrypted, because I could make out some familiar formatting from memory. This is when I wondered if they used a strong encryption, or a simple cipher. Noticing they included special characters (/ * ‘ etc) as they were, this hinted at a simple cipher, so I started to dig a little more.

The first thing I observed was the article wanted to call java, and execute it against any *.js files in the directory. The first answer looked like this:

sbe /s %%n va ('qve /o *.wf') qb wnin -wne "%%n" $vaSvyr $bhgSvyr

I took a stab in the dark, and assume *.wf was really supposed to be *.js. I guessed that it might have been Caeser Shift, and popped open Excel to do some simple mapping of letters. Under the column j, I put the W, and under s, I put F. I then started to fill in the rest, t became g, u became h, and so on. It didn’t take too long to figure out that the above ended up being:

for /f %%n in ('dir /b *.js') do java -jar "%%n" $infile $outfile

While I dislike Experts-Exchange policy on masking all answers, they do so for their own survival. I’m guessing server, and bandwidth usages alone are pretty high. However, the fact that it only took me 2 minutes to decode their encrypted answers means they really weren’t taking the time. If I were them, I’d probably just remove the answers, and put in a “more answers after you sign up” kind of link.