将KeyStore和Cipher用于用户控制的程序

时间:2018-05-24 18:48:00

标签: java encryption keystore

我目前是加密的新手并在Java中使用它所以我决定使用非对称方法(密钥)和AES来制作加密/解密数据的程序。现在我只有在密钥库实例中创建和存储密钥的类。这是我的代码:

import java.security.*;
import java.util.Scanner;
import javax.crypto.*;

public class KeyGen
{

public KeyGen()
{
}

public void createKey()
{
    try
    {
        System.out.println("Initializing...");
        KeyGenerator keyG = KeyGenerator.getInstance("AES"); //Creates instance of KeyGenerator using algorithm "AES"
        int keyBitSize = 128; //Key bit size
        keyG.init(keyBitSize); //Initializes keyG

        System.out.println("Generating...");

        SecretKey secKey = keyG.generateKey();
        System.out.println("Finishing...");

        //Creates keystore, gets and creates password array
        KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
        System.out.println("Enter Password For Key: ");
        Scanner in = new Scanner(System.in);
        String pass = in.next();
        char[] keyPassword = pass.toCharArray();

        //Initializes keystore
        int choice = 0;
        Scanner in2 = new Scanner(System.in);
        while (!(choice == 1) && !(choice == 2))
        {
            System.out.println("Create a new KeyStore or access another? (1/2)");
            try
            {
                choice = in2.nextInt();
            }
            catch (Exception e)
            {
            }

        }
        if (choice == 1)
        {
            java.io.FileInputStream fis = null;
            try
            {
                fis = new java.io.FileInputStream("keyStoreName");
            }
            finally
            {
                if (fis != null)
                {
                    fis.close();
                }
            }
            keystore.load(fis, keyPassword);
            System.out.println("Keystore created. Stored: False");
        }
        else if (choice == 2)
        {
            System.out.println("Enter keyStore name to load: ");
            String keyStoreName = in.next();
            java.io.FileInputStream fis = null;
            try
            {
                fis = new java.io.FileInputStream(keyStoreName);
                keystore.load(fis, keyPassword);

            }
            finally
            {
                if (fis != null)
                {
                    fis.close();
                }
            }
            System.out.println("Keystore loaded. Stored: False");
        }

        //Adds the Key to the Keystore
        KeyStore.SecretKeyEntry secretKeyEntry = new KeyStore.SecretKeyEntry(secKey);
        KeyStore.ProtectionParameter entryPassword = new KeyStore.PasswordProtection(keyPassword); //Sets pass to protParam for the keystore
        System.out.println("Please enter your key alias: ");
        String alias = in.nextLine();
        keystore.setEntry(alias, secretKeyEntry, entryPassword); //adds key as entry to the keystore
        System.out.println("Enter keyStore name to save as: ");
        String newKeyStoreName = in.nextLine();
        java.io.FileOutputStream fos = null;
        try
        {
            fos = new java.io.FileOutputStream(newKeyStoreName);
            keystore.store(fos, keyPassword);
        }
        finally
        {
            if (fos != null)
            {
                fos.close();
            }
        }
        System.out.println("KeyStore successfully stored. Key Alias: " + alias);
    }
    catch (Exception e)
    {

    }

}
}

我不确定我在使用Keystore方面是否完全有效,但这是我的主要问题:我如何使用它来解密/加密外部类中的数据?我不知道如何使用密码,所以如果有人可以帮助我尝试解决这个问题,我将不胜感激。看documentation我可以理解getInstance,但除此之外的一切对我来说都是一个谜。在这种情况下如何使用它。

0 个答案:

没有答案