AES encrypt-decrypt in Java

 public static String decrypt(byte[] data, byte[] key) throws NoSuchPaddingException, NoSuchAlgorithmException,

            BadPaddingException, IllegalBlockSizeException, InvalidKeyException {

        Cipher aes = Cipher.getInstance("AES/ECB/PKCS5Padding");

        aes.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key,

    "AES"));

        return new String(Base64.getDecoder().decode(Base64.getEncoder().encodeToString((aes.doFinal(data)))));

}


public static String encrypt(byte[] data, byte[] key) throws NoSuchPaddingException, NoSuchAlgorithmException,

            BadPaddingException, IllegalBlockSizeException, InvalidKeyException {

        Cipher aes = Cipher.getInstance("AES/ECB/PKCS5Padding");

        aes.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key,

    "AES"));

        return Base64.getEncoder().encodeToString(aes.doFinal(data));

}


public static void main(String [] args) {

String s="{\"mac\":\"1354345235465463464356\",\"dt\":\"web\",\"lang\":\"az\",\"dId\":\"9bc969f3-93f8-4748-a398-fe1d44d2b84d\",\"uA\":{\"m\":\"unknown\",\"os\":\"Win32\",\"osV\":\"unknownVersion\",\"b\":\"Chrome\",\"dt\":\"web\",\"bV\":\"98\"}}";

        String encrypt = encrypt(s.getBytes(StandardCharsets.UTF_8), "7zjoqLVdqnxj43AaX9dFhA==".getBytes(

                StandardCharsets.UTF_8));

        System.out.println(encrypt);


        String s2="oSuHx/72gqwPr668o0JKxX5XVz7GpVIsy9JpUIoko+aQaocu38ygBN3PcleGLp1W0IpacCTz1kfqdymUVPQ5UFyoheoRChuGCeUWP/B8PuE4pHJZBtbK/UDN2a8AaK2SatZ3WYIX47YJ1NkWTSCNLTReqCvrie9hJVZgwdESPQZ9QnKmdMEczUs2caiTLx0isW/ez1v4kpXeIV2W/ZEOFmlmlh2a0Pk5+D8OXM2DtuwKQcLJ1Sa3b3sXPtRUQhKn";

        String decrypt = decrypt(Base64.getDecoder().decode(s2), "7zjoqLVdqnxj43AaX9dFhA==".getBytes(StandardCharsets.UTF_8));

        System.out.println(decrypt);

}

Comments

Popular posts from this blog

Installation instructions for some programs on linux ubuntu

Hibernate and Application Performance

timezone ile bagli Jackson deserializableda problem