您的位置:首页 > 编程语言 > Delphi

Delphi的(xor)转成java(xor),以及加密解密

2020-06-07 05:08 2271 查看

1.xor —> 异或算法的加密原理:
一个整数 a 和任意一个整数 b 异或两次,得到的结果是整数 a 本身,即: a == a ^ b ^ b。

这里的 a 就是需要加密的原数据,b 则是密钥。a ^ b 就是加密过程,异或的结果就是加密后的密文。密文 (a ^ b) 再与密钥 b 异或,就是解密过程,得到的结果就是原数据 a 本身。

a = 原数据
b = 密钥

// 一次异或, 加密得到密文
c = a ^ b

// 二次异或, 解密得到原数据(d == a)
d = c ^ b

2.代码实例
2.1 xor加密工具类封装:XorUtils
如图所示:


代码如下:

package com.xinqiao.utils;

public class XorUtils {

//异或算法(xor)
//定义成byte字节的数组(8个字节16进制)   XOR 加密/解密用的原始密码
private static byte[] cipKey = {(byte)0xAA,(byte)0x55,(byte)0x22,(byte)0x11,(byte)0xBB,(byte)0x66,(byte)0xEE,(byte)0x77};

/**
* 异或算法加密/解密
* @param data  数据(密文/明文)
* @param key  密钥
* @return 返回解密/加密后的数据
*/
public static  byte[] encrypt(byte[] data, byte[] key) {
if (data == null || data.length == 0 || key == null || key.length == 0) {
return data;
}
byte[] result = new byte[data.length];
// 使用密钥字节数组循环加密或解密
for (int i = 0; i < data.length; i++) {
// 输入的字符串第 i 位与密钥中第 i 位进行异或
result[i] = (byte) (data[i] ^ key[i]);
}
return result;
}

// 加密数据, 返回密文
public static  String byteHex(String content) {
byte[] cipherBytes = encrypt(content.getBytes(), cipKey);
String byteToHex = byteToHex(cipherBytes);
return byteToHex.toUpperCase();
}

// 解密数据, 返回明文
public static  String getPwd(String byteToHex) {
byte[] hexToByte = hexToByte(byteToHex);
byte[] plainBytes = encrypt(hexToByte, cipKey);
return new String(plainBytes);
}

// byte[] 转换成16进制。
public static  String byteToHex(byte[] bytes) {
String strHex = "";
StringBuilder sb = new StringBuilder("");
for (int n = 0; n < bytes.length; n++) {
strHex = Integer.toHexString(bytes
 & 0xFF);
sb.append((strHex.length() == 1) ? "0" + strHex : strHex); // 每个字节由两个字符表示,位数不够,高位补0
}
return sb.toString().trim();
}

/**
* hex转byte数组 16转byte[]
* @param hex
* @return
*/
public static  byte[] hexToByte(String hex) {
int m = 0, n = 0;
int byteLen = hex.length() / 2; // 每两个字符描述一个字节
byte[] ret = new byte[byteLen];
for (int i = 0; i < byteLen; i++) {
m = i * 2 + 1;
n = m + 1;
int intVal = Integer.decode("0x" + hex.substring(i * 2, m) + hex.substring(m, n));
ret[i] = Byte.valueOf((byte) intVal);
}
return ret;
}

}

3.调用方式:
如图所示:

代码如下:

package com.xinqiao.user.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xinqiao.utils.XorUtils;

@CrossOrigin
@Controller
@RequestMapping(value="/user")
public class UserController {

@RequestMapping(value="/toUserLogin")
public ModelAndView toUserLogin( Model model,HttpServletRequest req){
ModelAndView mode = new ModelAndView();
mode.setViewName("/main/user_login");
return mode;
}

@RequestMapping(value="/xorData")
@ResponseBody
public Map<String, String> xorData(
@RequestParam(value = "userName", required = false) String userName){
Map<String, String> data = new HashMap<String, String>();
String aa = XorUtils.byteHex(userName); //xor加密,解密
data.put("data", aa);
return data;
}

}

备注1:xor方式调用可以选择main或者controller层调用byteHex(参数)加密的方法,解密getPwd(参数)方法。

4.Delphi的代码如下;
如图所示:

备注2:以上就是java代码的异或算法和Delphi代码的异或算法互相转换。得出结果是一致的。

5.测试效果图:(前端页面可自己编写)


备注3:输入字符串aa,点击确定即可获取密码 为CB34。
当前的CB34就是XOR算法加密的。

总结:以上就是我解决思路。纯属个人见解,记录一下以防万一。如果您有更好的解决方式,欢迎留言指教。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: