Java、C#互传账号信息

    public JsonResult SendInfoTONPIJavaSys()
    {
        var res = new JsonResult();
        UserModel objUser = Session["User"] as UserModel;
        String time = System.DateTime.Now.ToLongTimeString();
        time = System.DateTime.Now.ToString("yyyyMMddHHmmss");
        //String urlStr = "UserNO="+objUser.EmpNo + "&Token=JavaNPISYS&Time=" + System.DateTime.Now.ToString("yyyyMMddHHmmss");
        String urlStr = "UserNO=" + objUser.EmpNo+ "&Time=" + System.DateTime.Now.ToString("yyyyMMddHHmmss");
        //密文要和Java端一致
        var Key = Encoding.UTF8.GetBytes("23147404hunterht23147404hunterht");
        var toEncryptArray = Encoding.UTF8.GetBytes(urlStr);
        using (var aes = new System.Security.Cryptography.RijndaelManaged())
        {
            aes.Key = Key;
            //IV要和Java端一致
            aes.IV = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
            aes.Mode = System.Security.Cryptography.CipherMode.CBC;
            aes.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
            var cTransform = aes.CreateEncryptor();
            var resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            res.Data = Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }
        //Byte[] toEncryptArray = Encoding.UTF8.GetBytes(urlStr);
        //System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
        //{
        //    Key = Encoding.UTF8.GetBytes("23147404hunterht23147404hunterht"),
        //    Mode = System.Security.Cryptography.CipherMode.ECB,
        //    Padding = System.Security.Cryptography.PaddingMode.PKCS7
        //};
        //System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateEncryptor();
        //Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
        //res.Data = Convert.ToBase64String(resultArray, 0, resultArray.Length);
        return res;
    }

前端请求

function getUserInfo() {
    var para;
    $.post("/Home/SendInfoTONPIJavaSys", function (data) {
        //var src = "http://localhost:8080/NPIForF1/.AES?" + escape(data);
        var src = "http://localhost:8080/NPIForF1/.AES?" + data;
        var strContent = '';
        //console.log('DMgZqlQaIzFBDj7rmQVXYqyRorHDaYXJUzJc/tY6M3H40k+MWyOHfA1tEDNTk7gj' + "  data");
        //console.log(encodeURI('DMgZqlQaIzFBDj7rmQVXYqyRorHDaYXJUzJc/tY6M3H40k+MWyOHfA1tEDNTk7gj') + "  encodeURI");
        $("#tabs").append(strContent);
        //document.getElementById("tabs").removeChild(document.getElementById("vimainFrame"));
        //$.getJSON("http://localhost:8080/NPIForF1?callback=?&" + data+".AES.", function () {

        //})
    });
}

Java解密

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        
        Map paramterMap = new HashMap();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String dateStr = sdf.format(new Date());
        //拦截加密URL参数值
        String encryptionUrlParameter = req.getQueryString();
        //拦截URL
        String url = req.getRequestURI().replace(req.getContextPath(), "");
        
        //解密URL参数
        //参数类型 UserNO=F1200000&Token=JavaWebSYS&Time=20170621114934
        if(encryptionUrlParameter != null && !encryptionUrlParameter.equals("")){
                //String urlstr = URLDecoder.decode(encryptionUrlParameter, "UTF-8");
            String urlstr = encryptionUrlParameter;
                String paramter=CommonUtils.getDecodeStr(urlstr,"23147404hunterht23147404hunterht");
                //获取加密后的参数
                if(!"".equals(paramter)){
                    String[] paramters = paramter.split("&");
                    for (String string : paramters) {
                        String[] value = string.split("=");
                        paramterMap.put(value[0], value[1]);
                    }
                    if(paramterMap.size()>0){
                        String time = paramterMap.get("Time");
                        Long timeDifference =Long.parseLong(dateStr) - Long.parseLong(time);
                        if(timeDifference<3){
                            String UserNO = paramterMap.get("UserNO");
                            req.getSession().setAttribute("SysUserNO", UserNO);
                            req.getRequestDispatcher(url+ "?" +paramter).forward(req, response);
                        }else{
                            //页面未找到异常
                            throw new PageNotFindException();
                        }
                         
                    }
                }
//          
        }else{
            chain.doFilter(req, response);
        }
    }



package com.foxconn.system.util;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;



import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.InvalidAlgorithmParameterException;
import java.security.spec.AlgorithmParameterSpec;
import java.util.Base64;



/**
 * 
  • AES加密解密 *
  • @author 作者 : H2601977 *
  • @version 创建时间:2017年6月21日 上午10:46:40 */ public class AES256Cipher { public static byte[] ivBytes = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; private static final String SECURITY_KEY = "qc5644abd868x36fr16a57a7fbe8ss1x"; private AES256Cipher(){} /** * 加密 使用系统默认KEY * * @param str * @return * @throws InvalidKeyException * @throws UnsupportedEncodingException * @throws NoSuchAlgorithmException * @throws NoSuchPaddingException * @throws InvalidAlgorithmParameterException * @throws IllegalBlockSizeException * @throws BadPaddingException */ public static String AES_Encode(String str) throws InvalidKeyException, UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException { return AES_Encode(str, SECURITY_KEY); } /** * 解密 使用系统默认KEY * * @param str * @return * @throws InvalidKeyException * @throws UnsupportedEncodingException * @throws NoSuchAlgorithmException * @throws NoSuchPaddingException * @throws InvalidAlgorithmParameterException * @throws IllegalBlockSizeException * @throws BadPaddingException */ public static String AES_Decode(String str) throws InvalidKeyException, UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException { return AES_Decode(str, SECURITY_KEY); } /** * * @Desc:加密 * @param str * @param key * @return * @throws java.io.UnsupportedEncodingException * @throws NoSuchAlgorithmException * @throws NoSuchPaddingException * @throws InvalidKeyException * @throws InvalidAlgorithmParameterException * @throws IllegalBlockSizeException * @throws BadPaddingException * @CreatedBy H2601977 * @CreatedTime 2017年6月21日 上午10:45:09 * @EditBy * @EditTime */ public static String AES_Encode(String str, String key) throws java.io.UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException { byte[] textBytes = str.getBytes("UTF-8"); AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes); SecretKeySpec newKey = new SecretKeySpec(key.getBytes("UTF-8"), "AES"); Cipher cipher = null; cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, newKey, ivSpec); return Base64.getEncoder().encodeToString(cipher.doFinal(textBytes));//此处使用BASE64做转码功能,同时能起到2次加密的作用。 } /** * * @Desc: 解密 * @param str * @param key * @return * @throws java.io.UnsupportedEncodingException * @throws NoSuchAlgorithmException * @throws NoSuchPaddingException * @throws InvalidKeyException * @throws InvalidAlgorithmParameterException * @throws IllegalBlockSizeException * @throws BadPaddingException * @CreatedBy H2601977 * @CreatedTime 2017年6月21日 上午10:44:56 * @EditBy * @EditTime */ public static String AES_Decode(String str, String key) throws java.io.UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException { byte[] textBytes = Base64.getDecoder().decode(str); // byte[] textBytes = str.getBytes("UTF-8"); AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes); SecretKeySpec newKey = new SecretKeySpec(key.getBytes("UTF-8"), "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");//"算法/模式/补码方式" cipher.init(Cipher.DECRYPT_MODE, newKey, ivSpec); return new String(cipher.doFinal(textBytes), "UTF-8"); } }
  • 你可能感兴趣的:(Java、C#互传账号信息)