49 lines
1.8 KiB
Plaintext
49 lines
1.8 KiB
Plaintext
|
|
package com.nationalelectric.greenH5.utils;
|
|||
|
|
|
|||
|
|
import java.io.UnsupportedEncodingException;
|
|||
|
|
|
|||
|
|
public class EncryptUtil {
|
|||
|
|
public static final int C1 = 52845;
|
|||
|
|
public static final int C2 = 22719;
|
|||
|
|
public static String Encrypt(String mingwen, int Key) throws UnsupportedEncodingException //key=0x0610 加密函数
|
|||
|
|
{
|
|||
|
|
/*
|
|||
|
|
* liuyujing
|
|||
|
|
* 2019-08-29
|
|||
|
|
* Encrypt:餐卡系统二维码加密算法,餐卡系统为C#语言,转JAVA代码
|
|||
|
|
* mingwen:待加密字符串,常规格式:餐卡号_时间戳
|
|||
|
|
* Key:秘钥,Key=0x0610 ,必须是int,JAVA位移处理为32位
|
|||
|
|
* byte在C#中为无符号类型,JAVA的byte为有符号类型
|
|||
|
|
* byte[] szOut 在运算过程需要 & 0xFF 转为 无符号类型
|
|||
|
|
*/
|
|||
|
|
|
|||
|
|
byte[] str = new byte[2];
|
|||
|
|
int length = mingwen.length();
|
|||
|
|
byte[] szOut = new byte[length];
|
|||
|
|
byte[] enCode = new byte[length * 2];
|
|||
|
|
int i,j;
|
|||
|
|
byte[] S = mingwen.getBytes();
|
|||
|
|
//szOut = S;
|
|||
|
|
// 初始化结果字符串
|
|||
|
|
for (i = 0; i < length; i++) // 依次对字符串中各字符进行操作
|
|||
|
|
{
|
|||
|
|
szOut[i] = (byte)(S[i] ^ (Key >> 8)); // 将密钥移位后与字符异或
|
|||
|
|
Key = (int)(((szOut[i]& 0xFF) + Key) * C1 + C2); // 产生下一个密钥
|
|||
|
|
//byte为有符号类型,& 0xFF转为无符号,与服务端C#算法统一
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
for (i = 0; i < length; i++) // 对加密结果进行转换
|
|||
|
|
{
|
|||
|
|
j = szOut[i]& 0xFF;
|
|||
|
|
str[0]=(byte)(65+j/26);
|
|||
|
|
str[1]=(byte)(65+j%26);
|
|||
|
|
//System.out.println((byte)(65+j/26));
|
|||
|
|
enCode[2*i] = str[0];
|
|||
|
|
enCode[2*i+1] = str[1];
|
|||
|
|
}
|
|||
|
|
//System.out.println(new String(enCode));
|
|||
|
|
return new String(enCode,"UTF-8");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|