⼩程序的加密与解密--java   可能是认为java 对称加密算法做的⽐较好。没有给出java的例⼦。
签名加密:利⽤最简单的AES加密⽅式
关键代码:
public static String encode(String str) {
if (str == null) {
return null;
}
try {
MessageDigest messageDigest = Instance("SHA1");
messageDigest.Bytes("UTF-8"));
return getFormattedText(messageDigest.digest());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
  解密⽅式:
    需要密钥,需要偏移量
关键代码:
/**
* AES解密
*
* @param content 密⽂
* @return
* @throws InvalidAlgorithmParameterException
* @throws NoSuchProviderException
*/
public static byte[] decrypt(byte[] content, byte[] keyByte, byte[] ivByte) throws InvalidAlgorithmParameterException {
js代码加密软件
try {
Cipher cipher = Instance("AES/CBC/PKCS5Padding");
SecretKeySpec sKeySpec = new SecretKeySpec(keyByte, "AES");
cipher.init(Cipher.DECRYPT_MODE, sKeySpec, generateIV(ivByte));
byte[] result = cipher.doFinal(content);
return result;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
整个类:
package com.paic.aimsmon.util;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import dec.binary.Base64;
pto.Cipher;
pto.spec.IvParameterSpec;
pto.spec.SecretKeySpec;
import java.security.AlgorithmParameters;
import java.security.InvalidAlgorithmParameterException;
import java.security.MessageDigest;
import java.security.NoSuchProviderException;
/**
* @author zhongzheng664
* @date 2018/9/26.
*/
public class Sha1Utils {
private static final char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5', '6',
'7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
/**
* 把密⽂转换成⼗六进制的字符串形式
* @param bytes
* @return⼗六进制的字符串
*/
private static String getFormattedText(byte[] bytes) {
int len = bytes.length;
StringBuilder buf = new StringBuilder(len * 2);
// 把密⽂转换成⼗六进制的字符串形式
for (int j = 0; j < len; j++) {
buf.append(HEX_DIGITS[(bytes[j] >> 4) & 0x0f]);
buf.append(HEX_DIGITS[bytes[j] & 0x0f]);
}
String();
}
public static String encode(String str) {
if (str == null) {
return null;
}
try {
MessageDigest messageDigest = Instance("SHA1");
messageDigest.Bytes("UTF-8"));
return getFormattedText(messageDigest.digest());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* AES解密
*
* @param content 密⽂
* @return
* @throws InvalidAlgorithmParameterException
* @throws NoSuchProviderException
*/
public static byte[] decrypt(byte[] content, byte[] keyByte, byte[] ivByte) throws InvalidAlgorithmParameterException {
try {
Cipher cipher = Instance("AES/CBC/PKCS5Padding");
SecretKeySpec sKeySpec = new SecretKeySpec(keyByte, "AES");
cipher.init(Cipher.DECRYPT_MODE, sKeySpec, generateIV(ivByte));
byte[] result = cipher.doFinal(content);
return result;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
// ⽣成iv
public static AlgorithmParameters generateIV(byte[] iv) throws Exception {
AlgorithmParameters params = Instance("AES");
params.init(new IvParameterSpec(iv));
return params;
}
public static void main(String[] agrs) {
String str = "{\"nickName\":\"Band\",\"gender\":1,\"language\":\"zh_CN\",\"city\":\"Guangzhou\",\"province\":\"Guangdong\",\"country\":\"CN\",\"avatarUrl\":\"wx.qlogo/mmopen/vi_32/1vZvI39NWFQ9XM4LtQpFrQJ1xlgZxx3w7bQxKAR //加密
String encode = encode(str);
String session_key = "HyVFkGl5F5OQWJZZaNzBBg==";
String appId = "wx4f4bc4dec97d474b";
String encryptedData = "CiyLU1Aw2KjvrjMdj8YKliAjtP4gsMZMQmRzooG2xrDcvSnxIMXFufNstNGTyaGS9uT5geRa0W4oTOb1WT7fJlAC+oNPdbB+3hVbJSRgv+4lGOETKUQz6OYStslQ142dNCuabNPGBzlooOmB231qMM85d2/fV6ChevvX        String sessionKey = "tiihtNczf5v6AKRyjwEUhQ==";
String iv = "r7BXXKkLb8qrSNn05n0qiA==";
System.out.println(encode);
try {
byte[] resultByte=decrypt(Base64.decodeBase64(encryptedData),Base64.decodeBase64(sessionKey),
Base64.decodeBase64(iv));
String jsonStr=new String(resultByte, "utf-8");
//字符串转json
JSONObject jsonObject= JSON.parseObject(jsonStr);
System.out.println(new String(resultByte, "utf-8"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
View Code

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。