hz-zhhq-app-service/greenH5modul/.svn/pristine/fb/fba7713b26e1cc74a4148d3cd0f...

49 lines
1.8 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 必须是intJAVA位移处理为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");
}
}