Java/Security/Keytool

Материал из Java эксперт
Перейти к: навигация, поиск

Creating a New Key Pair and Self-Signed Certificate Using keytool

   <source lang="java">
  

keytool -genkey -alias alias -keystore .keystore


 </source>
   
  
 
  



Export certificate in binary using keytool, if the certificate is in the key store

   <source lang="java">
  

keytool -storepass my-keystore-password -alias myalias -export -file outfilename.cer


 </source>
   
  
 
  



Export certificate in text format using keytool, if the certificate is in the key store

   <source lang="java">
  

keytool -storepass my-keystore-password -alias myalias -export -rfc -file outfilename.cer


 </source>
   
  
 
  



generates a 1024-bit Digital Signature Algorithm (DSA) key pair.

   <source lang="java">
  

keytool -genkey -alias alias -keystore .keystore


 </source>
   
  
 
  



Java Key Tools

   <source lang="java">
 

import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; import java.security.Key; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.SecureRandom; public class KeyTools {

 public static void writeToFile(Key key, File file) throws IOException {
   FileOutputStream fileoutputstream = new FileOutputStream(file);
   ObjectOutputStream objectoutputstream = new ObjectOutputStream(fileoutputstream);
   objectoutputstream.writeObject(key);
   objectoutputstream.close();
 }
 public static void main(String[] rgstring) {
   try {
     File filePublic = new File(rgstring[0]);
     File filePrivate = new File(rgstring[1]);
     KeyPairGenerator keypairgenerator = KeyPairGenerator.getInstance("DSA");
     keypairgenerator.initialize(1024, new SecureRandom());
     KeyPair keypair = keypairgenerator.generateKeyPair();
     
     writeToFile(keypair.getPublic(), filePublic);
     writeToFile(keypair.getPrivate(), filePrivate);
   } catch (Exception ex) {
     ex.printStackTrace();
   }
 }

}


 </source>
   
  
 
  



To create a 1024-bit RSA key:

   <source lang="java">
  

keytool -genkey -keyalg RSA -keysize 1024 -alias alias -keystore .keystore


 </source>
   
  
 
  



using keytool to import a certificate into a keystore

   <source lang="java">
  

keytool -storepass my-keystore-password -alias myalias -import -file infilename.cer


 </source>