zoukankan      html  css  js  c++  java
  • java对称加密报错:Input length must be multiple of 8 when decrypting with padded cipher

    HTTP Status 500 - Request processing failed; nested exception is javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher

    org.springframework.web.util.NestedServletException: Request processing failed; nested exception is javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher
        org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:894)
        org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

    Here is my Encryption and Decryption code

    //secret key 8 
        private static String strkey ="Blowfish";
    

    UPDATED

     //encrypt using blowfish algorithm
        public static byte[] encrypt(String Data)throws Exception{
    
            SecretKeySpec key = new SecretKeySpec(strkey.getBytes("UTF8"), "Blowfish");
             Cipher cipher = Cipher.getInstance("Blowfish");
             cipher.init(Cipher.ENCRYPT_MODE, key);
    
             return (cipher.doFinal(Data.getBytes("UTF8")));
    
        }
    
        //decrypt using blow fish algorithm
        public static String decrypt(byte[] encryptedData)throws Exception{
             SecretKeySpec key = new SecretKeySpec(strkey.getBytes("UTF8"), "Blowfish");
             Cipher cipher = Cipher.getInstance("Blowfish");
             cipher.init(Cipher.DECRYPT_MODE, key);
             byte[] decrypted = cipher.doFinal(encryptedData);
             return new String(decrypted); 
    
        }
    



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

    import org.apache.commons.codec.binary.Base64;


    public class Test {

        private static String strkey ="Blowfish";
        private static Base64 base64 = new Base64(true);

         //encrypt using blowfish algorithm
        public static String encrypt(String Data)throws Exception{

            SecretKeySpec key = new SecretKeySpec(strkey.getBytes("UTF8"), "Blowfish");
             Cipher cipher = Cipher.getInstance("Blowfish");
             cipher.init(Cipher.ENCRYPT_MODE, key);

             return base64.encodeToString(cipher.doFinal(Data.getBytes("UTF8")));

        }

        //decrypt using blow fish algorithm
        public static String decrypt(String encrypted)throws Exception{
            byte[] encryptedData = base64.decodeBase64(encrypted);
             SecretKeySpec key = new SecretKeySpec(strkey.getBytes("UTF8"), "Blowfish");
             Cipher cipher = Cipher.getInstance("Blowfish");
             cipher.init(Cipher.DECRYPT_MODE, key);
             byte[] decrypted = cipher.doFinal(encryptedData);
             return new String(decrypted);

        }

        public static void main(String[] args) throws Exception {
            String data = "will this work?";
            String encoded = encrypt(data);
            System.out.println(encoded);
            String decoded = decrypt(encoded);
            System.out.println(decoded);
        }
    }

    Hope this answers your questions.

  • 相关阅读:
    一款新型的智能家居WiFi选择方案——SimpleWiFi在无线智能家居中的应用
    智能手机的工业控制应用方案——SimpleWiFi在工业控制领域应用
    一种单片机支持WiFi的应用——SimpleWiFi在单片机中的应用
    TI推出SimpleLink低能耗蓝牙CC2541
    SimpleWiFi模块评估板
    Android架构设计和软硬整合完整训练
    CentOS上解压ZIP乱码的解决办法
    更具体的描述JNI
    数据市中心全省中国mysql脚本
    几种方法枚举子集
  • 原文地址:https://www.cnblogs.com/firstdream/p/5478450.html
Copyright © 2011-2022 走看看