I am trying to make a little encryption/decryption software for my APCS-A final project. I have most of it done with various text ciphers and such, but I am trying to get DES encryption to work as like the big thing. I couldn't get it working in the main program so I created its own little thing to try to learn and test it first. I think I've gotten decently far, but I am getting stuck on an error that I can't figure out.
import java.util.Scanner;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
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.*;
import java.util.Base64;
public class BigETest
{
public static void main(String[]args) throws NoSuchAlgorithmException,NoSuchPaddingException,InvalidKeyException,IllegalBlockSizeException,BadPaddingException
{
Scanner kbd = new Scanner(System.in);
System.out.print("Enter text: ");
String t = kbd.nextLine();
String[] rets = en(t);
System.out.println("Encrypted: "+ rets[0]);
System.out.println("Key: " + rets[1]);
String dec = de(rets[0],rets[1]);
System.out.println("Decrypted: "+dec);
}
public static String[] en(String inText) throws BadPaddingException,IllegalBlockSizeException,InvalidKeyException,NoSuchPaddingException,NoSuchAlgorithmException
{
KeyGenerator keyGen = KeyGenerator.getInstance("DES");
SecretKey key = keyGen.generateKey();
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] text = Base64.getUrlDecoder().decode(inText);
byte[] encryptedBytes = cipher.doFinal(text);
String encrypted = new String(Base64.getUrlEncoder().encodeToString(encryptedBytes));
byte[] rawKey = key.getEncoded();
String stringKey = new String(Base64.getUrlEncoder().encodeToString(rawKey));
String[] ret = {encrypted,stringKey};
return ret;
}
public static String de(String inText, String inKey) throws BadPaddingException,IllegalBlockSizeException,InvalidKeyException,NoSuchPaddingException,NoSuchAlgorithmException
{
byte[] keyB = Base64.getUrlDecoder().decode(inKey);
SecretKey key = new SecretKeySpec(keyB, "DES");
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] text = Base64.getUrlDecoder().decode(inText);
byte[] fin = cipher.doFinal(text);
String dec = new String(Base64.getUrlEncoder().encodeToString(fin));
return dec;
}
}
The code works sometimes if the input is short and doesn't have spaces in it
Enter text: test
Encrypted: BOM9tFOUfWc=
Key: bq2ooSbc1n8=
Decrypted: test
but falls apart if the input is a bit longer
Enter text: BigTest
Encrypted: jme0nIAHXyA=
Key: 90UxVwetj2g=
Decrypted: BigTess=
And then gives me an error if there are any spaces
Enter text: bigger test
java.lang.IllegalArgumentException: Illegal base64 character 20
at java.base/java.util.Base64$Decoder.decode0(Base64.java:746)
at java.base/java.util.Base64$Decoder.decode(Base64.java:538)
at java.base/java.util.Base64$Decoder.decode(Base64.java:561)
at BigETest.en(BigETest.java:34)
at BigETest.main(BigETest.java:20)
I have tried changing things up and looked around both on here and just around anyplace I could find on Google for some answers, but nothing seems to be working. Any help would be greatly appreciated.