*Author:Yuanhonglong
*Date:2013-12-20
*1948281915
*/
package mine.algorithm.key.Des_Rsa;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.UnrecoverableEntryException;
import java.security.spec.InvalidKeySpecException;
import java.util.Scanner;
import javax.crypto.KeyGenerator;
public class 生成密钥 extends 获得密钥信息{
static String path,pripath,pubpath;
static Scanner scanner=new Scanner(System.in);
static Key key;
static PrivateKey privatekey;
static PublicKey publickey;
static KeyPair mykeypair;
static FileOutputStream fos;
static File fi;
static String choose;
public static void main(String[] args) throws NoSuchAlgorithmException,IOException{
System.out.println("Enter algorithm:(Rsa,Aes or Des)");
choose=scanner.next();
create(choose);
}
public static void create(String algo) throws NoSuchAlgorithmException,IOException{
switch(algo){
case "Aes":
System.out.println("Enter path:");
path=scanner.next();
key=Aess(path);
break;
case "Des":
System.out.println("Enter path:");
path=scanner.next();
System.out.println("A init key or a new key will your password?(init/password)");
String xzxz=scanner.next();
switch(xzxz){
case "password":
System.out.println("Enter password:");
String str=scanner.next();
key=getKey(str,path);
break;
case "init":
key=Dess(path);
break;
default:
System.err.print("No this choice!");
System.exit(-1);
}http://www.huiyi8.com/jiaoben/
break; javascript特效
case "Rsa":
System.out.println("Enter privatepath:");
pripath=scanner.next();
System.out.println("Enter publicpath:");
pubpath=scanner.next();
Rsa(pripath,pubpath);
break;
default:
System.err.print("No this choice!");
System.exit(-1);
}
System.out.println("Do you want to get the informations of the key?(yes/no)");
String xuanzhe=scanner.next();
switch(xuanzhe){
case "yes":
switch(algo){
case "Aes":
try{
getinformation(choose,path);
}
catch(InvalidKeySpecException|UnrecoverableEntryException|KeyStoreException|ClassNotFoundException e){
}
break;
case "Des":
try{
getinformation(choose,path);
}
catch(InvalidKeySpecException|UnrecoverableEntryException|KeyStoreException|ClassNotFoundException e){
}
break;
case "Rsa":
try{
getinformation("private",pripath);
getinformation("public",pubpath);
}
catch(InvalidKeySpecException|UnrecoverableEntryException|KeyStoreException|ClassNotFoundException e){
}
break;
default:
System.err.print("No this choice!");
System.exit(-1);
}
case "no":
break;
default:
System.err.print("No this choice!");
System.exit(-1);
}
}
public static Key Aess(String path) throws NoSuchAlgorithmException,IOException{
KeyGenerator keyGenerator=KeyGenerator.getInstance("AES");
System.out.println("输入密钥长度:(128/256)");
Integer xx=null;
xx=scanner.nextInt();
keyGenerator.init(xx);
Key myKey=keyGenerator.generateKey();
// 生成密钥
System.out.println("得到AES密钥:"+myKey.getEncoded());
File fi=new File(path);
FileOutputStream fos=new FileOutputStream(fi);
fos.write(myKey.getEncoded());
try{
fos.close();
}
catch(IOException e){
}
System.out.println("AES密钥保存在:"+fi.getPath());
return myKey;
}
public static Key Dess(String path) throws NoSuchAlgorithmException,IOException{
KeyGenerator keyGenerator=KeyGenerator.getInstance("DES");
String x="9999991";
keyGenerator.init(new SecureRandom(x.getBytes()));
Key myKey=keyGenerator.generateKey();
System.out.println("得到DES密钥:"+myKey.getEncoded());
File fi=new File(path);
FileOutputStream fos=new FileOutputStream(fi);
fos.write(myKey.getEncoded());
fos.close();
System.out.println("DES密钥保存在:"+fi.getPath());
return myKey;
}
public static Key getKey(String strKey,String path) throws IOException{
KeyGenerator generator=null;
try{
generator=KeyGenerator.getInstance("DES");
}
catch(NoSuchAlgorithmException e){
}
generator.init(new SecureRandom(strKey.getBytes()));
Key key=generator.generateKey();
System.out.println("得到DES密钥:"+key);
File fi=new File(path);
FileOutputStream fos=new FileOutputStream(fi);
fos.write(key.getEncoded());
fos.close();
System.out.println("DES密钥保存在:"+fi.getPath());
return key;
}
public static void Rsa(String pripath,String pubpath) throws NoSuchAlgorithmException,IOException{
KeyPairGenerator keyPairGenerator=KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(1024);
mykeypair=keyPairGenerator.generateKeyPair();
privatekey=mykeypair.getPrivate();
publickey=mykeypair.getPublic();
fi=new File(pripath);
fos=new FileOutputStream(fi);
fos.write(privatekey.getEncoded());
System.out.println("私钥保存在:"+fi.getPath());
fos.close();
fi=new File(pubpath);
fos=new FileOutputStream(fi);
fos.write(publickey.getEncoded());
fos.close();
System.out.println("公钥保存在:"+fi.getPath());
}
安全加密