For this code, I'm trying to decrypt a message. But I keep getting an IllegalBlockSizeException.
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class Decrypt {
static Cipher DESCipher;
static KeyGenerator KEY_generator;
static SecretKey myDesKey;
static byte[] textEncrypted;
public static byte[] Decrypt(byte[] input, String sk_string) {
try {
//Convert String to secret key
byte[] decodedKey = Base64.getDecoder().decode(sk_string);
SecretKeySpec originalKey = new SecretKeySpec(decodedKey, 0, decodedKey.length, "DES");
//init the Mode
DESCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
// Initialize the same cipher for decryption
DESCipher.init(Cipher.DECRYPT_MODE, originalKey);
// Decrypt the text
byte[] textDecrypted = DESCipher.doFinal(input);
//Return the text has been decrypted
System.out.println("Text Decryted : " + new String(textDecrypted));
return textDecrypted;
} catch (IllegalBlockSizeException | InvalidKeyException | BadPaddingException | NoSuchPaddingException | NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
String sk = "3Kh/EMEHxBA=";
String input = "[B@7a92922";
byte[] gb= input.getBytes();
Decrypt(gb,sk);
}
}
My encryption class works, but I can't properly pad the input for decrypt to get the message back. Heres my encrypt class:
import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.io.*;
import java.util.Base64;
public class Encrypt {
static Cipher DESCipher;
static KeyGenerator KEY_generator;
static SecretKey myDesKey;
static byte[] textEncrypted;
public static byte[] Encrypt(String user_input, String sk_string) {
try {
//Convert String to secret key
byte[] decodedKey = Base64.getDecoder().decode(sk_string);
SecretKey originalKey = new SecretKeySpec(decodedKey, 0, decodedKey.length, "DES");
// Create the cipher type as DES
DESCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
// Initialize the cipher for encryption
DESCipher.init(Cipher.ENCRYPT_MODE, originalKey);
//Turn the String of user_input to the byte mode
byte[] text = user_input.getBytes();
// Encrypt the text -> Do final
textEncrypted = DESCipher.doFinal(text);
//Return the text;
//System.out.println(textEncrypted);
return textEncrypted;
} catch (NoSuchAlgorithmException | IllegalBlockSizeException | InvalidKeyException | BadPaddingException | NoSuchPaddingException e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
String s= "hello everyone";
String sk= "3Kh/EMEHxBA=";
System.out.println(s);
System.out.println("Text encrypted : " + Encrypt(s,sk));
}
}
Can anyone tell me what's wrong and how my code should look like to fix it? I have to keep the classes separate and enter the key, cipher, and plaintext in as strings. Thanks!