|
@ -0,0 +1,182 @@
|
|
|
|
package com.yihu.wlyy.util;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Riva双向加密,解密算法实现
|
|
|
|
* @author huangwenjie
|
|
|
|
* @date 2018/1/27 14:36
|
|
|
|
*/
|
|
|
|
public class RivaEDCode {
|
|
|
|
/**
|
|
|
|
* 16进制转换成10进制
|
|
|
|
*
|
|
|
|
* @param hexString 要进行转换的16进制数字字符串
|
|
|
|
* @return Sring
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public static String hextoString(String hexString)
|
|
|
|
{
|
|
|
|
|
|
|
|
String hexStr = "";
|
|
|
|
|
|
|
|
int i;
|
|
|
|
int k = 0;
|
|
|
|
|
|
|
|
int str1 = 0;
|
|
|
|
int str2 = 0;
|
|
|
|
|
|
|
|
// 每次取得两个16进制数字字符
|
|
|
|
char str3 = hexString.charAt(0);
|
|
|
|
char str4 = hexString.charAt(1);
|
|
|
|
|
|
|
|
// 定义16进制字符所对应的数值,每个16进制数值对应为数组的索引值
|
|
|
|
char myarray[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
|
|
|
|
|
|
|
|
for(i = 0; i < myarray.length; i++)
|
|
|
|
{
|
|
|
|
// 对应16进制字符所对应的数值
|
|
|
|
if(myarray[i] == str3)
|
|
|
|
{
|
|
|
|
str1 = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(myarray[i] == str4)
|
|
|
|
{
|
|
|
|
str2 = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 换算成10进制数字
|
|
|
|
k = str1 * 16 + str2;
|
|
|
|
|
|
|
|
hexStr = Integer.toString(k);
|
|
|
|
|
|
|
|
return hexStr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param encode_str1 字符串一
|
|
|
|
* @param encode_str2 需要加密的字符串
|
|
|
|
* @return String 加密后的字符串
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public static String uncPassEncode(String encode_str1, String encode_str2)
|
|
|
|
{
|
|
|
|
|
|
|
|
String passString = "";
|
|
|
|
|
|
|
|
String str = "";
|
|
|
|
int userLens = encode_str1.length();
|
|
|
|
int passLens = encode_str2.length();
|
|
|
|
|
|
|
|
int userPos = 0;
|
|
|
|
int passPos = 0;
|
|
|
|
|
|
|
|
while (passPos < passLens)
|
|
|
|
{
|
|
|
|
|
|
|
|
char acii1 = encode_str2.charAt(passPos);
|
|
|
|
char acii2 = encode_str1.charAt(userPos);
|
|
|
|
|
|
|
|
str += Integer.toHexString(acii1 + acii2);
|
|
|
|
userPos++;
|
|
|
|
|
|
|
|
if(userPos >= userLens)
|
|
|
|
userPos = 0;
|
|
|
|
passPos++;
|
|
|
|
}
|
|
|
|
|
|
|
|
passString = str.toUpperCase();
|
|
|
|
|
|
|
|
return passString;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 解密用riva加密算法加密的字符串
|
|
|
|
*
|
|
|
|
* @param encode_str1 字符串
|
|
|
|
* @param encode_str2 需要解密的密文
|
|
|
|
* @return String 解密的明文
|
|
|
|
* @throws Exception
|
|
|
|
* @throws NumberFormatException
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public static String uncPassDecode(String encode_str1, String encode_str2)
|
|
|
|
{
|
|
|
|
|
|
|
|
String decodePassStr = "";
|
|
|
|
// String en1;
|
|
|
|
// String en2;
|
|
|
|
|
|
|
|
int userLens = encode_str1.length();
|
|
|
|
int passLens = encode_str2.length();
|
|
|
|
|
|
|
|
int userPos = 0;
|
|
|
|
int passPos = 0;
|
|
|
|
|
|
|
|
while (passPos < passLens - 1)
|
|
|
|
{
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 16进制转换成10进制
|
|
|
|
*/
|
|
|
|
int str1 = Integer.parseInt(hextoString(encode_str2.substring(passPos, passPos + 2)));
|
|
|
|
|
|
|
|
int str2 = encode_str1.charAt(userPos);
|
|
|
|
|
|
|
|
decodePassStr += String.valueOf((char)(str1 - str2));
|
|
|
|
|
|
|
|
userPos++;
|
|
|
|
|
|
|
|
if(userPos >= userLens)
|
|
|
|
userPos = 0;
|
|
|
|
|
|
|
|
passPos += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return decodePassStr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 双重解密riva算法加密的密文
|
|
|
|
*
|
|
|
|
* @param str1 字符串1
|
|
|
|
* @param str2 字符串2
|
|
|
|
* @param key 加密的密文
|
|
|
|
* @return String 解密后的明文
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public static String passDecode(String str1, String str2, String key)
|
|
|
|
{
|
|
|
|
|
|
|
|
return uncPassDecode(str1, uncPassDecode(str2, key));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 利用riva加密算法双重加密字符串
|
|
|
|
*
|
|
|
|
* @param str1 字符串1
|
|
|
|
* @param key 需要加密字符串
|
|
|
|
* @param str2 字符串2
|
|
|
|
* @return String 经过加密的字符串
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public static String passEncode(String str1, String str2, String key)
|
|
|
|
{
|
|
|
|
|
|
|
|
return uncPassEncode(str2, uncPassEncode(str1, key));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//测试-加密解密
|
|
|
|
public static void main(String[] args)
|
|
|
|
{
|
|
|
|
//解密
|
|
|
|
// System.out.println(RivaEDCode.passDecode("LUZ7TN3KOT8AWCD3ZA4NBMI5VNF7E50F6XYEP2WZM68JQYY5JE02L4L5FS9R4NGUGMHSCAPW9AL","C3SHUI8OWBOA4ZASS7FEYJ6RIVXA9SW6U5OA56ERUYZTRFHCRZO8AHT4TTW2MAGT80MGXN","7A778B7F8D8D6E918F7B87756A9B"));
|
|
|
|
|
|
|
|
//加密
|
|
|
|
// System.out.println(RivaEDCode.passEncode("LUZ7TN3KOT8AWCD3ZA4NBMI5VNF7E50F6XYEP2WZM68JQYY5JE02L4L5FS9R4NGUGMHSCAPW9AL","C3SHUI8OWBOA4ZASS7FEYJ6RIVXA9SW6U5OA56ERUYZTRFHCRZO8AHT4TTW2MAGT80MGXN","1234567"));
|
|
|
|
}
|
|
|
|
}
|