package com.hengyunsoft.cryptography.sign.desimpl;
import java.util.Arrays;
import java.util.Random;
import com.hengyunsoft.cryptography.sign.SignReversible;
public class SimpleDesSign implements SignReversible {
//下面这个是秘钥
private byte[] secretKeys = new byte[] {5,62,54,110,26,57,96,41,96,45,12,74,9,63,7,4,120,44}; //56 bit 秘钥
public SimpleDesSign() {
// Random r = new Random();
// r.nextBytes(secretKeys);
}
@Override
public byte[] encrypt(byte[] b) {
if(b.length !=
{
throw new IllegalArgumentException();
}
Entity[] fs = new Entity[17];
fs[0] = IP(b);
for (int i = 1; i <= 16; i++) {
fs[i] = new Entity();
fs[i].l = fs[i-1].r;
fs[i].r = xor(fs[i-1].l , f(fs[i-1].r,i));
}
return DIP(fs[16]);
}
/**
* 组合
* @param entity
* @return
*/
private byte[] DIP(Entity entity) {
byte[] result = new byte[8];
byte[] l = entity.l;
for (int i = 0; i < 4; i++) {
result[i] = l[i];
}
l = entity.r;
for (int i = 4; i < 8; i++) {
result[i] = l[i-4];
}
return result;
}
private byte[] xor(byte[] l, byte[] f) {
byte[] result = new byte[l.length];
for (int i = 0; i < l.length; i++) {
result[i] = (byte) (l[i] ^ f[i]);
}
return result;
}
private byte[] f(byte[] r, int secretKeyBitIndex) {
secretKeyBitIndex = secretKeyBitIndex*2;
int index = secretKeyBitIndex >> 3;
int startIndex = secretKeyBitIndex & 7;
byte[] secretKey = new byte[4];
if(startIndex == 0) {
for (int i = index,j=0; j < secretKey.length; i++,j++) {
secretKey[j] = secretKeys[i];
}
} else {
for (int i = index,j=0; j < secretKey.length; i++,j++) {
secretKey[j] = (byte) ((byte)(secretKeys[i] >> startIndex) | (byte)(secretKeys[i+1] << (8-startIndex))) ;
}
}
byte[] result = new byte[4];
for (int i = 0; i < result.length; i++) {
result[i] = (byte) (r[i] ^ secretKey[i]);
}
return result;
}
private byte[] Df(byte[] r, int i) {
return f(r, i);
}
/**
* 密码分组
* @param b
* @return
*/
private Entity IP(byte[] b) {
Entity result = new Entity();
result.l = Arrays.copyOfRange(b, 0, 4);
result.r = Arrays.copyOfRange(b, 4,
;
return result;
}
@Override
public byte[] decrypt(byte[] b) {
if(b.length !=
{
throw new IllegalArgumentException();
}
Entity[] fs = new Entity[17];
fs[16] = IP(b);
for (int i = 16; i >= 1; i--) {
fs[i-1] = new Entity();
fs[i-1].r = fs[i].l;
fs[i-1].l = xor(fs[i].r , Df(fs[i-1].r,i));
}
return DIP(fs[0]);
}
private static class Entity{
private byte[] l;
private byte[] r;
}
public static void main(String[] args) {
SimpleDesSign s = new SimpleDesSign();
byte[] encode = s.encrypt("12549632".getBytes());
System.out.println(new String(encode));
System.out.println(new String(s.decrypt(encode)));
}
}
分享到:
相关推荐
密码学3des+c语言代码及说明密码学3des+c语言代码及说明密码学3des+c语言代码及说明
现代密码学~DES加密算法实验报告.doc
密码学实验 实现 DES 加、解密: 构造一 DES 密钥,根据获得的密钥分别用 ECB 和 CBC 两种 模式将文本文件 图像 进行加、解密
密码学 DES加密 c++编写 注释详细
密码学 课程设计 DES 密码学 课程设计 DES 密码学 课程设计 DES 密码学 课程设计 DES
我的密码学实验 有DES算法 MD5,RSA加密 有实验报告可供参考
C语言实现的DES位图加解密,密码学第二次实验
DES代码,用于DES加密实验,其中的key部分,需要下载者自行修改,否者所编译的文档中密钥无法进行读取。
应用密码学会学到的三种加密算法,DES AES RSA,内容包含三种算法的源代码和.exe文件和具体实验报告。对密码理论与应用课程有参考作用。
密码学试验DES的源程序代码,是一个.cpp文件
河北工业大学密码学实验(des和rsa实验),只找到这两个实验的代码了,如果需要的话就下载吧
密码学加密算法,DES的实验,是一种传统的加密算法。
des 密码实验,用c++实现 des 密码实验,用c++实现
应用密码学的源代码,包括des加解密的实现,还有ides等等。
信安的密码学专用课设,里面对des的实现与短块进行了处理,代码里面有详细的注释,其它里面也都加了注释,生成的程序一并发给大家,方便大家调试。用C语言写的,在vc6.0编译通过。
DES的简化版本,S-DES,密码学资料
用 DES 实现口令的安全: 登陆时,用户的口令作为 DES 密钥,加密某个固定的明文,生成的密文存储在 计算机中。下次登陆时,把生成的密文和已存储的密文进行比较,若一致则登陆 成功。实验结果截图 在压缩包里
华科密码学课设之DES的c语言算法实现,包括简单数字的加解密还有文件的加解密,c语言实现
初学者编写的,不过是一个可以直接使用、编译通过的DES程序,附加详细注释
应用密码学 | DES代码