views:

69

answers:

3

Is it possible to do something like this with regular expressions: encode and decode a number?

For example I want to encode the id 15123 (which actually represents a port number in my case) into something which is useless to a user, for example a seemingly random string such as c95Dd7!7. And then decode it afterwards.

I want to do this with a regular expression because I will need the decoded id for URL Rewriting.

I know javascript packer does about the same... but I have no clue where to start. Or are there any out-of-the-box examples or solutions for my question?

Thanks!

+1  A: 

You won't be able to do this with a regular expression. You can do split or replace operations based on matches found with regular expressions. But that requires that you have direct access to the Regex Class (I assume you're working with .NET based on your past questions). And even then, that's a poor way to encode, scramble, or encrypt data.

I'd suggest a symmetric encryption algorithm. The one I've used most is Rijndael. Someone wrote a wrapper class to make working with Rijndael encryption super easy.

UPDATE: This is a very crude method. Again, regular expressions are very poorly suited to this sort of thing. But you could do something like this if you simply want to shift the order of the digits:

Regex:

(\d)(\d)(\d)(\d)(\d)

Replace with:

$1$4$3$5$2

Basically what that does is it captures each digit (\d) into a group. Then the replacement notation allows you to access each group by its numbered identifier (first group is $1, second is $2, and so on).

  • 12345 would become 14352
  • 15123 would become 12135

Here it is in action.

Steve Wortham
I don't need encryption, just some simple scrambling is good enough.
Gerrie Schenck
@Gerrie - OK, I updated my answer. It's a crude solution but maybe it'll help.
Steve Wortham
A: 

Since regex is really meant to match ranges and other states that encompass multiple matches, it's not really suited for reversible string transformations. I think the closest you'll be able to get with regex is character<->character mappings, which of course are much easier to do in other ways.

There are plenty of other ways of encrypting, why not use something more common?

Steve B.
RegEx is a requirement because I need to break the URL into capture groups to create back references for my URL Rewriting logic.
Gerrie Schenck
+1  A: 

Going along with what Steve B. mentioned.

My thought would be to do some simple encoding since it just appears you want to do a simple keep the user in the dark idea.

Maybe reverse the bytes and output to a string? Should be fairly unreadable but easier to convert.

confusedGeek
Yes, this could be what I'm looking for. Any clues how to achieve this? I'm not really an expert with RegEx...
Gerrie Schenck
@Gerrie, sorry the byte idea would only work in code. This isn't something RegEx was designed to do. It only does pattern matching. If you are limited to RegEx then @Steve Wortham already suggested my only idea.
confusedGeek