How to create Java Key Store (.jks) file with AES encryption

后端 未结 4 1338
栀梦
栀梦 2021-02-03 16:12

Reading Oracle documentation, I see that by default JKS files are encrypted using PBEWithMD5AndTripleDES. While DES alone makes me feel uneasy, MD5 lights a big red

4条回答
  •  隐瞒了意图╮
    2021-02-03 17:03

    Main Class

    `public class a {
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception {
        // TODO code application logic here
        String b = "MSISDN=559915129&productID=5859";
    
        AEScryptography enj = new AEScryptography();
        String[] argts = {b, "EN"};
        System.out.println("ENCY -> "+enj.encryptionDecryption(argts));
        System.out.println(checksum.encode(b));
    } }`
    

    AEScryptography Class

    `public class AEScryptography {
    public String encryptionDecryption(String[] args) throws UnsupportedEncodingException, NoSuchProviderException, NoSuchPaddingException, InvalidKeyException, ShortBufferException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException {
        String returnVariable = "";
        if (args.length == 2) {
            try {
                String keystorePass = "201774";  // KeyStroe Password
                String keyPass = "mc7129";       // KeyPassword 
                String alias = "raVi";           // Alias
    
            InputStream keystoreStream = new FileInputStream("D:/keyFile.jks");
                KeyStore keystore = KeyStore.getInstance("JCEKS");
                keystore.load(keystoreStream, keystorePass.toCharArray());
                Key key = keystore.getKey(alias, keyPass.toCharArray());
                byte[] bt = key.getEncoded();
                String s = new String(bt);
                String originalString = args[0];
                switch (args[1]) {
                    case "EN":
                        String encryptedString = AES.encrypt(originalString, s, bt);
                        returnVariable = encryptedString;
                        break;
                    case "DE":
                        String decryptedString = AES.decrypt(originalString, s, bt);
                        returnVariable = decryptedString;
                        break;
                    default:
                        System.out.println("java -jar /home/jocg/AES-256Encryption.jar StringToEncrypt/Decrypt EN/DE");
                        break;
                }
    
            } catch (KeyStoreException | IOException | NoSuchAlgorithmException | CertificateException | UnrecoverableKeyException ex) {
                System.out.println(ex);
    
            }
        } else {
            System.out.println("java -jar /home/jocg/AES-256Encryption.jar StringToEncrypt/Decrypt EN/DE");
        }
        return returnVariable;
    } }`
    

    AES Class

     public class AES {
    private static SecretKeySpec secretKey;
    private static byte[] key;
    public static void setKey(String myKey, byte[] ActualKey) throws 
    NoSuchAlgorithmException {
        try {
            key = myKey.getBytes("UTF-8");
            key = ActualKey;
            secretKey = new SecretKeySpec(key, "AES");
        } catch (UnsupportedEncodingException e) {
        }
    }
    
    public static String encrypt(String strToEncrypt, String secret, byte[] bt) throws UnsupportedEncodingException, NoSuchProviderException, InvalidAlgorithmParameterException {
        try {
            setKey(secret, bt);
            byte[] iv = new byte[16];
            IvParameterSpec ivspec = new IvParameterSpec(iv);
            SecretKey secKey = new SecretKeySpec(key, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, secKey, ivspec);
            byte[] newData = cipher.doFinal(strToEncrypt.getBytes("UTF-8"));
            return Base64.getEncoder().encodeToString(newData);
        } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) {
            System.out.println("Error while encrypting: " + e.toString());
        }
        return null;
    }
     }`
    

提交回复
热议问题