简体   繁体   中英

How to store the autogenerated secret key in a text file,

How do i store the generated key a text file at a particular location. I am trying to store the key and decrypt when required for the particular user. I am using two different java class one for encrypting and decryption, i need the store the particular key use to encrypt the user else i am getting illegal block size, not able to decrypt the data in the file, the data gets deleted.

SecretKey secKey = keyGen.generateKey();

System.out.println(secKey);
String KeyStore = "C:\\keystore.txt";
FileOutputStream fout=new FileOutputStream(KeyStore);  


byte[] b=secKey.getBytes();//this is not working for me 
fout.write(b);  
fout.close(); 

help me out this there another option to store the key for a particular use

All the Above answers are nonsense, being java developers we all should know that there is a function called:

Java KeyStore to store the autogenerated keys Here is the code, I figured out on my own. This Code is just small piece i have posted not complete code. Just to give you guys an Idea

 final String keyStoreFile = "C:\\mykey.jceks";// Storing the Key in a Particulary.
 KeyStore keyStore = createKeyStore(keyStoreFile, "java143");
 System.out.println("Stored Key: " + (secKey));
 System.out.println("secured key: " + (keyStore));


 // store the secret key
 KeyStore.SecretKeyEntry keyStoreEntry = new KeyStore.SecretKeyEntry(secKey);
 //Padding for password
 PasswordProtection keyPassword = new PasswordProtection("pw-secret".toCharArray()); 

 keyStore.setEntry("mySecret", keyStoreEntry, keyPassword);
 keyStore.store(new FileOutputStream(keyStoreFile), "java143".toCharArray());

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