社区
Java SE
帖子详情
DES加密算法
uglyFairy
2003-10-29 09:38:35
用Java怎么实现DES加密算法.
(
有价值的相关资料,给50分
有可以运行的例子,给100分
有可以运行的例子和详细说明,给300分
)
我的MSN:sjm428124@msn.com
大家可以在MSN上交流一下,我很欢迎.
...全文
102
6
打赏
收藏
DES加密算法
用Java怎么实现DES加密算法. ( 有价值的相关资料,给50分 有可以运行的例子,给100分 有可以运行的例子和详细说明,给300分 ) 我的MSN:sjm428124@msn.com 大家可以在MSN上交流一下,我很欢迎.
复制链接
扫一扫
分享
转发到动态
举报
AI
作业
写回复
配置赞助广告
用AI写文章
6 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
207
2003-10-29
打赏
举报
回复
关注!
cbhyk
2003-10-29
打赏
举报
回复
private void scrunch(byte outof[], int into[])
{
int i = 0;
into[0] = (outof[i] & 0xff) << 24;
i++;
into[0] |= (outof[i] & 0xff) << 16;
i++;
into[0] |= (outof[i] & 0xff) << 8;
i++;
into[0] |= outof[i] & 0xff;
i++;
into[1] = (outof[i] & 0xff) << 24;
i++;
into[1] |= (outof[i] & 0xff) << 16;
i++;
into[1] |= (outof[i] & 0xff) << 8;
i++;
into[1] |= outof[i] & 0xff;
}
private void unscrunch(int outof[], byte into[])
{
int i = 0;
into[i] = (byte)(outof[0] >> 24 & 0xff);
i++;
into[i] = (byte)(outof[0] >> 16 & 0xff);
i++;
into[i] = (byte)(outof[0] >> 8 & 0xff);
i++;
into[i] = (byte)(outof[0] & 0xff);
i++;
into[i] = (byte)(outof[1] >> 24 & 0xff);
i++;
into[i] = (byte)(outof[1] >> 16 & 0xff);
i++;
into[i] = (byte)(outof[1] >> 8 & 0xff);
i++;
into[i] = (byte)(outof[1] & 0xff);
}
private void desfunc(int block[], int keys[])
{
int i = 0;
int k = 0;
int leftt = block[0];
int right = block[1];
int work = (leftt >>> 4 ^ right) & 0xf0f0f0f;
right ^= work;
leftt ^= work << 4;
work = (leftt >>> 16 ^ right) & 0xffff;
right ^= work;
leftt ^= work << 16;
work = (right >>> 2 ^ leftt) & 0x33333333;
leftt ^= work;
right ^= work << 2;
work = (right >>> 8 ^ leftt) & 0xff00ff;
leftt ^= work;
right ^= work << 8;
right = (right << 1 | right >>> 31 & 0x1) & 0xffffffff;
work = (leftt ^ right) & 0xaaaaaaaa;
leftt ^= work;
right ^= work;
leftt = (leftt << 1 | leftt >>> 31 & 0x1) & 0xffffffff;
for(int round = 0; round < 8; round++)
{
work = right << 28 | right >>> 4;
long tempkeys = 0L;
tempkeys = keys[k];
work ^= keys[k];
k++;
int fval = sp7[work & 0x3f];
fval |= sp5[work >>> 8 & 0x3f];
fval |= sp3[work >>> 16 & 0x3f];
fval |= sp1[work >>> 24 & 0x3f];
work = right ^ keys[k];
k++;
fval |= sp8[work & 0x3f];
fval |= sp6[work >>> 8 & 0x3f];
fval |= sp4[work >>> 16 & 0x3f];
fval |= sp2[work >>> 24 & 0x3f];
leftt ^= fval;
work = leftt << 28 | leftt >>> 4;
work ^= keys[k];
k++;
fval = sp7[work & 0x3f];
fval |= sp5[work >>> 8 & 0x3f];
fval |= sp3[work >>> 16 & 0x3f];
fval |= sp1[work >>> 24 & 0x3f];
work = leftt ^ keys[k];
k++;
fval |= sp8[work & 0x3f];
fval |= sp6[work >>> 8 & 0x3f];
fval |= sp4[work >>> 16 & 0x3f];
fval |= sp2[work >>> 24 & 0x3f];
right ^= fval;
}
right = right << 31 | right >>> 1;
work = (leftt ^ right) & 0xaaaaaaaa;
leftt ^= work;
right ^= work;
leftt = leftt << 31 | leftt >>> 1;
work = (leftt >>> 8 ^ right) & 0xff00ff;
right ^= work;
leftt ^= work << 8;
work = (leftt >>> 2 ^ right) & 0x33333333;
right ^= work;
leftt ^= work << 2;
work = (right >>> 16 ^ leftt) & 0xffff;
leftt ^= work;
right ^= work << 16;
work = (right >>> 4 ^ leftt) & 0xf0f0f0f;
leftt ^= work;
right ^= work << 4;
block[0] = right;
block[1] = leftt;
}
//example
public static void main(String[] args)
{
byte[] key = new byte[]{0x34, 0x23, 0x57, 0xf3, 0xb2, 0xc7, 0xd1, 0xa9};
byte[] data = "test data".getBytes();
DESCodec codec = new DESCodec();
byte[] encodedData = codec.encrypt(key, data);
byte[] decodedData = codec.decrypt(key, encodedData);
String decodedStr = new String(decodedData);
System.out.println(decodedStr);
}
}
cbhyk
2003-10-29
打赏
举报
回复
public byte[] encrypt(byte key[], byte data[])
{
setKeys(key);
byte val[] = new byte[8];
int padding = 8 - data.length % 8;
byte ebuf[] = new byte[data.length + padding];
java.lang.System.arraycopy(data, 0, ebuf, 0, data.length);
for(int i = data.length; i < ebuf.length; i++)
ebuf[i] = (byte)padding;
for(int i = 0; i < ebuf.length / 8; i++)
{
for(int j = 0; j < 8; j++)
val[j] = ebuf[i * 8 + j];
des_enc(val, 1);
java.lang.System.arraycopy(val, 0, ebuf, i * 8, 8);
}
return ebuf;
}
public byte[] decrypt(byte key[], byte data[])
{
setKeys(key);
byte val[] = new byte[8];
byte buffer[] = new byte[data.length];
for(int i = 0; i < data.length / 8; i++)
{
for(int j = 0; j < 8; j++)
val[j] = data[i * 8 + j];
des_dec(val, 1);
java.lang.System.arraycopy(val, 0, buffer, i * 8, 8);
}
byte result[] = new byte[buffer.length - buffer[buffer.length - 1]];
java.lang.System.arraycopy(buffer, 0, result, 0, result.length);
return result;
}
public byte[] encryptAll8BytesBlocks(byte value[], boolean only8bytes)
{
byte ebuf[] = new byte[8];
byte evalue[] = new byte[value.length];
for(int i = 0; i < value.length / 8; i++)
{
for(int j = 0; j < 8; j++)
ebuf[j] ^= value[i * 8 + j];
des_enc(ebuf, 1);
if(!only8bytes)
java.lang.System.arraycopy(ebuf, 0, evalue, i * 8, 8);
}
if(only8bytes)
return ebuf;
else
return evalue;
}
public void des_enc(byte data[], int blocks)
{
int work[] = new int[2];
byte cp[] = data;
for(int i = 0; i < blocks; i++)
{
scrunch(cp, work);
desfunc(work, ek);
unscrunch(work, cp);
}
}
public void des_dec(byte data[], int blocks)
{
int work[] = new int[2];
byte cp[] = data;
for(int i = 0; i < blocks; i++)
{
scrunch(cp, work);
desfunc(work, dk);
unscrunch(work, cp);
}
}
public void des_enc(byte key[], byte data[], int blocks)
{
int work[] = new int[2];
byte cp[] = data;
for(int i = 0; i < blocks; i++)
{
scrunch(cp, work);
desfunc(work, deskey(key, (short)0));
unscrunch(work, cp);
}
}
public void des_dec(byte key[], byte data[], int blocks)
{
int work[] = new int[2];
byte cp[] = data;
for(int i = 0; i < blocks; i++)
{
scrunch(cp, work);
desfunc(work, deskey(key, (short)1));
unscrunch(work, cp);
}
}
public void setKeys(byte key[])
{
ek = deskey(key, (short)0);
dk = deskey(key, (short)1);
}
private void setEncryptionKey(byte key[])
{
ek = deskey(key, (short)0);
}
private void setDecryptionKey(byte key[])
{
dk = deskey(key, (short)1);
}
private void nullKeys()
{
ek = null;
dk = null;
}
private void nullEncryptionKey()
{
ek = null;
}
private void nullDecryptionKey()
{
dk = null;
}
private int[] deskey(byte key[], short edf)
{
byte pc1m[] = new byte[56];
byte pcr[] = new byte[56];
int kn[] = new int[32];
for(int j = 0; j < 56; j++)
{
int l = pc1[j];
int m = l & 0x7;
pc1m[j] = (byte)((key[l >> 3] & bytebit[m]) == 0 ? 0 : 1);
}
for(int i = 0; i < 16; i++)
{
int m;
if(edf == 1)
m = 15 - i << 1;
else
m = i << 1;
int n = m + 1;
kn[m] = kn[n] = 0;
for(int j = 0; j < 28; j++)
{
int l = j + totrot[i];
if(l < 28)
pcr[j] = pc1m[l];
else
pcr[j] = pc1m[l - 28];
}
for(int j = 28; j < 56; j++)
{
int l = j + totrot[i];
if(l < 56)
pcr[j] = pc1m[l];
else
pcr[j] = pc1m[l - 28];
}
for(int j = 0; j < 24; j++)
{
if(pcr[pc2[j]] != 0)
kn[m] |= bigbyte[j];
if(pcr[pc2[j + 24]] != 0)
kn[n] |= bigbyte[j];
}
}
return cookey(kn);
}
private int[] cookey(int raw1[])
{
int dough[] = new int[32];
int cook[] = dough;
int raw0[] = raw1;
int i = 0;
int r0 = 0;
int r1 = 0;
int c = 0;
while(i < 16)
{
r0 = r1++;
cook[c] = (raw0[r0] & 0xfc0000) << 6;
cook[c] |= (raw0[r0] & 0xfc0) << 10;
cook[c] |= (raw1[r1] & 0xfc0000) >> 10;
cook[c] |= (raw1[r1] & 0xfc0) >> 6;
c++;
cook[c] = (raw0[r0] & 0x3f000) << 12;
cook[c] |= (raw0[r0] & 0x3f) << 16;
cook[c] |= (raw1[r1] & 0x3f000) >> 4;
cook[c] |= raw1[r1] & 0x3f;
c++;
i++;
r1++;
}
return cook;
}
cbhyk
2003-10-29
打赏
举报
回复
public class DESCodec
{
private final boolean DEBUG = false;
private final boolean DEBUG2 = false;
static final byte ENCRYPT = 0;
static final byte DECRYPT = 1;
private int ek[];
private int dk[];
private static final short bytebit[] = {
128, 64, 32, 16, 8, 4, 2, 1
};
private static final int bigbyte[] = {
0x800000, 0x400000, 0x200000, 0x100000, 0x80000, 0x40000, 0x20000, 0x10000, 32768, 16384,
8192, 4096, 2048, 1024, 512, 256, 128, 64, 32, 16,
8, 4, 2, 1
};
private static final byte pc1[] = {
56, 48, 40, 32, 24, 16, 8, 0, 57, 49,
41, 33, 25, 17, 9, 1, 58, 50, 42, 34,
26, 18, 10, 2, 59, 51, 43, 35, 62, 54,
46, 38, 30, 22, 14, 6, 61, 53, 45, 37,
29, 21, 13, 5, 60, 52, 44, 36, 28, 20,
12, 4, 27, 19, 11, 3
};
private static final byte totrot[] = {
1, 2, 4, 6, 8, 10, 12, 14, 15, 17,
19, 21, 23, 25, 27, 28
};
private static final byte pc2[] = {
13, 16, 10, 23, 0, 4, 2, 27, 14, 5,
20, 9, 22, 18, 11, 3, 25, 7, 15, 6,
26, 19, 12, 1, 40, 51, 30, 36, 46, 54,
29, 39, 50, 44, 32, 47, 43, 48, 38, 55,
33, 52, 45, 41, 49, 35, 28, 31
};
private static final int sp1[] = {
0x1010400, 0, 0x10000, 0x1010404, 0x1010004, 0x10404, 4, 0x10000, 1024, 0x1010400,
0x1010404, 1024, 0x1000404, 0x1010004, 0x1000000, 4, 1028, 0x1000400, 0x1000400, 0x10400,
0x10400, 0x1010000, 0x1010000, 0x1000404, 0x10004, 0x1000004, 0x1000004, 0x10004, 0, 1028,
0x10404, 0x1000000, 0x10000, 0x1010404, 4, 0x1010000, 0x1010400, 0x1000000, 0x1000000, 1024,
0x1010004, 0x10000, 0x10400, 0x1000004, 1024, 4, 0x1000404, 0x10404, 0x1010404, 0x10004,
0x1010000, 0x1000404, 0x1000004, 1028, 0x10404, 0x1010400, 1028, 0x1000400, 0x1000400, 0,
0x10004, 0x10400, 0, 0x1010004
};
private static final int sp2[] = {
0x80108020, 0x80008000, 32768, 0x108020, 0x100000, 32, 0x80100020, 0x80008020, 0x80000020, 0x80108020,
0x80108000, 0x80000000, 0x80008000, 0x100000, 32, 0x80100020, 0x108000, 0x100020, 0x80008020, 0,
0x80000000, 32768, 0x108020, 0x80100000, 0x100020, 0x80000020, 0, 0x108000, 32800, 0x80108000,
0x80100000, 32800, 0, 0x108020, 0x80100020, 0x100000, 0x80008020, 0x80100000, 0x80108000, 32768,
0x80100000, 0x80008000, 32, 0x80108020, 0x108020, 32, 32768, 0x80000000, 32800, 0x80108000,
0x100000, 0x80000020, 0x100020, 0x80008020, 0x80000020, 0x100020, 0x108000, 0, 0x80008000, 32800,
0x80000000, 0x80100020, 0x80108020, 0x108000
};
private static final int sp3[] = {
520, 0x8020200, 0, 0x8020008, 0x8000200, 0, 0x20208, 0x8000200, 0x20008, 0x8000008,
0x8000008, 0x20000, 0x8020208, 0x20008, 0x8020000, 520, 0x8000000, 8, 0x8020200, 512,
0x20200, 0x8020000, 0x8020008, 0x20208, 0x8000208, 0x20200, 0x20000, 0x8000208, 8, 0x8020208,
512, 0x8000000, 0x8020200, 0x8000000, 0x20008, 520, 0x20000, 0x8020200, 0x8000200, 0,
512, 0x20008, 0x8020208, 0x8000200, 0x8000008, 512, 0, 0x8020008, 0x8000208, 0x20000,
0x8000000, 0x8020208, 8, 0x20208, 0x20200, 0x8000008, 0x8020000, 0x8000208, 520, 0x8020000,
0x20208, 8, 0x8020008, 0x20200
};
private static final int sp4[] = {
0x802001, 8321, 8321, 128, 0x802080, 0x800081, 0x800001, 8193, 0, 0x802000,
0x802000, 0x802081, 129, 0, 0x800080, 0x800001, 1, 8192, 0x800000, 0x802001,
128, 0x800000, 8193, 8320, 0x800081, 1, 8320, 0x800080, 8192, 0x802080,
0x802081, 129, 0x800080, 0x800001, 0x802000, 0x802081, 129, 0, 0, 0x802000,
8320, 0x800080, 0x800081, 1, 0x802001, 8321, 8321, 128, 0x802081, 129,
1, 8192, 0x800001, 8193, 0x802080, 0x800081, 8193, 8320, 0x800000, 0x802001,
128, 0x800000, 8192, 0x802080
};
private static final int sp5[] = {
256, 0x2080100, 0x2080000, 0x42000100, 0x80000, 256, 0x40000000, 0x2080000, 0x40080100, 0x80000,
0x2000100, 0x40080100, 0x42000100, 0x42080000, 0x80100, 0x40000000, 0x2000000, 0x40080000, 0x40080000, 0,
0x40000100, 0x42080100, 0x42080100, 0x2000100, 0x42080000, 0x40000100, 0, 0x42000000, 0x2080100, 0x2000000,
0x42000000, 0x80100, 0x80000, 0x42000100, 256, 0x2000000, 0x40000000, 0x2080000, 0x42000100, 0x40080100,
0x2000100, 0x40000000, 0x42080000, 0x2080100, 0x40080100, 256, 0x2000000, 0x42080000, 0x42080100, 0x80100,
0x42000000, 0x42080100, 0x2080000, 0, 0x40080000, 0x42000000, 0x80100, 0x2000100, 0x40000100, 0x80000,
0, 0x40080000, 0x2080100, 0x40000100
};
private static final int sp6[] = {
0x20000010, 0x20400000, 16384, 0x20404010, 0x20400000, 16, 0x20404010, 0x400000, 0x20004000, 0x404010,
0x400000, 0x20000010, 0x400010, 0x20004000, 0x20000000, 16400, 0, 0x400010, 0x20004010, 16384,
0x404000, 0x20004010, 16, 0x20400010, 0x20400010, 0, 0x404010, 0x20404000, 16400, 0x404000,
0x20404000, 0x20000000, 0x20004000, 16, 0x20400010, 0x404000, 0x20404010, 0x400000, 16400, 0x20000010,
0x400000, 0x20004000, 0x20000000, 16400, 0x20000010, 0x20404010, 0x404000, 0x20400000, 0x404010, 0x20404000,
0, 0x20400010, 16, 16384, 0x20400000, 0x404010, 16384, 0x400010, 0x20004010, 0,
0x20404000, 0x20000000, 0x400010, 0x20004010
};
private static final int sp7[] = {
0x200000, 0x4200002, 0x4000802, 0, 2048, 0x4000802, 0x200802, 0x4200800, 0x4200802, 0x200000,
0, 0x4000002, 2, 0x4000000, 0x4200002, 2050, 0x4000800, 0x200802, 0x200002, 0x4000800,
0x4000002, 0x4200000, 0x4200800, 0x200002, 0x4200000, 2048, 2050, 0x4200802, 0x200800, 2,
0x4000000, 0x200800, 0x4000000, 0x200800, 0x200000, 0x4000802, 0x4000802, 0x4200002, 0x4200002, 2,
0x200002, 0x4000000, 0x4000800, 0x200000, 0x4200800, 2050, 0x200802, 0x4200800, 2050, 0x4000002,
0x4200802, 0x4200000, 0x200800, 0, 2, 0x4200802, 0, 0x200802, 0x4200000, 2048,
0x4000002, 0x4000800, 2048, 0x200002
};
private static final int sp8[] = {
0x10001040, 4096, 0x40000, 0x10041040, 0x10000000, 0x10001040, 64, 0x10000000, 0x40040, 0x10040000,
0x10041040, 0x41000, 0x10041000, 0x41040, 4096, 64, 0x10040000, 0x10000040, 0x10001000, 4160,
0x41000, 0x40040, 0x10040040, 0x10041000, 4160, 0, 0, 0x10040040, 0x10000040, 0x10001000,
0x41040, 0x40000, 0x41040, 0x40000, 0x10041000, 4096, 64, 0x10040040, 4096, 0x41040,
0x10001000, 64, 0x10000040, 0x10040000, 0x10040040, 0x10000000, 0x40000, 0x10001040, 0, 0x10041040,
0x40040, 0x10000040, 0x10040000, 0x10001000, 0x10001040, 0, 0x10041040, 0x41000, 0x41000, 4160,
4160, 0x40040, 0x10000000, 0x10041000
};
yangFrame
2003-10-29
打赏
举报
回复
我用c做过一个
#include<fstream.h>
static char IP_Table[64] = {58,50,42,34,26,18,10,2,
60,52,44,36,28,20,12,4,
62,54,46,38,30,22,14,6,
64,56,48,40,32,24,16,8,
57,49,41,33,25,17,9,1,
59,51,43,35,27,19,11,3,
61,53,45,37,29,21,13,5,
63,55,47,39,31,23,15,7};
static char IPR_Table[64] = {40,8,48,16,56,24,64,32,
39,7,47,15,55,23,63,31,
38,6,46,14,54,22,62,30,
37,5,45,13,53,21,61,29,
36,4,44,12,52,20,60,28,
35,3,43,11,51,19,59,27,
34,2,42,10,50,18,58,26,
33,1,41,9,49,17,57,25};
static char E_Table[48] = {32,1,2,3,4,5,
4,5,6,7,8,9,
8,9,10,11,12,13,
12,13,14,15,16,17,
16,17,18,19,20,21,
20,21,22,23,24,25,
24,25,26,27,28,29,
28,29,30,31,32,31};
static char P_Table[32] ={16,7,20,21,
29,12,28,17,
1,15,23,26,
5,18,31,10,
2,8,24,14,
32,27,3,9,
19,13,30,6,
22,11,4,25};
static char PC1_Table[56] = {
57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18,
10, 2, 59, 51, 43, 35, 27, 19, 11, 3, 60, 52, 44, 36,
63, 55, 47, 39, 31, 23, 15, 7, 62, 54, 46, 38, 30, 22,
14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 28, 20, 12, 4
};
static char PC2_Table[48] = {
14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21, 10,
23, 19, 12, 4, 26, 8, 16, 7, 27, 20, 13, 2,
41, 52, 31, 37, 47, 55, 30, 40, 51, 45, 33, 48,
44, 49, 39, 56, 34, 53, 46, 42, 50, 36, 29, 32
};
static char LOOP_Table[16] = {
1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1
};
static char S_box[8][4][16] = {
// S1
14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7,
0,15,7,4,14,2,13,1,10,6,12,11,9,5,3,8,
4,1,14,8,13,6,2,11,15,12,9,7,3,10,5,0,
15,12,8,2,4,9,1,7,5,11,3,14,10,0,6,13,
// S2
15,1,8,14,6,11,3,4,9,7,2,13,12,0,5,10,
3,13,4,7,15,2,8,14,12,0,1,10,6,9,11,5,
0,14,7,11,10,4,13,1,5,8,12,6,9,3,2,15,
13,8,10,1,3,15,4,2,11,6,7,12,0,5,14,9,
// S3
10,0,9,14,6,3,15,5,1,13,12,7,11,4,2,8,
13,7,0,9,3,4,6,10,2,8,5,14,12,11,15,1,
13,6,4,9,8,15,3,0,11,1,2,12,5,10,14,7,
1,10,13,0,6,9,8,7,4,15,14,3,11,5,2,12,
// S4
7,13,14,3,0,6,9,10,1,2,8,5,11,12,4,15,
13,8,11,5,6,15,0,3,4,7,2,12,1,10,14,9,
10,6,9,0,12,11,7,13,15,1,3,14,5,2,8,4,
3,15,0,6,10,1,13,8,9,4,5,11,12,7,2,14,
// S5
2,12,4,1,7,10,11,6,8,5,3,15,13,0,14,9,
14,11,2,12,4,7,13,1,5,0,15,10,3,9,8,6,
4,2,1,11,10,13,7,8,15,9,12,5,6,3,0,14,
11,8,12,7,1,14,2,13,6,15,0,9,10,4,5,3,
// S6
12,1,10,15,9,2,6,8,0,13,3,4,14,7,5,11,
10,15,4,2,7,12,9,5,6,1,13,14,0,11,3,8,
9,14,15,5,2,8,12,3,7,0,4,10,1,13,11,6,
4,3,2,12,9,5,15,10,11,14,1,7,6,0,8,13,
// S7
4,11,2,14,15,0,8,13,3,12,9,7,5,10,6,1,
13,0,11,7,4,9,1,10,14,3,5,12,2,15,8,6,
1,4,11,13,12,3,7,14,10,15,6,8,0,5,9,2,
6,11,13,8,1,4,10,7,9,5,0,15,14,2,3,12,
// S8
13,2,8,4,6,15,11,1,10,9,3,14,5,0,12,7,
1,15,13,8,10,3,7,4,12,5,6,11,0,14,9,2,
7,11,4,1,9,12,14,2,0,6,10,13,15,3,5,8,
2,1,14,7,4,10,8,13,15,12,9,0,3,5,6,11
};
//order key
static char orderkey[2][16]={
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
};
//key
static char subKey[16][48];
//const static char
//*********************************************************************
//代码入口
void init(char *ycode,char *mcode){
for(char i=7,b=63;i>=0;i--){
unsigned char a=ycode[i];
for(char j=0;j<8;j++){
mcode[b--]=a%2;
a=a/2;
}
}
}
void uninit(char *mcode,char *ycode){
for(char i=0,b=0;i<8;i++){
ycode[i]=0;
for(char j=0;j<8;j++){
ycode[i]=ycode[i]*2;
ycode[i]=ycode[i]+mcode[b++];
}
}
}
void cp(char *ycode,char *mcode,int n){
for(int i=0;i<n;i++)
mcode[i]=ycode[i];
}
void exchange(char *r,char *t,int n,const char *order){
char *s=new char[n];
for(int i=0;i<n;i++)
s[i]=r[order[i]-1];
cp(s,t,n);
}
void schange(int i,char *in,char *out){
char c=S_box[i][in[0]*2+in[5]][in[1]*8+in[2]*4+in[3]*2+in[4]];
for(int j=3;j>=0;j--){
out[j]=c%2;
c=c/2;}
}
void sbox(char *ycode){
for(int i=0;i<8;i++){
schange(i,&ycode[6*i],&ycode[4*i]);
}
}
void rol(char *mcode,int n,int k){
char *c=new char[k];
for(int i=0;i<k;i++)
c[i]=mcode[i];
for(i=k;i<n;i++)
mcode[i-k]=mcode[i];
for(i=0;i<k;i++)
mcode[i+n-k]=c[i];
}
void xor(char *s,char *r,char *t,int n){
for(int i=0;i<n;i++)
t[i]=(s[i]+r[i])%2;
}
void f(char *r,char i){
char t[48];
exchange(r,t,48,E_Table); //32~48放大换位
xor(t,subKey[i],t,48); //48+48与密钥
sbox(t); //S盒变换
exchange(t,r,32,P_Table); //32~32单纯换位
}
void makekey(char *ykey){
char mkey[64];
init(ykey,mkey); //位至字节转换
exchange(mkey,mkey,56,PC1_Table); //PC1换位
for(int i=0;i<16;i++){ //16次密钥计算循环
rol(mkey,28,LOOP_Table[i]);
rol(mkey+28,28,LOOP_Table[i]); //循环移位
exchange(mkey,subKey[i],48,PC2_Table); //PC2换位输出至密钥I
}
}
void run(char *ycode,char *scode,int order){
char mcode[64],tmp[32];
init(ycode,mcode); //位至字节转换
exchange(mcode,mcode,64,IP_Table); //初始置换IP
for(int i=0;i<16;i++){ //16次密文计算循环
cp(mcode+32,tmp,32); //tmp=R(i)
f(mcode+32,orderkey[order][i]); //R(i)=f(Ri,Ki)
xor(mcode,mcode+32,mcode+32,32); //R(i+1)=L(i)xorR(i)
cp(tmp,mcode,32); //L(i+1)=tmp
}
cp(mcode,tmp,32);
cp(mcode+32,mcode,32);
cp(tmp,mcode+32,32); //左右交换
exchange(mcode,mcode,64,IPR_Table); //逆置换IP
uninit(mcode,scode); //字节至位转换
}
//代码结尾
//调试主程序
void main(){
char key[]="computer",code[]="security";
// cin>>key;
// cin>>code;
cout<<"key:\n"<<key<<endl;
makekey(key);
cout<<"Before encrypting:\n"<<code<<endl;
run(code,code,0);
cout<<"after encrypting:\n"<<code<<endl;
run(code,code,1);
cout<<"after decrypting:\n"<<code<<endl;
}
fast_time
2003-10-29
打赏
举报
回复
JDK中有实现的接口,不用单独实现
des
加密算法
(js+java)加密与解密结果相同
des
加密算法
(js+java)加密与解密结果相同 包含三个文件 :
des
.js
des
.html ,用于实现前端脚本的加密与解密
des
.java 用于后台的加密解密操作; 项目中正好用到,已经过验证,两个加密解密结果相同,分享给大家!
3
DES
加密算法
源代码
DES
加密源代码,用3
DES
加密算法
。加密强度高,到目前为止,还无人能够破解!
DES
加密算法
源代码
DES
(Data Encrypt Standard)
加密算法
.
Rijndael算法源代码
Rijndael算法,已通过AES的认证。
加密算法
------
DES
加密算法
详解
一、
加密算法
的分类1.对称加解密算法a.通信双方同时掌握一个密钥,加密解密都是由一个密钥完成的(即加密密钥等于解密密钥,加解密密钥可以相互推倒出来)。b.双方通信前共同拟定一个密钥,不对第三方公开。c.不具有个体原子性,一个密钥被共享,泄漏几率增大2.公私钥加解密算法a.通信双方掌握不同的密钥,不同方向的加解密由不同的密钥完成。二、对称
加密算法
的代表----
DES
加密算法
原理:该算法是一个利用56...
Java SE
62,634
社区成员
307,265
社区内容
发帖
与我相关
我的任务
Java SE
Java 2 Standard Edition
复制链接
扫一扫
分享
社区描述
Java 2 Standard Edition
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章