Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
2.3 kB
3
Indexable
Never
public static void doElGamalDecrypt() throws Exception
    {
        BigInteger g256 = new BigInteger(
            "3F798DAD0EA7742FB9FE821D556B1AF7B3E1747B4E88C483DC495CBFE0F4078E", 16);
        BigInteger p256 = new BigInteger(
            "00A791047E786375ECADB5AC4557AE26D0475425A1AA6C34A707214548E62931C3", 16);
        ElGamalParameterSpec  egSpec = new ElGamalParameterSpec(p256, g256); 
        BigInteger       ct = new BigInteger ("37D932E3A086C334FFED0194F381E2E830A25D04B95AA0CB1875AF4B7D159CE5A60EB2291FA870DA29F29726ECCC90BB8601F5A4CC8FE4CB6884DE39532DE30E", 16);
        byte[]           inputBytes = ct.toByteArray();
        Cipher	         cipher = Cipher.getInstance("ElGamal/None/PKCS1Padding");
        SecureRandom     random = new SecureRandom();

        KeyFactory      keyFactory = KeyFactory.getInstance("ElGamal");
        ElGamalPublicKeySpec pubKeySpec = new ElGamalPublicKeySpec(
                new BigInteger("3BF48147393A021C345267958223303310FA81EC08ABDFD09C922ED91CE53DFE", 16),
                egSpec);
        ElGamalPrivateKeySpec privKeySpec = new ElGamalPrivateKeySpec(
                new BigInteger("6BA1A8CEB02B1478D59C15305A6979E2B38871912356CAB58D63B263738A1BCC", 16),
                egSpec);
        ElGamalPublicKey pubEG = (ElGamalPublicKey)keyFactory.generatePublic(pubKeySpec);
        ElGamalPrivateKey privEG = (ElGamalPrivateKey)keyFactory.generatePrivate(privKeySpec);

        System.out.println("Duotoji sifrograma : " + toHex(inputBytes));
        cipher.init(Cipher.DECRYPT_MODE, privEG);
        byte[] plainText = cipher.doFinal(inputBytes, 0, inputBytes.length);

        System.out.println("Iššifruota tekstograma : " + toHex(plainText));

        //patikrinimas
        cipher.init(Cipher.ENCRYPT_MODE, pubEG, random);
        byte[] cipherText = cipher.doFinal(plainText);

        System.out.println("Vel uzsifruotas : " + toHex(cipherText));

        System.out.println("EG viesas Y : " + toHex(pubEG.getY().toByteArray()));
        System.out.println("EG privatus X : " + toHex(privEG.getX().toByteArray()));
        System.out.println("EG generatorius G : " + toHex(privEG.getParameters().getG().toByteArray()));
        System.out.println("EG modulis P : " + toHex(pubEG.getParameters().getP().toByteArray()));

    }
Leave a Comment