0

I've recently wrote a simple java application experimenting with ECB CBC modes of encryption and I've noticed something strange with how my code works. For some reason my resulting ciphertext will always result in the same string of characters, and I am noticing very little difference in the resulting ciphertext between the ECB and CBC modes. I am unsure if this is normal behavior so if anyone could shed some light on this I would greatly appreciate it.

import java.security.*;
import java.util.Scanner;

import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;

public class MainApp
{
    static Scanner sc = new Scanner(System.in);
    public KeyGenerator keygen;
    public SecretKey secKey;
    Cipher c;

    static SecureRandom rnd = new SecureRandom();
    static IvParameterSpec iv = new IvParameterSpec(rnd.generateSeed(8));

    public static void main(String[] args) throws Exception
    {
        MainApp theApp = new MainApp();
        theApp.start();
    }

    public void start() throws Exception
    {
        keygen = KeyGenerator.getInstance("DES");
        secKey = keygen.generateKey();

        System.out.println(secKey);

        boolean success = false;
        boolean success2 = false;
        boolean exit = false;
        int type = 0;

        do
        {
            do 
            {
                System.out.println("Weclome to the DES Encryption/Decription zone!");
                System.out.println("What form of mode do you wish to use? [E]CB or [C]BC? Type [Q]uit to exit");
                String input = sc.nextLine();

                    if(input.equalsIgnoreCase("e")){

                        type = 1;

                        do{
                            System.out.println("Do you wish to use padding? [Y]es or [N]o?");
                            input = sc.nextLine();

                            if(input.equalsIgnoreCase("y")){
                                c = Cipher.getInstance("DES/ECB/PKCS5Padding");
                                success = true;
                                success2 = true;
                            }
                            else if(input.equalsIgnoreCase("n")){
                                c = Cipher.getInstance("DES/ECB/NoPadding");
                                success = true;
                                success2 = true;
                            }
                            else{
                                System.out.println("Error - please enter a valid input");
                                success = false;
                                success2 = false;
                            }
                        }while(!success2);

                    }
                    else if(input.equalsIgnoreCase("c")){

                        type = 2;

                        do{
                            System.out.println("Do you wish to use padding? [Y]es or [N]o?");
                            input = sc.nextLine();

                            if(input.equalsIgnoreCase("y")){
                                c = Cipher.getInstance("DES/CBC/PKCS5Padding");
                                success = true;
                                success2 = true;
                            }
                            else if(input.equalsIgnoreCase("n")){
                                c = Cipher.getInstance("DES/CBC/NoPadding");
                                success = true;
                                success2 = true;
                            }
                            else{
                                System.out.println("Error - please enter a valid input");
                                success = false;
                                success2 = false;
                            }
                        }while(!success2);
                    }

                    else if(input.equalsIgnoreCase("q")){
                        System.out.println("Thanks for using me!");
                        System.exit(0);
                        success = true;
                        exit = true;
                    }
                    else{
                        System.out.println("Error - please enter a valid input");
                        success = false;
                    }
            }while(!success);


            System.out.println("Input what you wish to encrypt");
            String input = sc.nextLine();

            byte[] text = input.getBytes();

            System.out.println(type);

            System.out.println("--------------------------------------------");

            System.out.println("Text : " + new String(text));

            byte[] textEncrypted = encrypt(text, c, type);

            System.out.println("Text Encrypted : " + textEncrypted);

            byte[] textDecrypted = decrypt(textEncrypted, c, type);

            System.out.println("Text Decrypted : " + new String(textDecrypted));

            System.out.println("--------------------------------------------");

        }while(!exit);
    }

    public byte[] encrypt(byte[] b, Cipher c, int type) throws Exception
    {
        if(type == 1)
        {
        c.init(Cipher.ENCRYPT_MODE, secKey);
        }
        else if(type == 2)
        {   
            c.init(Cipher.ENCRYPT_MODE, secKey, iv);
        }
        byte[] encryptedText = null;
        try {
            encryptedText = c.doFinal(b);
        } catch (IllegalBlockSizeException e) {
            System.out.println("ERROR - If you have selected to not automatically pad your plaintext it must be a mutiple of eight bytes to be accepted. Exiting program");
            System.exit(0);
        } 

        return encryptedText;
    }

    public byte[] decrypt(byte[] b, Cipher c, int type) throws Exception
    {
        if(type == 1)
        {
        c.init(Cipher.DECRYPT_MODE, secKey);
        }
        else if(type == 2)
        {   
            c.init(Cipher.DECRYPT_MODE, secKey, iv);
        }

        byte[] decryptedText = c.doFinal(b);

        return decryptedText;

    }


}

Edit: Here is an example of my application output. On closer inspection it seems to cycle through a collection of a few different results occasionally, but I can tell they are being repeated for some reason. I've seen [B@3b2bad06 appear as the ciphertext before as well as [B@306c8343.

com.sun.crypto.provider.DESKey@184ba
Weclome to the DES Encryption/Decription zone!
What form of mode do you wish to use? [E]CB or [C]BC? Type [Q]uit to exit
e
Do you wish to use padding? [Y]es or [N]o?
y
Input what you wish to encrypt
testtext
1
--------------------------------------------
Text : testtext
Text Encrypted : [B@2aa75818
Text Encrypted : [B@2aa75818
Text Decrypted : testtext
--------------------------------------------
Weclome to the DES Encryption/Decription zone!
What form of mode do you wish to use? [E]CB or [C]BC? Type [Q]uit to exit
e
Do you wish to use padding? [Y]es or [N]o?
y
Input what you wish to encrypt
testtext
1
--------------------------------------------
Text : testtext
Text Encrypted : [B@5088a588
Text Encrypted : [B@5088a588
Text Decrypted : testtext
--------------------------------------------
Weclome to the DES Encryption/Decription zone!
What form of mode do you wish to use? [E]CB or [C]BC? Type [Q]uit to exit
e
Do you wish to use padding? [Y]es or [N]o?
y
Input what you wish to encrypt
sometext
1
--------------------------------------------
Text : sometext
Text Encrypted : [B@3b2bad06
Text Encrypted : [B@3b2bad06
Text Decrypted : sometext
--------------------------------------------
Weclome to the DES Encryption/Decription zone!
What form of mode do you wish to use? [E]CB or [C]BC? Type [Q]uit to exit
e
Do you wish to use padding? [Y]es or [N]o?
y
Input what you wish to encrypt
sometext
1
--------------------------------------------
Text : sometext
Text Encrypted : [B@306c8343
Text Encrypted : [B@306c8343
Text Decrypted : sometext
--------------------------------------------
Liam
  • 77
  • 1
  • 11

1 Answers1

4

Print your encrypted string after converting to hex: How to convert a byte array to a hex string in Java?

You are printing a hash representation of your encrypted byte array, not its contents.

Community
  • 1
  • 1
mattm
  • 5,851
  • 11
  • 47
  • 77