package algorithm;
import algorithmUtil.DESUtil;
/**
* DES算法的实现类
*/
public class DES {
public byte[] desEncrypt(byte[] input, byte[][] loopKeys) {
byte[] encryptResult; /* 加密结果 */
byte[] it = initialReplace(input);
byte[] temp;
temp = feistelFunction(it, loopKeys[0], true);
for(int i = 1;i < 15;i++)
{
temp = feistelFunction(temp, loopKeys[i], true);
}
temp = feistelFunction(temp, loopKeys[15], false);
encryptResult = initialReplaceReverse(temp);
return encryptResult;
}
/**
* 轮密钥生成函数
* @param initKey
* @return
*/
public byte[][] getLoopKey(byte[] initKey)
{
byte[][] P1Result = new byte[16][56];
byte[][] loopKeyResult = new byte[16][48];
byte[] P1Key1 = PC1_KeyReplace(initKey);
P1Result[0] = moveLeftLoop(P1Key1, DESUtil.MoveLeft[0]);
for(int i = 1;i < 16;i++)
{
P1Result[i] = moveLeftLoop(P1Result[i - 1], DESUtil.MoveLeft[i]);
}
for (int i = 0;i < 16;i++)
{
loopKeyResult[i] = PC2_KeyReplace(P1Result[i]);
}
return loopKeyResult;
}
/**
* 初始置换
* @param m
* @return
*/
private byte[] initialReplace(byte[] m)
{
byte[] mResult = new byte[64];
for (int i = 0; i < 64; i++)
{
mResult[i] = m[DESUtil.IP[i] - 1];
}
return mResult;
}
/**
* F函数
* @param input
* @param loopKey 轮密钥
* @param direction 控制左右两半
* @return
*/
private byte[] feistelFunction(byte[] inputData, byte[] roundKey, boolean direction)
{
byte[] result = new byte[64];
byte[] L = new byte[32];
byte[] R = new byte[32];
byte[] RResult;
for(int i = 0;i < 32;i++)
{
L[i] = inputData[i];
R[i] = inputData[i + 32];
}
byte[] eR = eExtend(R);
byte[] xR48 = xor(eR, roundKey, 48);
byte[] sR = sBoxReplace(xR48);
byte[] pR = pReplace(sR);
RResult = xor(L, pR, 32);
if(direction)
{
for(int i = 0;i < 32;i++)
{
result[i] = R[i];
result[i + 32] = RResult[i];
}
}
else
{
for(int i = 0;i < 32;i++)
{
result[i] = RResult[i];
result[i + 32] = R[i];
}
}
return result;
}
/**
* E盒扩展
* @param r
* @return
*/
private byte[] eExtend(byte[] r)
{
byte[] eResult = new byte[48];
for (int i = 0; i < 48; i++)
{
eResult[i] = r[DESUtil.E[i] - 1];
}
return eResult;
}
/**
* S盒代替
* @param rResult
* @return
*/
private byte[] sBoxReplace(byte[] rResult)
{
byte[][] input = new byte[8][6];
byte[] result = new byte[32];
for(int i = 0;i < 8;i++)
{
for(int j = 0;j < 6;j++)
{
input[i][j] = rResult[6 * i + j];
}
int column = 2*input[i][0] + input[i][5];
int row = 8*input[i][1] + 4*input[i][2] + 2*input[i][3] + input[i][4];
int temp = DESUtil.SBox[i][column][row];
result[4 * i + 3] = (byte) (temp % 2);
temp /= 2;
result[4 * i + 2] = (byte) (temp % 2);
temp /= 2;
result[4 * i + 1] = (byte) (temp % 2);
temp /= 2;
result[4 * i] = (byte) (temp % 2);
}
return result;
}
/**
* P盒置换
* @param sResult
* @return
*/
private byte[] pReplace(byte[] sResult)
{
byte[] pResult = new byte[32];
for (int i = 0;i < 32;i++)
{
pResult[i] = sResult[DESUtil.P[i] - 1];
}
return pResult;
}
private byte[] initialReplaceReverse(byte[] rl)
{
byte[] mResult = new byte[64];
for (int i = 0; i < 64; i++){
mResult[i] = rl[DESUtil.InverseIP[i] - 1];
}
return mResult;
}
/**
* 轮密钥生成的置换选择1
* @param initKey
* @return
*/
private byte[] PC1_KeyReplace(byte[] initKey)
{
byte[] PC1_Key = new byte[56];
for(int i = 0;i < 56;i++)
{
PC1_Key[i] = initKey[DESUtil.PC1[i] - 1];
}
return PC1_Key;
}
/**
* 轮密钥生成的置换选择2
* @param PC1_Result
* @return
*/
private byte[] PC2_KeyReplace(byte[] PC1_Result)
{
byte[] PC2_Key = new byte[48];
for(int i = 0;i < 48;i++)
{
PC2_Key[i] = PC1_Result[DESUtil.PC2[i] - 1];
}
return PC2_Key;
}
/**
* 轮密钥生成的循环左移
* @param input
* @param index
* @return
*/
private byte[] moveLeftLoop(byte[] input,int index)
{
byte[] moveResult = new byte[56];
for (int i = 0;i < 28;i++)
{
moveResult[i] = input[(i + index) % 28];
}
for (int i = 28;i < 56;i++)
{
moveResult[i] = input[(i + index) % 28 + 28];
}
return moveResult;
}
/**
* 异或
* @param leftOp
* @param rightOp
* @param length
* @return
*/
private byte[] xor(byte[] leftOp,byte[] rightOp,int length)
{
byte[] x = new byte[length];
for (int i = 0; i < length; i++){
x[i] = (byte)(leftOp[i] ^ rightOp[i]);
}
return x;
}
/**
* 二进制字节数组转换为字符串
* @param bytes
* @return
*/
public String byte2BinaryStr(byte[] bytes)
{
String result = "";
for (byte b: bytes)
{
result += b == 0 ?"0":"1";
}
return result;
}
}
DES.zip_des_unioneaa
版权申诉
112 浏览量
2022-09-24
10:40:37
上传
评论
收藏 3KB ZIP 举报
局外狗
- 粉丝: 67
- 资源: 1万+
最新资源
- 基于JavaScript和CSS的随寻订购网页设计源码 - web-order
- 基于MATLAB的声纹识别系统设计源码 - VoiceprintRecognition
- 基于Java的微服务插件集合设计源码 - wsy-plugins
- 基于Vue和微信小程序的监理日志系统设计源码 - supervisionLog
- 基于Java和LCN分布式事务框架的设计源码 - tx-lcn
- 基于Java和JavaScript的茶叶评级管理系统设计源码 - tea
- IMG_5680.JPG
- IMG_0437.jpg
- 基于Java的JAVA项目分析工具设计源码 - JAVAProjectAnalysis
- top888.json
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈