开发者

Is this possible with RegEx: encoding/scrambling an id?

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 seem开发者_开发问答ingly 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!


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.


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.


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜