简体   繁体   中英

Generate a 32bit String using another String?

I want to generate a 32bit String using another String in my application. How could I do this , I have a String like this 64459003-1a63-4b3b-b649-ffab8433806b. I need to generate a 32 bit String using a PassKey like a Password for the String. 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.

Yes I want it to be reversed also!

A String with 32-bits of state can only represent 2^32 states. 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.)

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.

okay then assuming you will have the password and the previous string in the database. You can use random class of 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()).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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