小程序获取用户绑定的电话号码及解密方法(提供Java后台解密)

小程序前台代码

<button open-type="getPhoneNumber" bindgetphonenumber="telPhone" class="id_style">请填写手机号码
</button>

.id_style {
  padding-right: 0rpx;
  padding-left: 0rpx;
  background-color: #fff;
  font-size: 30rpx;
  text-align: right;
  line-height: 72rpx;
  border: none;
  color: #888;
}

Java后台代码

工具类

package com.ods.common.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import it.sauronsoftware.base64.Base64;  

public class Base64Util {
    /** *//** 
     * 文件读取缓冲区大小 
     */  
    private static final int CACHE_SIZE = 1024;  
      
    /** *//** 
     * <p> 
     * BASE64字符串解码为二进制数据 
     * </p> 
     *  
     * @param base64 
     * @return 
     * @throws Exception 
     */  
    public static byte[] decode(String base64) throws Exception {  
        return Base64.decode(base64.getBytes());  
    }  
      
    /** *//** 
     * <p> 
     * 二进制数据编码为BASE64字符串 
     * </p> 
     *  
     * @param bytes 
     * @return 
     * @throws Exception 
     */  
    public static String encode(byte[] bytes) throws Exception {  
        return new String(Base64.encode(bytes));  
    }  
      
    /** *//** 
     * <p> 
     * 将文件编码为BASE64字符串 
     * </p> 
     * <p> 
     * 大文件慎用,可能会导致内存溢出 
     * </p> 
     *  
     * @param filePath 文件绝对路径 
     * @return 
     * @throws Exception 
     */  
    public static String encodeFile(String filePath) throws Exception {  
        byte[] bytes = fileToByte(filePath);  
        return encode(bytes);  
    }  
      
    /** *//** 
     * <p> 
     * BASE64字符串转回文件 
     * </p> 
     *  
     * @param filePath 文件绝对路径 
     * @param base64 编码字符串 
     * @throws Exception 
     */  
    public static void decodeToFile(String filePath, String base64) throws Exception {  
        byte[] bytes = decode(base64);  
        byteArrayToFile(bytes, filePath);  
    }  
      
    /** *//** 
     * <p> 
     * 文件转换为二进制数组 
     * </p> 
     *  
     * @param filePath 文件路径 
     * @return 
     * @throws Exception 
     */  
    public static byte[] fileToByte(String filePath) throws Exception {  
        byte[] data = new byte[0];  
        File file = new File(filePath);  
        if (file.exists()) {  
            FileInputStream in = new FileInputStream(file);  
            ByteArrayOutputStream out = new ByteArrayOutputStream(2048);  
            byte[] cache = new byte[CACHE_SIZE];  
            int nRead = 0;  
            while ((nRead = in.read(cache)) != -1) {  
                out.write(cache, 0, nRead);  
                out.flush();  
            }  
            out.close();  
            in.close();  
            data = out.toByteArray();  
         }  
        return data;  
    }  
      
    /** *//** 
     * <p> 
     * 二进制数据写文件 
     * </p> 
     *  
     * @param bytes 二进制数据 
     * @param filePath 文件生成目录 
     */  
    public static void byteArrayToFile(byte[] bytes, String filePath) throws Exception {  
        InputStream in = new ByteArrayInputStream(bytes);     
        File destFile = new File(filePath);  
        if (!destFile.getParentFile().exists()) {  
            destFile.getParentFile().mkdirs();  
        }  
        destFile.createNewFile();  
        OutputStream out = new FileOutputStream(destFile);  
        byte[] cache = new byte[CACHE_SIZE];  
        int nRead = 0;  
        while ((nRead = in.read(cache)) != -1) {     
            out.write(cache, 0, nRead);  
            out.flush();  
        }  
        out.close();  
        in.close();  
    }  
}

解析方法

	/**
	 * 解密用户电话
	 * @param keyStr sessionkey
	 * @param ivStr  ivData
	 * @param encDataStr 带解密数据
	 * @return
	 * @throws Exception 
	 * @author pangxianhe
	 * @date 2018年10月22日
	 */
	public static String decrypt(String keyStr, String ivStr, String encDataStr)throws Exception {
			
			byte[] encData = Base64Util.decode(encDataStr);
			byte[] iv =Base64Util.decode(ivStr);
			byte[] key = Base64Util.decode(keyStr);
			AlgorithmParameterSpec ivSpec = new IvParameterSpec(iv);
			Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
			SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
			cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
			 return new String(cipher.doFinal(encData),"UTF-8");
	}
	




public static void main(String[] args) throws Exception {
    String  encrypData = "dhxK6mXwVPUabiK9i78OP4bmjg1YEYlQNIimEm5WEU6PxB/nexMk+CeBgsH9FFI2v9ikDAjGShPjZJj4xsRkbOH4otOHB3GumTRzh9ZYxXlQyrOnyL2//Yr2AL/KYW47n3JxqeeiVP9y/5JDPcY5HAM4/XDV3SzROI730aItFdHmDhc0/CyDLH1WDzv8y24J9Z/eyv+GzP9rTpdKjN4DA==";
    String ivData =  "ZwRBTP8AtevYJtbLLh9Lg==";
    String sessionKey =  "3LL836kTRzaJ5llVmRN6w==";
    System.out.println(decrypt(sessionKey,ivData,encrypData));
}

返回:

{"phoneNumber":"15000000000000","purePhoneNumber":"15000000000000","countryCode":"86","watermark":{"timestamp":1540212690,"appid":"wxe48a00000000000"}}

注意:由于以上的字符串已经过修改,请填写正确的参数,然后进行解密

 

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值