위 챗 애플 릿 암호 화 데이터 복호화 자바 구현
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;
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
콩짜개 검색 - 위 챗 공공 플랫폼 접속 (wechatpy)위의 글 은 위 챗 공공 플랫폼 을 어떻게 연결 하 는 지 소 개 했 지만 그 안의 검증 코드 는 우리 가 스스로 실현 한 것 이다.그러나 지금 우 리 는 더 좋 은 선택 이 생 겼 다.위 챗 (WeChat) 퍼 블...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.