Created
March 2, 2018 08:03
-
-
Save zhanggang807/176fe802bec23a26507df6cad045fbe6 to your computer and use it in GitHub Desktop.
key store of java security
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.crypto.KeyGenerator; | |
import javax.crypto.SecretKey; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.security.KeyStore; | |
import java.security.KeyStoreException; | |
import java.security.NoSuchAlgorithmException; | |
import java.security.cert.CertificateException; | |
public class KeyStoreTest { | |
public static void main(String[] args) { | |
try { | |
secretKeyStore(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
public static void secretKeyStore() throws KeyStoreException, NoSuchAlgorithmException, | |
CertificateException, IOException { | |
char[] password = "123456".toCharArray(); | |
String fileName = System.getProperty("user.home") + File.separator + ".keystore"; | |
FileInputStream fis = new FileInputStream(fileName); | |
KeyStore ks = KeyStore.getInstance("jceks"); | |
ks.load(fis, password); | |
KeyGenerator kg = KeyGenerator.getInstance("DES"); | |
SecretKey key = kg.generateKey(); | |
ks.setKeyEntry("myKeyEntry", key, password, null); | |
FileOutputStream fos = new FileOutputStream(fileName); | |
ks.store(fos, password); | |
System.out.println("store key in " + fileName); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment