DES密文长度在传输过程中减少,并且不是8字节的倍数

时间:2015-05-04 16:41:53

标签: java encryption byte des

对于项目,尝试使用DES算法加密pwd。

当我尝试这个时

    private static String key = "my8bcode"; /*Key 8 bytes or 56 bit supported by algo OF*/
    private static byte[] byteKey = key.getBytes(); 

    public static void main(String[] args) throws Exception {
          String ss = "yuyuvdzdsfdsfsdsdsdsdsa";
          byte[] plainText = ss.getBytes();//Conversion en byte

        try {
            SecretKeySpec secretKeySpec = new SecretKeySpec(byteKey, "DES");

            Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); // Request the use of the DES algorithm, using the ECB mode (Electronic CodeBook) and style padding PKCS-5.
            cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
            byte[] myCipherText = cipher.doFinal(plainText);
            System.out.println(new String(myCipherText, "UTF8"));
            System.out.println(myCipherText.length);

             System.out.println("\nStart decryption");
             cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
             byte[] newPlainText = cipher.doFinal(myCipherText);
             System.out.println(new String(newPlainText, "UTF8"));
        } catch (Exception e) {
            e.printStackTrace();
        }

我没有问题,但为了更进一步,我尝试使用此代码在两个单独的步骤中执行此操作:

    try {
        SecretKeySpec secretKeySpec = new SecretKeySpec(byteKey, "DES");

        Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); //Request the use of the DES algorithm, using the ECB mode (Electronic CodeBook) and style padding PKCS-5.
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
        byte[] myCipherText = cipher.doFinal(plainText);
        byte[] test = (new String(myCipherText, "UTF8")).getBytes();

         System.out.println("\nStart decryption");
         cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
         byte[] newPlainText = cipher.doFinal(test);
         System.out.println(new String(newPlainText, "UTF8"));
    } catch (Exception e) {
        e.printStackTrace();
    }

它没有用,我在我的控制台中有这条消息:

  

javax.crypto.IllegalBlockSizeException:输入长度必须是8的倍数

确实,测试[]的长度是7而不是8.可能怎么样?

为什么我在转录过程中丢失了一个字节?

请帮助我理解并解决我的问题;)

2 个答案:

答案 0 :(得分:1)

byte[]String应该分开。主要错误是随机字节序列(加密文本)可能很容易错误UTF-8 。 UTF-8是一种多字节格式,其中高位以指定的方式标记多字节序列,10xxxxxx是一个连续字节。

但是要加密字符串(Unicode),使用UTF-8非常合适。单字节编码将是有损的;如果文字包含希腊文和保加利亚文,请说。

byte[] data = text.getBytes(StandardCharsets.UTF_8);
text = new String(data, StandardCharsets.UTF_8);

这些是String构造函数和getBytes方法的重载版本,并指定了charset。否则它是依赖于平台的,因此与另一台计算机的通信是灾难性的。

但是只要你有二进制数据byte[],就不要将它转换为(Unicode)String,其中 是一个(昂贵的)转换,多余且容易出错。

因此,人们经常会看到二进制数据另外转换为Base64,接收更长的ASCII字符串。

答案 1 :(得分:0)

我只是这样做(删除UTF 8规范)并经过大量测试后才能正常工作:

try {

            SecretKeySpec secretKeySpec = new SecretKeySpec(byteKey, "DES");



            Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); //Demande l'utilisation de l'algorithme DES, en utilisant le mode ECB (Electronic CodeBook) et le style de padding PKCS-5.
            cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
            byte[] myCipherText = cipher.doFinal(plainText);
            byte[] test = (new String(myCipherText)).getBytes();


             cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
             byte[] newPlainText = cipher.doFinal(test);
             System.out.println(new String(newPlainText));

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

非常感谢