Question

In: Computer Science

java cryptography extension with an example from end to end with code explanation. it should support...

java cryptography extension with an example from end to end with code explanation. it should support Java 8 and AES 256.

how to do encrypt and decrypt the specific example.

Enable AES 256-bit encryption for Java 8 with a sample developed code and workflow. Thank you so much.

Solutions

Expert Solution

AES 256 Encryption:

Java program to encrypt a password (or any information) using AES 256 bits.

AES256 encryption example:

private static String secretKey = "boooooooooom!!!!";

private static String salt = "ssshhhhhhhhhhh!!!!";

public static String encrypt(String strToEncrypt, String secret)

{

    try

    {

        byte[] iv = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

        IvParameterSpec ivspec = new IvParameterSpec(iv);

         

        SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");

        KeySpec spec = new PBEKeySpec(secretKey.toCharArray(), salt.getBytes(), 65536, 256);

        SecretKey tmp = factory.generateSecret(spec);

        SecretKeySpec secretKey = new SecretKeySpec(tmp.getEncoded(), "AES");

         

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

        cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivspec);

        return Base64.getEncoder().encodeToString(cipher.doFinal(strToEncrypt.getBytes("UTF-8")));

    }

    catch (Exception e)

    {

        System.out.println("Error while encrypting: " + e.toString());

    }

    return null;

}

AES 256 Decryption:

Java program to decrypt a password (or any information) using AES 256 bits:

AES256 decryption example:

private static String secretKey = "boooooooooom!!!!";

private static String salt = "ssshhhhhhhhhhh!!!!";

public static String decrypt(String strToDecrypt, String secret) {

    try

    {

        byte[] iv = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

        IvParameterSpec ivspec = new IvParameterSpec(iv);

         

        SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");

        KeySpec spec = new PBEKeySpec(secretKey.toCharArray(), salt.getBytes(), 65536, 256);

        SecretKey tmp = factory.generateSecret(spec);

        SecretKeySpec secretKey = new SecretKeySpec(tmp.getEncoded(), "AES");

         

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");

        cipher.init(Cipher.DECRYPT_MODE, secretKey, ivspec);

        return new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt)));

    }

    catch (Exception e) {

        System.out.println("Error while decrypting: " + e.toString());

    }

    return null;

}

Java AES 256 Example:

Let’s test our AES256 encryption and decryption methods with a simple string.

public static void main(String[] args)

{

    String originalString = "howtodoinjava.com";

     

    String encryptedString = AES.encrypt(originalString, secretKey) ;

    String decryptedString = AES.decrypt(encryptedString, secretKey) ;

      

    System.out.println(originalString);

    System.out.println(encryptedString);

    System.out.println(decryptedString);

}

Program output:

howtodoinjava.com

biXhp3Ha1fgxVEp48zHrvVoXMStmxPuAPHo3TVz5lHU=

howtodoinjava.com


Related Solutions

This is an intro to java question. Please post with pseudocode and java code. Problem should...
This is an intro to java question. Please post with pseudocode and java code. Problem should be completed using repetition statements like while and selection statements. Geometry (10 points) Make API (API design) Java is an extensible language, which means you can expand the programming language with new functionality by adding new classes. You are tasked to implement a Geometry class for Java that includes the following API (Application Programming Interface): Geometry Method API: Modifier and Type Method and Description...
The answer should be in JAVA. You will design and implement two classes to support a...
The answer should be in JAVA. You will design and implement two classes to support a client program, RockPaperScissorsGame.java, to simulate Rock-Paper-Scissors game. Read and understand the client program to find out the requirements for the HandShape and Player classes. The rules of the Rock-Paper-Scissors game are:     Scissors✌️ beats Paper✋ that beats Rock✊ that beats Scissors✌️ Additionally, to simplify the game logic (and complexify a little bit the HandSahpe class) , two players cannot show the same hand shape....
The answer should be in JAVA. You will design and implement two classes to support a...
The answer should be in JAVA. You will design and implement two classes to support a client program, RockPaperScissorsGame.java, to simulate Rock-Paper-Scissors game. Read and understand the client program to find out the requirements for the HandShape and Player classes. The rules of the Rock-Paper-Scissors game are:     Scissors✌️ beats Paper✋ that beats Rock✊ that beats Scissors✌️ Additionally, to simplify the game logic (and complexify a little bit the HandSahpe class) , two players cannot show the same hand shape....
What are the Signature lines of a Java class code? With an example. and the UML...
What are the Signature lines of a Java class code? With an example. and the UML diagram.
Explain direct material variance and labor variance. Please give a good explanation and     example to support...
Explain direct material variance and labor variance. Please give a good explanation and     example to support your answer.
Three potential ethical issues involving statistics. Support your answer with an explanation and an example: Improper...
Three potential ethical issues involving statistics. Support your answer with an explanation and an example: Improper sampling Misleading charts, graphs, and descriptive measures Inappropriate statistical analysis or inappropriate interpretation of statistical results
Write a java code to demonstrate the File IO. Your code should get the following information...
Write a java code to demonstrate the File IO. Your code should get the following information from the user. • Get a file name fname for output • Get number of data (numbers) (N) you want to process from the user • Get N numbers from the users through keyboard and store them in an array • Get M (How many numbers to read from file) • (Or)You are free to use same N for M (use N for both...
Write a java code to demonstrate the File IO. Your code should get the following information...
Write a java code to demonstrate the File IO. Your code should get the following information from the user. • Get a file name fname for output • Get number of data (numbers) (N) you want to process from the user • Get N numbers from the users through keyboard and store them in an array • Get M (How many numbers to read from file) • (Or)You are free to use same N for M (use N for both...
Write a java code to demonstrate the File IO. Your code should get the following information...
Write a java code to demonstrate the File IO. Your code should get the following information from the user. • Get a file name fname for output • Get number of data (numbers) (N) you want to process from the user • Get N numbers from the users through keyboard and store them in an array • Get M (How many numbers to read from file) • (Or)You are free to use same N for M (use N for both...
Explain importance of gender in moral reasoning. Support your explanation with some modern times example(s).
Explain importance of gender in moral reasoning. Support your explanation with some modern times example(s).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT