本文实例为大家分享了java解密 微信小程序 手机号 的具体代码,供大家参考,具体内容如下
第一步: 创建aes解密工具类:代码如下
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
import org.apache测试数据mons.codec.binary.base64; import javax.crypto.cipher; import javax.crypto.keygenerator; import javax.crypto.spec.ivparameterspec; import javax.crypto.spec.secretkeyspec; import java.security.algorithmparameters; import java.security.key; import java.security.security;
public class aes { // 算法名 public static final string key_name = "aes" ; // 加解密算法/模式/填充方式 // ecb模式只用密钥即可对数据进行加密解密,cbc模式需要添加一个iv public static final string cipher_algorithm = "aes/cbc/pkcs7padding" ;
/** * 微信 数据解密<br/> * 对称解密使用的算法为 aes-128-cbc,数据采用pkcs#7填充<br/> * 对称解密的目标密文:encrypted=base64_decode(encryptdata)<br/> * 对称解密秘钥:key = base64_decode(session_key),aeskey是16字节<br/> * 对称解密算法初始向量:iv = base64_decode(iv),同样是16字节<br/> * * @param encrypted 目标密文 * @param session_key 会话id * @param iv 加密算法的初始向量 */ public static string wxdecrypt(string encrypted, string session_key, string iv) { string json = null ; byte [] encrypted64 = base64.decodebase64(encrypted); byte [] key64 = base64.decodebase64(session_key); byte [] iv64 = base64.decodebase64(iv); byte [] data; try { init(); json = new string(decrypt(encrypted64, key64, generateiv(iv64))); } catch (exception e) { e.printstacktrace(); } return json; }
/** * 初始化密钥 */ public static void init() throws exception { security.addprovider( new org.bouncycastle.jce.provider.bouncycastleprovider()); keygenerator.getinstance(key_name).init( 128 ); }
/** * 生成iv */ public static algorithmparameters generateiv( byte [] iv) throws exception { // iv 为一个 16 字节的数组,这里采用和 ios 端一样的构造方法,数据全为0 // arrays.fill(iv, (byte) 0x00); algorithmparameters params = algorithmparameters.getinstance(key_name); params.init( new ivparameterspec(iv)); return params; }
/** * 生成解密 */ public static byte [] decrypt( byte [] encrypteddata, byte [] keybytes, algorithmparameters iv) throws exception { key key = new secretkeyspec(keybytes, key_name); cipher cipher = cipher.getinstance(cipher_algorithm); // 设置为解密模式 cipher.init(cipher.decrypt_mode, key, iv); return cipher.dofinal(encrypteddata); }
} |
第二步: 接口调用
接收参数: encrypted session_key iv
|
1 2 3 4 5 6 |
public string decodeuserinfo(string encrypted, string session_key, string iv) throws ioexception {
string json = wxdecrypt(encrypted, session_key, iv); system.out.println(json); return json; } |
官方文档: 链接地址
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
原文链接:https://blog.csdn.net/Elion_jia/article/details/79551781
查看更多关于Java解密微信小程序手机号的方法的详细内容...