加密和解密最终块没有正确填充错误?

时间:2018-02-11 20:28:15

标签: java encryption import cryptography

我正在尝试修复我的代码。在我的代码中,我正在尝试生成一个我已经完成的16位密钥。其次,生成随机消息,这也是完成的。加密和解密我收到错误的数据。最后有一个强力算法来解密我将在稍后尝试做的消息。因此,对于我的加密,代码会对其进行加密,但不会对随机生成的字符串进行加密。我收到了很多错误。

Errors: 
Given final block not properly padded. Such issues can arise if a bad key is used during decryption.
    at java.base/com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:991)
    at java.base/com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:847)
    at java.base/com.sun.crypto.provider.BlowfishCipher.engineDoFinal(BlowfishCipher.java:319)
    at java.base/javax.crypto.Cipher.doFinal(Cipher.java:2189)
    at Assignment1Demo.decryption(Assignment1Demo.java:109)
    at Assignment1Demo.bruteForce(Assignment1Demo.java:130)
    at Assignment1Demo.main(Assignment1Demo.java:30)

Exception in thread "main" java.lang.NullPointerException
    at Assignment1Demo.bruteForce(Assignment1Demo.java:131)
    at Assignment1Demo.main(Assignment1Demo.java:30)

我的代码:

import java.util.Random;
import java.security.Security;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class Assignment1Demo {

    private static String msg;  
    private static String msgE; 
    private static String msgD;
    private static int key;

    public static void main(String[] args){
        //TODO: You can only call methods in main method            

        key = generateKey();

        msg = generateMsg();

        msgE = encryption(key,msg);

        bruteForce(msgE);
    }

    private static int generateKey() {

        //TODO: implement step a (randomly generate 16-bit key)

        //16 bit digit means 2^16 -1 in decimal     
        Random rand = new Random(); 
        return rand.nextInt((int) (Math.pow(2, 16)-1));
    }

    private static String generateMsg() {

        //TODO: implement step b (randonly generate a string with an even number of characters)
        String chractersU="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        String chractersL=chractersU.toLowerCase();
        String space=" ";
        String alphanum=chractersU+space+chractersL;
        String random="";
        int length=alphanum.length();
        Random rand=new Random();
        char[] text=new char[length];

        for(int i=0;i<length;i++) {
            text[i]=alphanum.charAt(rand.nextInt(alphanum.length()));
        }

        for(int i=0;i<text.length/2;i++) {
            if(text.length%2!=0) {
                random += text[i];
            }
        }

        return random;
    }

    private static String encryption (int key, String msg) {

        //TODO: implement step c (encrypt the message)

        String strData="";
        String strKey=Integer.toString(key);

        try {
            SecretKeySpec skeyspec=new SecretKeySpec(strKey.getBytes(),"Blowfish");
            Cipher cipher=Cipher.getInstance("Blowfish");
            cipher.init(Cipher.ENCRYPT_MODE, skeyspec);
            byte[] encrypted=cipher.doFinal(msg.getBytes());
            strData=new String(encrypted);

        } catch (Exception e) {
            e.printStackTrace();

        }
        return strData;
    }   

    private static void decryption(int key, String msgE) {

        //TODO: implement step d (decryption)

        String strKey = Integer.toString(key);
        String strData="";
        try {
            SecretKeySpec skeyspec=new SecretKeySpec(strKey.getBytes(),"Blowfish");
            Cipher cipher=Cipher.getInstance("Blowfish");
            cipher.init(Cipher.DECRYPT_MODE, skeyspec);
            byte[] decrypted=cipher.doFinal(msgE.getBytes());
            strData=new String(decrypted);

        } catch (Exception e) {
            e.printStackTrace();    
        }
        System.out.println(strData);
    }

    private static void bruteForce(String msgE) {

        //TODO: implement bruteForce algorithm, you may need the above decryption(key,msgE) method

        boolean isEnglisString = msgE.matches("[a-zA-Z]+");

        if(isEnglisString)
            System.out.println("Yes encrypted message is Randomly English generated message " + msgE);
        else
            System.out.println("encrypted message is Not Randomly english generated message "+msgE);

        decryption(key, msgE);
        isEnglisString = msgD.matches("[a-zA-Z]+");

        if(isEnglisString)
            System.out.println("Yes decrypted message is Randomly english generated message "+ msgD);
        else
            System.out.println("decrypted message is not Randomly english generated message "+ msgD);
    }
}

1 个答案:

答案 0 :(得分:1)

一个问题是你正在做一些我不确定会工作的类型转换。例如,在cat my.min.css | awk '{gsub(/{|}|;/,"&\n"); print}' >> my.css 方法中,将encryption字节数组转换为字符串(使用encrypted),然后使用new String(encrypted)方法返回字节数组(使用{{1} }})。结果不是相同的字节数组。

为什么您使用decryption代替getBytes()来处理简单和密文? (与您的密钥相同,为什么使用String代替byte[]?)

相关问题