content: /*
   * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
   * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
   */
  package com.eugc;
  
  import java.security.InvalidAlgorithmParameterException;
  import java.security.InvalidKeyException;
  import java.security.NoSuchAlgorithmException;
  import java.security.SecureRandom;
  import java.util.Base64;
  import javax.crypto.BadPaddingException;
  
  import javax.crypto.Cipher;
  import javax.crypto.IllegalBlockSizeException;
  import javax.crypto.KeyGenerator;
  import javax.crypto.NoSuchPaddingException;
  import javax.crypto.SecretKey;
  import javax.crypto.SecretKeyFactory;
  import javax.crypto.spec.IvParameterSpec;
  
  
  /**
   *
   * @author 520
   */
  public class encryption {
  private static SecretKey encKey;
  private static IvParameterSpec encIv;
  private static String algorithm = "AES/CBC/PKCS5Padding";
  private static String encryptedPassword;
  
  public encryption(String password) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException{
      encKey=generateKey(512);
      encIv=generateIv();
      encrypt(password);
    
  }
      
  private static SecretKey generateKey(int n) throws NoSuchAlgorithmException {
      KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
      keyGenerator.init(n);
      SecretKey key = keyGenerator.generateKey();
      return key;
  }
  private static IvParameterSpec generateIv() {
      byte[] iv = new byte[16];
      new SecureRandom().nextBytes(iv);
      return new IvParameterSpec(iv);
  }
  private static void encrypt(String input) throws NoSuchPaddingException, NoSuchAlgorithmException,
      InvalidAlgorithmParameterException, InvalidKeyException,
      BadPaddingException, IllegalBlockSizeException {
      
      Cipher cipher = Cipher.getInstance(algorithm);
      cipher.init(Cipher.ENCRYPT_MODE, encKey, encIv);
      byte[] cipherText = cipher.doFinal(input.getBytes());
      encryptedPassword=Base64.getEncoder()
          .encodeToString(cipherText);
  }
  
  public static String decrypt() throws NoSuchPaddingException, NoSuchAlgorithmException,
      InvalidAlgorithmParameterException, InvalidKeyException,
      BadPaddingException, IllegalBlockSizeException {
      
      Cipher cipher = Cipher.getInstance(algorithm);
      cipher.init(Cipher.DECRYPT_MODE, encKey, encIv);
      byte[] plainText = cipher.doFinal(Base64.getDecoder()
          .decode(encryptedPassword));
      return new String(plainText);
  }
  }
  
  
  