河豚加密和解密不起作用

时间:2019-05-09 19:35:58

标签: java encryption cryptography blowfish

因此,我正在为正在制作的应用程序使用简单的Blowfish加密/解密。有时可以使用,但不可靠。

正在加密的表单模板:

email@gmail.com

message

END

当我使用诸如test1@gmail.com或test2@gmail.com之类的电子邮件时,它可以完美工作。 当我使用我的个人电子邮件:tywemc@gmail.com时,该表单已加密,但是在解密后,它返回î/ÅÝ®Îmail.comÄþ4œ’> L / ND

因此,似乎它解密了表单的一部分,但未解密整个内容,我不知道为什么。它似乎使用不同的真实电子邮件部分解密了其他形式,但仍未完全解密。为了简单起见,我在Crypto.java类中使用了静态密钥。

我试图确保较长的电子邮件由于块大小或字符串长度问题而导致其无法正常工作,但我不认为这是问题所在。

Crypto.java

package core;

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

public class Crypto {

    private static String key = "key12345";

    public static String encrypt(String strClearText,String strKey) {
        String strData = "";

        try {
            SecretKeySpec skeyspec = new SecretKeySpec(key.getBytes(), "Blowfish");
            System.out.println("Ëncryption keyspec: " + skeyspec.toString());
            Cipher cipher = Cipher.getInstance("Blowfish");
            cipher.init(Cipher.ENCRYPT_MODE, skeyspec);
            byte[] encrypted = cipher.doFinal(strClearText.getBytes());
            strData = new String(encrypted);

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


    public static String decrypt(String strEncrypted,String strKey) {
        String strData = "";

        try {
            SecretKeySpec skeyspec = new SecretKeySpec(key.getBytes(),"Blowfish");
            System.out.println("Decryption keyspec: " + skeyspec.toString());
            Cipher cipher = Cipher.getInstance("Blowfish");
            cipher.init(Cipher.DECRYPT_MODE, skeyspec);
            byte[] decrypted = cipher.doFinal(strEncrypted.getBytes());
            strData = new String(decrypted);

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

我应该使用其他加密类型/方法还是我做错了什么?预先感谢!

更新代码:

package core;

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

public class Crypto {

    private static String key = "key12345";

    public static byte[] encrypt(String strClearText, String strKey) {
        byte[] encrypted = null;

        try {
            SecretKeySpec skeyspec = new SecretKeySpec(key.getBytes(), "Blowfish");
            System.out.println("Ëncryption keyspec: " + skeyspec.toString());
            Cipher cipher = Cipher.getInstance("Blowfish");
            cipher.init(Cipher.ENCRYPT_MODE, skeyspec);
            encrypted = cipher.doFinal(strClearText.getBytes());
//          strData = new String(encrypted);

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


    public static String decrypt(byte[] strEncrypted, String strKey) {
        String strData = "";

        try {
            SecretKeySpec skeyspec = new SecretKeySpec(key.getBytes(),"Blowfish");
            System.out.println("Decryption keyspec: " + skeyspec.toString());
            Cipher cipher = Cipher.getInstance("Blowfish");
            cipher.init(Cipher.DECRYPT_MODE, skeyspec);
            byte[] decrypted = cipher.doFinal(strEncrypted);
            strData = new String(decrypted);

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

[B@164b077d是作为strEncrypted传入的。

错误 javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher

0 个答案:

没有答案