轻源码

  • QingYuanMa.com
  • 全球最大的互联网技术和资源下载平台
搜索
一起源码网 门户 终极进阶 查看主题

Java实现小程序用户信息解密

发布者: antonfxb | 发布时间: 2017-2-26 14:30| 查看数: 4375| 评论数: 1|帖子模式

作者:百姓,来自蜂鸟社区,目前解密demo,已经有官方的
Node,php,Phthon,C++地址:
以及C#的代码:
ruby:
go:

加上这个,已经比较齐全了,值得参考,另外大家可以搜索一下“登录”这个关键词,查看一些文章关于登录的,用于辅助;
关键代码:
public String decrypt(String encryptedData, String sessionKey, String iv, String encodingFormat) throws Exception {
   try {
      Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
      BASE64Decoder base64Decoder = new BASE64Decoder();
      byte[] _encryptedData = base64Decoder.decodeBuffer(encryptedData);
      byte[] _sessionKey = base64Decoder.decodeBuffer(sessionKey);
      byte[] _iv = base64Decoder.decodeBuffer(iv);
      SecretKeySpec secretKeySpec = new SecretKeySpec(_sessionKey, "AES");
      IvParameterSpec ivParameterSpec = new IvParameterSpec(_iv);
      cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);
      byte[] original = cipher.doFinal(_encryptedData);
      byte[] bytes = WxPKCS7Encoder.decode(original);
      String originalString = new String(bytes, encodingFormat);      return originalString;
   } catch (Exception ex) {
      return null;
   }
}
依赖类 WxPKCS7Encoder.javaclass WxPKCS7Encoder {
   static Charset CHARSET = Charset.forName("utf-8");
   static int BLOCK_SIZE = 32;

   /**
    * 获得对明文进行补位填充的字节.
    *
    * @param count 需要进行填充补位操作的明文字节个数
    * @return 补齐用的字节数组
    */
   static byte[] encode(int count) {
      // 计算需要填充的位数
      int amountToPad = BLOCK_SIZE - (count % BLOCK_SIZE);
      if (amountToPad == 0) {
         amountToPad = BLOCK_SIZE;
      }
      // 获得补位所用的字符
      char padChr = chr(amountToPad);
      String tmp = new String();
      for (int index = 0; index < amountToPad; index++) {
         tmp += padChr;
      }
      return tmp.getBytes(CHARSET);
   }

   /**
    * 删除解密后明文的补位字符
    *
    * @param decrypted 解密后的明文
    * @return 删除补位字符后的明文
    */
   static byte[] decode(byte[] decrypted) {
      int pad = (int) decrypted[decrypted.length - 1];
      if (pad < 1 || pad > 32) {
         pad = 0;
      }
      return Arrays.copyOfRange(decrypted, 0, decrypted.length - pad);
   }

   /**
    * 将数字转化成ASCII码对应的字符,用于对明文进行补码
    *
    * @param a 需要转化的数字
    * @return 转化得到的字符
    */
   static char chr(int a) {
      byte target = (byte) (a & 0xFF);
      return (char) target;
   }

}
jdk版本1.8

最新评论

刘唐 发表于 2022-4-26 21:24
源代码怎么做成app软件

轻源码让程序更轻更快

QingYuanMa.com

工作时间 周一至周六 8:00-17:30

侵权处理

客服QQ点击咨询

关注抖音号

定期抽VIP

Copyright © 2016-2021 https://www.171739.xyz/ 滇ICP备13200218号

快速回复 返回顶部 返回列表