JavaWeb Action中和Java main函数调用相同加密方法 加密不相同

 1 package com.com.utils;
 2 
 3 import java.io.UnsupportedEncodingException;
 4 import java.security.InvalidAlgorithmParameterException;
 5 import java.security.InvalidKeyException;
 6 import java.security.NoSuchAlgorithmException;
 7 import java.util.LinkedHashMap;
 8 
 9 import javax.crypto.BadPaddingException;
10 import javax.crypto.Cipher;
11 import javax.crypto.IllegalBlockSizeException;
12 import javax.crypto.NoSuchPaddingException;
13 import javax.crypto.spec.IvParameterSpec;
14 import javax.crypto.spec.SecretKeySpec;
15 
16 import org.apache.commons.codec.binary.Base64;
17 import org.apache.commons.codec.digest.DigestUtils;
18 
19 public class EncoderStr {
20 
21     public static void encoder() throws InvalidKeyException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException{
22         String apiKey = "fd23ce7e40ca449aa5374117c8200ee0";
23         String secretKey = "973cc0c59f0adc3c";
24         long timeMillis = Long.valueOf("1490882040951");
25         
26         LinkedHashMap<String, Object> param = new LinkedHashMap<>();
27         param.put("key", "fd23ce7e40ca449aa5374117c8200ee0");
28         param.put("info",  "你好");
29         param.put("userid", "Doraemon");
30         
31         String aesKey = DigestUtils.md5Hex(secretKey + timeMillis + apiKey);
32         SecretKeySpec secretKeySpec = new SecretKeySpec(DigestUtils.md5(aesKey), "AES");
33         Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
34         cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, new IvParameterSpec(new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}));
35         byte[] bytes = cipher.doFinal(JsonUtils.toJson(param).getBytes());
36         System.out.println(new String(Base64.encodeBase64(bytes), "UTF-8"));
37     }
38 }

上面的是一个加密的工具类,使用的AES加密,数据都是写死的,按道理JavaWeb Action中和Java main函数都调用这个加密方法,输出的一样的,结果不一样

这是JAVA WEB里的方法

package com.com.cc;

import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.com.utils.EncoderStr;

public class TestAction extends HttpServlet{

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("Hello");
        try {
            EncoderStr.encoder();
        } catch (InvalidKeyException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (BadPaddingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    
}

输出的:

FdwokescL4GYIcIIK0EPF+9HTNHd2+7ZbK7/Qn/QtjP47dbwIkHHtb3lYx4SgiStYHJjxruNj0Gh9iB2jMdJG9m7Tmg0BweixC6FomGwfgM=

如图:

Java main函数调用的

package com.com.cc;

import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

import com.com.utils.EncoderStr;

public class Main {

    public static void main(String[] args) throws InvalidKeyException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
        // TODO Auto-generated method stub
        EncoderStr.encoder();
    }

}

输出:

FdwokescL4GYIcIIK0EPF+9HTNHd2+7ZbK7/Qn/QtjP47dbwIkHHtb3lYx4SgiStMITr+UoPmxK00VzQhUTE0SSQjvdgNQ5OPBwDbT+nJlc=

如图:

这到底是因为什么原因啊??

posted @ 2017-03-31 21:26  你个小兔崽子  阅读(314)  评论(0)    收藏  举报