cryptography - How to encode and decode SecretKey in JAVA? -


keystore ks = keystore.getinstance("jceks"); ks.load(null, null); secretkey skinput = new secretkeyspec("input".getbytes(), "desede"); secretkeyentry skeinput = new keystore.secretkeyentry(skinput); ks.setentry("input_key", skeinput, new keystore.passwordprotection("banana".tochararray())); fileoutputstream fos = new fileoutputstream("my.keystore"); pambks.store(fos, "password".tochararray()); fos.flush(); fos.close(); 

the code shown above trying encode input secretkey , store keystore. code shown below how retrieve key keystore. not know how decode original value?

fileinputstream fin = new fileinputstream("my.keystore"); keystore ks = keystore.getinstance("jceks"); ks.load(fin, "password".tochararray()); secretkeyentry ske = (secretkeyentry) readks.getentry("input_key", new keystore.passwordprotection("banana".tochararray())); secretkey sk = ske.getsecretkey(); 

i wasn't sure whether correct way of doing encryption , decryption secretkey, please correct me if i'm wrong.

reading q/a think i've misread question.

you can byte representation of key calling getencoded method on key. after possible use 1 of string constructors revert text. said, should not use string key. note des keys contain parity in last bit, may change result. use strings keys more advisable use hexadecimals. note keys should have enough entropy, , string not provide that.


there several things not entirely correct in code above:

  1. you should not (ever) use "input".getbytes(). first of all, getbytes() uses platform specific default character encoding. if want use des key string, decode decoder of hexadecimals (e.g. in apache commons codec or bouncy castle).

  2. you should use 24 bytes desede keys. "input".getbytes() not return enough bytes.

  3. for des keys pays put key specification through keyfactory if make sure parity bits set. sure key valid.

to amount of key data, use pbkdf2 passwords or use randomly generated keys.

if encrypted key store enough storage encrypted key depends on use case , other security measures.


Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

c# - Resource not found error -