Generate Secret Key


Generate secret key using MD5 algorithm


Java code for generating secret key



public class MD5Algorithm
{
    private static String key = "W8CyfwnRFRbQschifbQnWIYWFWDJgq3v";
    private static byte[] sharedVector = {
        0x01, 0x02, 0x03, 0x05, 0x07, 0x0B, 0x0D, 0x11,
        0x01, 0x02, 0x03, 0x05, 0x07, 0x0B, 0x0D, 0x11
    };

    public static String encryptText(String rawText) 
    {
        String encText = "";
        byte[] keyArray;
        byte[] toEncryptArray;
        try 
        {
            toEncryptArray = rawText.getBytes("UTF-8");
            MessageDigest m = MessageDigest.getInstance("MD5");
            keyArray = m.digest(key.getBytes("UTF-8"));
            Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
            c.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(keyArray,"AES"), new
            IvParameterSpec(sharedVector));
            byte[] encrypted = c.doFinal(toEncryptArray);
            encText = Base64.encodeToString(encrypted,Base64.DEFAULT);
        }
        catch (NoSuchAlgorithmException | UnsupportedEncodingException |NoSuchPaddingException |
            InvalidKeyException | InvalidAlgorithmParameterException |
            IllegalBlockSizeException | BadPaddingException e) 
        {
            e.printStackTrace();
        }
        returnencText;
    }
}

Copyright © . All Rights Reserved