Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Current Trends In Database Technology Edbt 2006 Edbt 2006 Workshops Phd Datax Iidb Iiha Icsnw Qlqp Pim Parma And Reactivity On The Web Munich Germany March 2006 Revised Selected Papers Lncs 4254

Authors: Torsten Grust ,Hagen Hopfner ,Arantza Illarramendi ,Stefan Jablonski ,Marco Mesiti ,Sascha Muller ,Paula-Lavinia Patranjan ,Kai-Uwe Sattler ,Myra Spiliopoulou ,Jef Wijsen

2006th Edition

3540467882, 978-3540467885

More Books

Students also viewed these Databases questions