Java分组密码算法DES
1 实验内容
掌握分组密码算法DES方法,能用高级语言实现分组密码算法DES。DES算法为密码体制中的对称密码体制,又被成为美国数据加密标准,是1972年美国IBM公司研制的对称密码体制加密算法。 明文按64位进行分组,密钥长64位,密钥事实上是56位参与DES运算(第8、16、24、32、40、48、56、64位是校验位, 使得每个密钥都有奇数个1)分组后的明文组和56位的密钥按位替代或交换的方法形成密文组的加密方法。
2 分组密码算法DES算法的基本原理
其入口参数有三个:key、data、mode。 key为加密解密使用的密钥,data为加密 des算法结构解密的数据,mode为其工作模式。当模式为加密模式时,明文按照64位进行分组,形成明文组,key用于对数据加密,当模式为解密模式时,key用于对数据解密。实际运用中,密钥只用到了64位中的56位,这样才具有高的安全性
2.1 分组密码算法DES算法概述
2.2 分组密码算法DES算法流程
DES算法把64位的明文输入块变为64位的密文输出块,它所使用的密钥也是64位,整个算法 des算法流程图的主流程图如下
3. 实验结果
❤Java初高级学习分享,共同学习才是最明智的选择,喜欢的话可以我的学习群64弍46衣3凌9,或加资料群69似64陆0吧3(进群备注平台名)
源代码:
public class GetSubKey {
private String inKey;
private StringBuffer keyBuf;
private byte[] key = new byte[64]; // 保存密钥的64位二进制表示
private byte[] kwork = new byte[56];
private static byte[] shift = { 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2,
2, 1 };
protected byte[] k1 = new byte[48];
protected byte[] k2 = new byte[48];
protected byte[] k3 = new byte[48];
protected byte[] k4 = new byte[48];
protected byte[] k5 = new byte[48];
protected byte[] k6 = new byte[48];
protected byte[] k7 = new byte[48];
protected byte[] k8 = new byte[48];
protected byte[] k9 = new byte[48];
protected byte[] k10 = new byte[48];
protected byte[] k11 = new byte[48];
protected byte[] k12 = new byte[48];
protected byte[] k13 = new byte[48];
protected byte[] k14 = new byte[48];
protected byte[] k15 = new byte[48];
protected byte[] k16 = new byte[48];
protected byte[] kn = new byte[48];
public GetSubKey(String inKey) {
byte j;
this.inKey = inKey;
int len = inKey.length();
keyBuf = new StringBuffer(inKey);
if (len <8) { // 密钥必须是8位,若小于8位不够的补空格 ,大于8位取前8位
for (int i = 1; i <= 8 - len; i++)
keyBuf.append("?");
}
inKey = keyBuf.toString();
// 将8个字符的密钥转换成64位二进制表示
for (int i = 0; i <8; i++) {
j = (byte) (inKey.charAt(i));
key[8 * i] = (byte) ((j / 128) % 2);
key[8 * i + 1] = (byte) ((j / 64) % 2);
key[8 * i + 2] = (byte) ((j / 32) % 2);
key[8 * i + 3] = (byte) ((j / 16) % 2);
key[8 * i + 4] = (byte) ((j / 8) % 2);
key[8 * i + 5] = (byte) ((j / 4) % 2);
key[8 * i + 6] = (byte) ((j / 2) % 2);
key[8 * i + 7] = (byte) (j % 2);
}
// 初始化键的排列顺序
kwork[0] = key[56];
kwork[1] = key[48];
kwork[2] = key[40];
kwork[3] = key[32];
kwork[4] = key[24];
kwork[5] = key[16];
kwork[6] = key[8];
kwork[7] = key[0];
kwork[8] = key[57];
kwork[9] = key[49];
kwork[10] = key[41];
kwork[11] = key[33];
kwork[12] = key[25];
kwork[13] = key[17];
kwork[14] = key[9];
kwork[15] = key[1];
kwork[16] = key[58];
kwork[17] = key[50];
kwork[18] = key[42];
kwork[19] = key[34];
kwork[20] = key[26];
kwork[21] = key[18];
kwork[22] = key[10];
kwork[23] = key[2];
kwork[24] = key[59];
kwork[25] = key[51];
kwork[26] = key[43];
kwork[27] = key[35];
kwork[28] = key[62];
kwork[29] = key[54];
kwork[30] = key[46];
kwork[31] = key[38];
kwork[32] = key[30];
kwork[33] = key[22];
kwork[34] = key[14];
kwork[35] = key[6];
kwork[36] = key[61];
kwork[37] = key[53];
kwork[38] = key[45];
kwork[39] = key[37];
kwork[40] = key[29];
kwork[41] = key[21];
kwork[42] = key[13];
kwork[43] = key[5];
kwork[44] = key[60];
kwork[45] = key[52];
kwork[46] = key[44];
kwork[47] = key[36];
kwork[48] = key[28];
kwork[49] = key[20];
kwork[50] = key[12];
kwork[51] = key[4];
kwork[52] = key[27];
kwork[53] = key[19];
kwork[54] = key[11];
kwork[55] = key[3];
/* 开始计算子键 */
byte nbrofshift;
byte temp1, temp2;
for (int iter = 0; iter <16; iter++) {
nbrofshift = shift[iter];
for (int i = 0; i < (int) nbrofshift; i++) {
temp1 = kwork[0];
temp2 = kwork[28];
for (int k = 0; k <27; k++) {
kwork[k] = kwork[k + 1];
kwork[k + 28] = kwork[k + 29];
}
kwork[27] = temp1;
kwork[55] = temp2;
}
/* Permute kwork - PC2 */
kn[0] = kwork[13];
kn[1] = kwork[16];
kn[2] = kwork[10];
kn[3] = kwork[23];
kn[4] = kwork[0];
kn[5] = kwork[4];
kn[6] = kwork[2];
kn[7] = kwork[27];
kn[8] = kwork[14];
kn[9] = kwork[5];
kn[10] = kwork[20];
kn[11] = kwork[9];
kn[12] = kwork[22];
kn[13] = kwork[18];
kn[14] = kwork[11];
kn[15] = kwork[3];
kn[16] = kwork[25];
kn[17] = kwork[7];
kn[18] = kwork[15];
kn[19] = kwork[6];
kn[20] = kwork[26];
kn[21] = kwork[19];
kn[22] = kwork[12];
kn[23] = kwork[1];
kn[24] = kwork[40];
kn[25] = kwork[51];
kn[26] = kwork[30];
kn[27] = kwork[36];
kn[28] = kwork[46];
kn[29] = kwork[54];
kn[30] = kwork[29];
kn[31] = kwork[39];
kn[32] = kwork[50];
kn[33] = kwork[44];
kn[34] = kwork[32];
kn[35] = kwork[47];
kn[36] = kwork[43];
kn[37] = kwork[48];
kn[38] = kwork[38];
kn[39] = kwork[55];
kn[40] = kwork[33];
kn[41] = kwork[52];
kn[42] = kwork[45];
kn[43] = kwork[41];
kn[44] = kwork[49];
kn[45] = kwork[35];
kn[46] = kwork[28];
kn[47] = kwork[31];
switch (iter) {
case 0:
for (int k = 0; k <48; k++) {
k1[k] = kn[k];
}
break;
case 1:
for (int k = 0; k <48; k++) {
k2[k] = kn[k];
}
break;
case 2:
for (int k = 0; k <48; k++) {
k3[k] = kn[k];
}
break;
case 3:
for (int k = 0; k <48; k++) {
k4[k] = kn[k];
}
break;
case 4:
for (int k = 0; k <48; k++) {
k5[k] = kn[k];
}
break;
case 5:
for (int k = 0; k <48; k++) {
k6[k] = kn[k];
}
break;
case 6:
for (int k = 0; k <48; k++) {
k7[k] = kn[k];
}
break;
case 7:
for (int k = 0; k <48; k++) {
k8[k] = kn[k];
}
break;
case 8:
for (int k = 0; k <48; k++) {
k9[k] = kn[k];
}
break;
case 9:
for (int k = 0; k <48; k++) {
k10[k] = kn[k];
}
break;
case 10:
for (int k = 0; k <48; k++) {
k11[k] = kn[k];
}
break;
case 11:
for (int k = 0; k <48; k++) {
k12[k] = kn[k];
}
break;
case 12:
for (int k = 0; k <48; k++) {
k13[k] = kn[k];
}
break;
case 13:
for (int k = 0; k <48; k++) {
k14[k] = kn[k];
}
break;
case 14:
for (int k = 0; k <48; k++) {
k15[k] = kn[k];
}
break;
case 15:
for (int k = 0; k <48; k++) {
k16[k] = kn[k];
}
break;
}
}
}
}
❤提问/互动请留言,可直接在最底右下方"写留言即可"
❤Java初高级学习分享,共同学习才是最明智的选择,喜欢的话可以我的学习群64弍46衣3凌9,或加资料群69似64陆0吧3(进群备注平台名)
❤如果觉得写得不错请点击“订阅”,每天更新全新的内容!
❤你们的喜欢和关注就是我最大的动力支撑!!!