java:Blowfish加密解密错误的填充异常

时间:2014-03-29 15:43:11

标签: java encryption blowfish

你好我必须加密和解密一个字符串。 这是我正在使用的方法

/** Utility method to Encrpyt a plain text string using blowfish algorithm. This method is synchronised between threads.
     * Base64 encoding is used to encode and decode byte array.
     * <p>NOTE: use the same key for Encryption and Decryption</p>
     * 
     * @param plainText Plain text String
     * @param key       Secret key ( If null default will be used)
     * @return String   URL safe encrypted String
     * @throws Exception
     */

    public synchronized static String blowfishEncryption(String plainText, String key) throws Exception {
        if(DEBUG) {
            logger.log(Level.INFO,"blowfishEncryption() method ===== passed normal text: { "+plainText+" passed key: "+key+" }");
        }
        if(key==null) {
            logger.log(Level.INFO,"passed key is null using default key");
            key=BLOWFISH_SECRET;
        }
        ByteArrayOutputStream os= new ByteArrayOutputStream(1024);
        byte[] keyByte = hexToBytes(key);
        SecretKeySpec skeySpec = new SecretKeySpec(keyByte, "Blowfish");
        Cipher ecipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");
        ecipher.init(Cipher.ENCRYPT_MODE, skeySpec);

        byte[] stringByte=plainText.getBytes("US-ASCII");

        byte[] econtent=ecipher.doFinal(stringByte);

        String out= new String(Base64.encodeBase64(econtent), "US-ASCII");

        return out;
    }

    /** Utility method for Blowfish Decryption. This method is synchronised between threads. 
     * <p>NOTE: use the same key for Encryption and Decryption</p>
     * 
     * @param cipherContent     Cipher Text Byte array to be decrypted
     * @param key               Key used for Decryption. NOTE: use same key for encryption and decryption
     * @return String           Plain text String
     * @throws Exception
     */

    public synchronized static String blowfishDecryption(String cipherText, String key) throws Exception {
        // String ciphertext is base 64 endoded string This method returns plain text string

        if(DEBUG) {
            logger.log(Level.INFO,"blowfishEncryption() method ===== passed key: "+key+" }");
        }
        if(key==null) {
            logger.log(Level.INFO,"passed key is null using default key");
            key=BLOWFISH_SECRET;
        }

        byte[] myKeyByte = hexToBytes(key);

        SecretKeySpec skeySpec = new SecretKeySpec(myKeyByte, "Blowfish");

        Cipher ecipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");

        ecipher.init(Cipher.DECRYPT_MODE, skeySpec);

        byte[] cipherContent=cipherText.getBytes("US-ASCII");


        byte[]  dContent=ecipher.doFinal(cipherContent);

        String out=new String(Base64.encodeBase64(dContent), "US-ASCII");

        return out;
    }

但我得到了错误的填充异常有什么问题。 我也希望结果在String中。我会将此字符串传递给我的服务器,服务器将使用此处使用的相同方法进行解密。

 Blowfish encryption
 Encrypt value=J7mbZ4mal7R9ckRBodqqyti70XD3+Bci
 [java] Blow fish decryption====================
 [java] Encrypt value=J7mbZ4mal7R9ckRBodqqyti70XD3+Bci

1 个答案:

答案 0 :(得分:1)

您使用blowfishEncryption方法对加密数据进行Base64编码,而不是将Base64解码为您正在使用的blowfishEncryption中的字节数组

byte[] cipherContent=cipherText.getBytes("US-ASCII");

替换该行
byte[] cipherContent=Base64.decodeBase64(cipherText);
// fix the method name if needed, as it is not clear what Base64 class you are using