package admin.cryptor;

import moduly.decrypt.DecruptStr;

import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) throws Exception {
        System.out.println("[1] - рандомный ключ\n[2] - свой ключ\n[3] - крипт настроек с рандомным ключом");
        Scanner sc = new Scanner(System.in);
        String otv = sc.nextLine();
        if (otv.equals("1")) {
            String key = generateString();
            ShiperInter shiperKey = new ShiperKey(key);
            Crupt crupt = new Crupt();
            System.out.println("рандомный ключ -> " + key);
            System.out.println("Введите строку для шифровки:");
            Scanner sc1 = new Scanner(System.in);

            System.out.println("шифрованая строка AES256 -> " + crupt.cryptor(shiperKey.getAlgorithm(), sc1.nextLine(), shiperKey.getSkeySpec(), shiperKey.getIvKey()));
            System.out.println("-----------------------------------------");
            System.out.println("шифрованая строка Base64 -> " + crupt.encryptBase64(sc1.nextLine()));
        } else if (otv.equals("2")) {
            System.out.println("Введите свой ключ:");
            Scanner sc1 = new Scanner(System.in);
            String key = sc1.nextLine();
            System.out.println("Введите строку для шифровки:");
            Scanner sc2 = new Scanner(System.in);
            String str_crupt = sc2.nextLine();
            ShiperInter shiperKey = new ShiperKey(key);
            Crupt crupt = new Crupt();
            System.out.println("ключ -> " + key);
            System.out.println("шифрованая строка AES256 -> " + crupt.cryptor(shiperKey.getAlgorithm(), str_crupt, shiperKey.getSkeySpec(), shiperKey.getIvKey()));
            System.out.println("-----------------------------------------");
            System.out.println("шифрованая строка Base64 -> " + crupt.encryptBase64(sc1.nextLine()));
        }
        else if (otv.equals("3")) {
            String key = generateString();
            ShiperInter shiperKey = new ShiperKey(key);
            Crupt crupt = new Crupt();
            System.out.println("рандомный ключ -> " + key);
            System.out.println("----------------------------------");
            CruptNastr cruptNastr = new CruptNastr();
            ArrayList<String> mas_crp = new ArrayList<>();
            for (int i = 0; i < cruptNastr.getMasCruptNastr().size(); i++) {
                mas_crp.add(crupt.cryptor(shiperKey.getAlgorithm(), cruptNastr.getMasCruptNastr().get(i), shiperKey.getSkeySpec(), shiperKey.getIvKey()));
                System.out.println("шифрованая строка AES256 "+cruptNastr.getMasCruptNastr().get(i)+" -> " + crupt.cryptor(shiperKey.getAlgorithm(), cruptNastr.getMasCruptNastr().get(i), shiperKey.getSkeySpec(), shiperKey.getIvKey()));
            }
            System.out.println("----------------------------------");
            DecruptStr decr = new DecruptStr();
            for (int i = 0; i < mas_crp.size(); i++) {
                System.out.println("шифрованая строка AES256 "+mas_crp.get(i)+" -> " + decr.decryptor(shiperKey.getAlgorithm(), mas_crp.get(i), shiperKey.getSkeySpec(), shiperKey.getIvKey()));
            }

            System.out.println("\n\n\n\n\n-------------------------------------------\n");
            System.out.println(cruptNastr.strNastr(mas_crp));
        }
    }

    public static String generateString() {
        SecureRandom rng = null;
        try {
            rng = new SecureRandom().getInstanceStrong();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        int length = 16;

        char[] text = new char[length];
        for (int i = 0; i < length; i++)
        {
            text[i] = characters.charAt(rng.nextInt(characters.length()));
        }
        return new String(text);
    }
}
