简体   繁体   English

使用另一个字符串生成32位字符串?

[英]Generate a 32bit String using another String?

I want to generate a 32bit String using another String in my application. 我想在我的应用程序中使用另一个String生成一个32bit的String。 How could I do this , I have a String like this 64459003-1a63-4b3b-b649-ffab8433806b. 我该怎么做,我有一个像64459003-1a63-4b3b-b649-ffab8433806b这样的字符串。 I need to generate a 32 bit String using a PassKey like a Password for the String. 我需要使用PassKey生成32位字符串,例如字符串的密码。 I am a newbie Please help? 我是新手,请帮忙吗?

Note:I want an algorithm which could regenerate the same string if called later? 注意:我想要一个可以在以后调用时重新生成相同字符串的算法?

What I Actually Want: I want to generate a Encrypted or Some type of 32Bit String From UUID of the android Device and use it for the activation of my app.The user will send the tech support the generated String and Support will send back a Activation Id to Activate the App. 我真正想要的是:我想从android设备的UUID生成一个加密的或某种类型的32Bit字符串,并将其用于激活我的应用程序。用户将向技术支持发送生成的字符串,技术支持将回传激活码激活应用程序的ID。

Yes I want it to be reversed also! 是的,我也希望它能逆转!

A String with 32-bits of state can only represent 2^32 states. 状态为32位的字符串只能表示2^32个状态。 But in order for the reverse transformation to work, you been to be able to represent significantly more state than that. 但是为了使反向转换起作用,您必须能够表现出更多的状态。 (The input string looks like the String representation of a 128 bit UUID. That implies 2^128 different states.) (输入字符串看起来像是128位UUID的String表示形式。这意味着2^128不同的状态。)

The best you could do is to implement a persistent lookup table. 您能做的最好的事情就是实现一个持久的查找表。 And even that would break if you have more that 2^32 pass-keys. 如果您拥有2^32通行键,即使那样也将中断。

okay then assuming you will have the password and the previous string in the database. 好的,那么假设您将拥有数据库中的密码和上一个字符串。 You can use random class of java. 您可以使用Java的随机类。
generate an initial string : 生成一个初始字符串:

//initially
Random rand=new Random();
byte[4] a=new byte[4];
rand.nextBytes(a);
//a will contain the 32 bit string
//store a in database
//Next time
Random b=new Random();
//convert tehe previous value to long
long value=0;
for(int i=0;i<b.length;i++)
 {
   value+=(b[i] & 0xff) <<(8*i);
   //System.out.println(Long.toString(value));
 }
b.setSeed(value);
//using this seed value generate a new random number
b.nextbytes(b);
//store this b to data base


Note: You will have to store the initial valuein the databse in aa seperate column. 注意:您必须将初始值存储在数据库的单独列中。 But the subsequent values can be replaced. 但是后续值可以替换。
To retrive the oiginal string you will have to look up the data base. 要检索原始字符串,您将必须查找数据库。 From the initial seed, the rest of the values can be found easily(by calling setSeed()). 从初始种子中,可以轻松找到其余值(通过调用setSeed())。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM