Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
3.0 kB
2
Indexable
Never
    public static void doElGamalDecrypt() throws Exception {
        BigInteger g256 = new BigInteger(
                "00C9D167714BDDCC" +
                        "A0FDE7670C118EB3" +
                        "3D217875085CF07F" +
                        "EDE745E21E5AC8ABC8", 16);
        BigInteger p256 = new BigInteger(
                "00E28DD8921B337F" +
                        "A87120753A18575F" +
                        "9AB91D179902AFEF" +
                        "899437B583E30D2183", 16);
        ElGamalParameterSpec  egSpec = new ElGamalParameterSpec(p256, g256);

//        BigInteger       ct = new BigInteger ("138B71DAB93BD1020F5E691C1AFB2145" +
//                "85FD25BEF670368785148ACA3A440DC0" +
//                "27BBD1EA89BFE87CD3272B98F9B5ADF9" +
//                "193059F1272AF6D181EEB0ADCAD1D84C", 16);
        BigInteger       ct = new BigInteger ("208B71DAB93BD1020F5E691C1AFB2145" + // changed first byte to 20
                "85FD25BEF670368785148ACA3A440DC0" +
                "27BBD1EA89BFE87CD3272B98F9B5ADF9" +
                "193059F1272AF6D181EEB0ADCAD1D84C", 16);
        byte[]           inputBytes = ct.toByteArray();
        Cipher	         cipher = Cipher.getInstance("ElGamal/None/NoPadding");
        SecureRandom     random = new SecureRandom();

        KeyFactory      keyFactory = KeyFactory.getInstance("ElGamal");
        ElGamalPublicKeySpec pubKeySpec = new ElGamalPublicKeySpec(
                new BigInteger("717CE2A8B18F4382" +
                        "B3CA0B29D0AB886C" +
                        "9D2E546BCC973B29" +
                        "C29EAD40E5022F31", 16),
                egSpec);
        ElGamalPrivateKeySpec privKeySpec = new ElGamalPrivateKeySpec(
                new BigInteger("1E75407BEAAE22C1" +
                        "8CD82C5E94A727A2" +
                        "DCF2D9467A3264D7" +
                        "04686819E1F0FA3F", 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));

        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