问题
How do I Generate AES key with seed value so that whenever I generate key with same seed value,I shud be able to get the same AES key?
I want to generate this key for my Blackberry Pearl 8100 device.
I am not able to generate AES key with AESKey(keyData) .
Also whenever I print it either in the form of String or byte[] , I am not able to generate it.(print it) Actual key is never printed.
What can be done to get the key?
Update
I tried generating AESKey by passing byte[] of my data,as follws:
AESKey key = new AESKey(keyData);
Everytime I get same key, which is correct.
Now using this key I am encrypting the data.Everytime I encrypt I get different value of encryption. Here is my problem. How can I get same data everytime after encryption,so that I can send it to server?
Update
I am not able to generate AES key with AESKey(keyData) .
Also whenever I print it either in the form of String or byte[] , I am not able to generate it.(print it) Actual key is never printed.
What can be done to get the key?
回答1:
Section 5.2 in RFC 2898 describes one way of doing this.
Specifically, the "seed" value consists of a "PASSPHRASE" and a "SALT"; but given the same passphrase and salt, will always result in the same key.
http://anandam.name/pbkdf2/ is a javascript implementation.
http://en.wikipedia.org/wiki/PBKDF2 has more information, and links to other implementations.
回答2:
This can not be done in a secure manner. You should not be generating encryption keys like this, particularly if you intend to protect anything important with the resulting keys. Nonetheless, a basic algorithm to do it looks like this (many enhancements are possible, but none will make it really secure):
- Choose a random number generator; probably Java has one built in that people usually use.
- Initialize (seed) the random number generator with a specific input value (passphrase? hash of passphrase? something like that).
- Take the first
N
bytes of RNG output; those are your encryption key. As long as you seed with the same value in step 2, the firstN
bytes generated will always be the same. - Reseed the RNG with a different value, preferably a random one (in Python you would seed with
None
, for instance; that tells the machine to choose any random seed).
回答3:
I need a bit more info, so this is a guess.
How are you using your AESKey? If you're doing things using CBC Encryption (i.e. with the net.rim.device.api.crypto.CBCEncryptorEngine) then your initialization vector has to be the same for each invocation too. Use the constructor for CBCEncryptorEngine that takes an InitializationVector as well as a BlockEncryptorEngine.
Wikipedia article with more info on why this is so.
来源:https://stackoverflow.com/questions/861679/generate-aes-key-with-seed-value