Question
I need help decrypting this code. I did the encryption part but I am having trouble decryting it. I am new to this and still
I need help decrypting this code. I did the encryption part but I am having trouble decryting it. I am new to this and still learning.
import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import java.lang.StringBuffer; import java.util.Base64; import java.util.Base64.Encoder; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException;
public class Encryption{ //Encrypts input using AES algorithm public static String encryptString(String key, String IV, String text){ try{ //Declares the initialization vector IvParameterSpec iv = new IvParameterSpec(IV.getBytes("UTF-8")); //Initializes the key in the AES Algorithm SecretKeySpec secretKey = new SecretKeySpec(key.getBytes("UTF-8"),"AES"); //Creates a cipher Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5padding"); //Initializes the encryption process cipher.init(Cipher.ENCRYPT_MODE, secretKey,iv); //Does the final step of encryption byte[] cipherText = cipher.doFinal(text.getBytes("UTF-8")); //Prints when encryption is finished System.out.println("Encryption Done!!"); //Returns encrypted string return Base64.getEncoder().encodeToString(cipherText); } catch(Exception e){ System.err.println(e.getMessage()); return "Error in Encryption"; } } //Writes the encrypted string and MAC to a text file public static void writeToFile(String text)throws IOException,NoSuchAlgorithmException{ //Creates a new message digest using SHA-256 hashing algorithm MessageDigest digest = MessageDigest.getInstance("SHA-256"); //Creates a hash of the text byte[] hashBytes = digest.digest(text.getBytes("UTF-8")); //Converts the hash from hex to a string StringBuffer sb = new StringBuffer(); for(int i=0;i
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started