安全加密

 *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);
                }
                break;
            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);
        }http://www.huiyi8.com/jiaoben/   网页特效代码
        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());
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值