java中的long to String(和back)的简单对称加密 [英] simple symmetric encryption of long to String (and back) in java

查看:141
本文介绍了java中的long to String(和back)的简单对称加密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种简单的方法来将long转换为String,并以隐藏长期值的方式。

I'm looking for a simple way to translate long to String and back in a way that will "hide" the long value.

我更喜欢避免为该功能的项目添加另一个.jar。

I'd prefer to avoid adding another .jar to the project for this feature.

它不一定是一个难以破解的加密,只是为了没有经验的眼睛看起来随机。

It does not have to be a hard-to-crack encryption, just to look random to the inexperienced eye.

添加:

我的目的是将一个计数器值(long类型)附加到URL跟踪参数,而用户不知道计数器的值(类似于tinyURL的哈希),以便servlet在URL被点击时会知道计数器的值。

My purpose here is to attach a counter value (of type long) to URLs as a tracking parameter without the users aware of the counter's value (sort of like tinyURL's hash), so that the servlet will know the value of the counter when the URL is clicked.

谢谢

推荐答案

如果

X * Y = 1 (mod 2^32)

然后

A * (X * Y) = A (mod 2^32)
(A * X) * Y = A (mod 2^32)

所以,你可以通过将它乘以X来加密一些32位数字,然后再加密解密所有你需要的是找到满足条件的一些不平凡的X和Y。

So, you can "encrypt" some 32-bit number by multiplying it by X, and then later "decrypt" by multiplying by Y. All you need is to find some non-trivial X and Y that satisfy the condition.

例如,(X,Y)=(3766475841,1614427073 )或(699185821,3766459317)。我只是用一个简单的暴力程序发现这些。

For instance, (X, Y) = (3766475841, 1614427073), or (699185821, 3766459317). I just found these with a simple brute force program.

一旦你有A * X,你可以使用Base-64或十六进制或一些类似的方案在URL中进行编码。我建议使用Base64,因为它占用的空间较小,看起来相当随机。

Once you have A*X you can encode it using Base-64 or hexadecimal or some similar scheme in the URL. I'd suggest Base64 because it takes up less space and looks fairly "random".

这篇关于java中的long to String(和back)的简单对称加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆