Java:DES / ECB加密始终产生相同的密文

时间:2015-03-18 17:52:43

标签: java encryption des

我最近编写了一个简单的Java应用程序,尝试使用ECB CBC加密模式,我注意到我的代码工作方式有些奇怪。由于某种原因,我得到的密文总是会产生相同的字符串,而且我注意到ECB和CBC模式之间产生的密文差异很小。我不确定这是否是正常行为,所以如果有人能够对此有所了解,我将非常感激。

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;

    }


}

编辑:这是我的应用程序输出的示例。仔细观察后,它似乎偶尔会循环收集一些不同的结果,但我可以说它们因某种原因而被重复。我见过[B @ 3b2bad06出现之前的密文以及[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
--------------------------------------------

1 个答案:

答案 0 :(得分:4)

转换为十六进制后打印加密字符串: How to convert a byte array to a hex string in Java?

您正在打印加密字节数组的哈希表示,而不是其内容。