위 챗 애플 릿 암호 화 데이터 복호화 자바 구현

5569 단어 작은 편지기초
먼저 두 편의 문장 을 참고 했다.
http://www.cnblogs.com/nosqlcoco/p/6105749.html
http://blog.csdn.net/sinat_29519243/article/details/70186622
우선 위 챗 애플 릿 이라는 디자인 은 사실 비밀문서 에 포 함 된 개발 에 유용 한 정보 가 많 지 않 습 니 다.
복호화 후의 유사 성:
{"openId":"oy9H90Nqxxxxxxxxxxx0BJmuw",
"nickName":"xxxxxxxxx",
"gender":1,
"language":"zh_CN",
"city":"city",
"province":"province",
"country":"country",
"avatarUrl":"https://wx.qlogo.cn/mmopen/vi_32/xxxxxxxxOcvbibeJxx0",
"watermark":{"timestamp":timestamp,"appid":"wx58b6xxxxxxxxx627"
}
복호화 하려 면 로그 인 할 때 제공 하 는 몇 가지 인자:
1. 암호 화 된 데이터
2. session_key
3. 오프셋 벡터 iv
로그 인 한 몇 가지 물건 을 어떻게 얻 는 지 간단히 말 해 보 세 요.
1. session ID 가 져 오기: wx. login () 함수 의 반환 에는 CODE 가 포함 되 어 있 습 니 다. 이 CODE 를 이용 하여 이 주소 로 교환 합 니 다.
https://api.weixin.qq.com/sns/jscode2session?grant_type=authorization_code&js_code=CODE&appid=APPID&secret=APP_SRCRET。
2. iv 와 encrypted Data 의 획득: wx. getUser Info () 를 호출 할 때 속성 with Credentials: true 를 설정 합 니 다.
wx.getUserInfo({ withCredentials: true, success: function(res) { console.log(res) that.globalData.userInfo = res.userInfo typeof cb == "function" && cb(that.globalData.userInfo) } })

모든 인 자 를 얻 을 수 있 습 니 다.
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 분할 선 = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
자바 측 에서 복호화 하려 면 다음 과 같은 가방 이 필요 합 니 다:
1. bcprov - jdk15on - 157. jar --- - 주로 AES 디 코딩
2. comons - codec - 1.10. jar --- - 주로 base 64 인 코딩
핵심 코드:
 
Map map = new HashMap();  
       try {  
               byte[] resultByte  = AES.decrypt(Base64.decodeBase64(encryptedData),  
                       Base64.decodeBase64(session_key),
                       Base64.decodeBase64(iv));  
                   if(null != resultByte && resultByte.length > 0){  
                       String userInfo = new String(resultByte, "UTF-8");                 
                       map.put("status", "1");  
                       map.put("msg", "    ");                 
                       map.put("userInfo", userInfo);  
                   }else{  
                       map.put("status", "0");  
                       map.put("msg", "    ");  
                   }  
           }catch (InvalidAlgorithmParameterException e) {  
                   e.printStackTrace();  
           } catch (UnsupportedEncodingException e) {  
                   e.printStackTrace();  
           }                
           Gson gson = new Gson();  
           String decodeJSON = gson.toJson(map);  
           System.out.println(decodeJSON); 

그 속
AES 코드:
package com.aes;


import org.bouncycastle.jce.provider.BouncyCastleProvider;  
import java.security.AlgorithmParameters;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Security;


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;


public class AES {
public static boolean initialized = false;    
    
    /** 
     * AES   
     * @param content    
     * @return 
     * @throws InvalidAlgorithmParameterException  
     * @throws NoSuchProviderException  
     */  
    public static byte[] decrypt(byte[] content, byte[] keyByte, byte[] ivByte) throws InvalidAlgorithmParameterException {  
        initialize();  
        try {  
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");  
            Key sKeySpec = new SecretKeySpec(keyByte, "AES");  
              
            cipher.init(Cipher.DECRYPT_MODE, sKeySpec, generateIV(ivByte));//       
            byte[] result = cipher.doFinal(content);  
            return result;  
        } catch (NoSuchAlgorithmException e) {  
            e.printStackTrace();    
        } catch (NoSuchPaddingException e) {  
            e.printStackTrace();    
        } catch (InvalidKeyException e) {  
            e.printStackTrace();  
        } catch (IllegalBlockSizeException e) {  
            e.printStackTrace();  
        } catch (BadPaddingException e) {  
            e.printStackTrace();  
        } catch (NoSuchProviderException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        } catch (Exception e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
        return null;  
    }    
      
    public static void initialize(){    
        if (initialized) return;    
        Security.addProvider(new BouncyCastleProvider());    
        initialized = true;    
    }  
    //  iv    
    public static AlgorithmParameters generateIV(byte[] iv) throws Exception{    
        AlgorithmParameters params = AlgorithmParameters.getInstance("AES");    
        params.init(new IvParameterSpec(iv));    
        return params;    
    }     


}

좋은 웹페이지 즐겨찾기