java - How to use Hash of a passphares to encrypt -


so basically. want use hash of passphrase encrypt masterkey. moment code used this

try {         //conservazione delle chiavi!         //secure prng         securerandom m = securerandom.getinstance("sha1prng");         //secure hash         messagedigest hash = messagedigest.getinstance("sha-1");         //keygenerator          keygenerator keygenerator = keygenerator.getinstance("des");         keygenerator.init(m);         //want obtain random masterkey need encrypt         key key = keygenerator.generatekey();         //get des cipher         cipher cipher = cipher.getinstance("des");         //and now?                     cipher.init(cipher.encrypt_mode, key); // that's problem.        } catch (nosuchalgorithmexception e) {         // todo auto-generated catch block         e.printstacktrace();     } catch (nosuchpaddingexception e) {         // todo auto-generated catch block         e.printstacktrace();     } 

the key hash of passphrase..
hash.digest(passphrase.getbytes());
passpharse simple string.
suggest how use hash key?
i should init keygenerator hash of passphrase?

the direct answer question use deskeyspec , keyfactory using algorithm string "des" getinstance(string) method (just keygenerator. wrap right amount of bytes front (leftmost) part of byte array containing hash, wrap in deskeyspec , use secretkeyfactory.generatesecret() method create key it.

indirectly, should not use "des" anymore. instead password based encryption, aes, cbc , key wrapping. make sure don't use code above in production environment before upgrade.


Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -