📄 desalgorithm.java
字号:
package Security_DES;
import java.math.*;
import java.io.*;
public class DESAlgorithm { //implements Runnable
private char IP[][];
private char[] Li = new char[32];
private char[] Ri = new char[32];
private char[] R_E = new char[48];
private byte[] buf = new byte[9];
private String type;
private int loop;
secretKey_DES secretKey;
indexVector iVector = new indexVector();
DESAlgorithm (String type,byte[]out,byte[] src,secretKey_DES secretKey){
if(src.length%8==0){
loop=src.length/8;
System.arraycopy(src,0,out,0,src.length);
}else{
loop=(int)(src.length/8)+1;
System.arraycopy(src,0,out,0,src.length);
for(int c=src.length;c<loop*8;c++){
out[c]=0;
}
}
System.out.println(loop);
this.type = type;
this.secretKey = secretKey;
if(secretKey.keyNum == 1){
if(type.equals("ENCRYPT")){
buf[0]=0;
for(int i=0;i<loop;i++){
System.arraycopy(out,i*8,buf,1,8);
toBinaryType(buf);
handleCourse(1,type);
System.arraycopy(getByte(),0,out,i*8,getByte().length);
}
}
else if(type.equals("DECRYPT")){
for(int j=0;j<loop;j++){
System.arraycopy(out,j*8,buf,1,8);
toBinaryType(buf);
handleCourse(1,type);
System.arraycopy(getByte(),0,out,j*8,getByte().length);
}
}
}
else if(secretKey.keyNum == 2){
toBinaryType(src);
handleCourse(1,type);
toBinaryType(getByte());
handleCourse(2,"DECRYPT");
toBinaryType(getByte());
handleCourse(1,type);
System.out.println("加密后的字符串:"+new String(getByte()));
toBinaryType(getByte());
handleCourse(1,"DECRYPT");
toBinaryType(getByte());
handleCourse(2,type);
toBinaryType(getByte());
handleCourse(1,"DECRYPT");
System.out.println("解密后的字符串:"+new String(getByte()));
getByte();
}
}
DESAlgorithm (String type,String src,secretKey_DES secretKey){
this.type = type;
this.secretKey = secretKey;
byte b[] = src.getBytes();
if(type.equals("DES")){
toBinaryType(b);
handleCourse(1,type);
toBinaryType(getByte());
handleCourse(1,"DECRYPT");
getByte();
}
else if(type.equals("3DES")){
}
}
//转换成二进制的表示形式
private void toBinaryType(byte[] src){
char[] out;
BigInteger bi = new BigInteger(src);
String str = bi.toString(2);
int n = str.length();
if(n<64){
for(int ll=0;ll<64-n;ll++){
str = "0"+str;
}
}
out = str.toCharArray();
for(int i=0;i<64;i++){
if(i<32){
Li[i] = out[iVector.replaceId[i]-1];
}else{
Ri[i-32] = out[iVector.replaceId[i]-1];
}
}
}
//16轮处理过程
private void handleCourse(int k,String mode){
if(mode.equals("ENCRYPT")){
for(int count = 0;count<16;count++){
Algorithm(Li,Ri,secretKey.subKey[(k-1)*16+count],count);
}
}
else if(mode.equals("DECRYPT")){
for(int count = 16;count>0;count--){
Algorithm(Li,Ri,secretKey.subKey[(k-1)*16+count-1],count-1);
}
}
}
//E盒算法
private void Ebox(char[] R){
for(int i = 0;i<48;i++){
R_E[i] = R[iVector.extendId[i]-1];
}
}
//单轮处理算法
private void Algorithm(char[] L,char[] R,char[] key,int count){
BigInteger B1,B2;
char[] src;
src = R;
Ebox(R);
B1 = new BigInteger(new String(R_E),2);
B2 = new BigInteger(new String(key),2);
B1 = B1.xor(B2);
B2 = new BigInteger(new String(L),2);
//问题
String str = B1.toString(2);
int n = str.length();
if(n<48){
for(int ll=0;ll<48-n;ll++){
str = "0"+str;
}
}
R = SPbox(str.toCharArray());
B1 = new BigInteger(new String(R),2);
B1 = B1.xor(B2);
Li = src;
//问题
String str1 = B1.toString(2);
int n1 = (32-str1.length());
for(int k=0;k<n1;k++){
str1 = "0"+str1;
}
Ri = str1.toCharArray();
}
//SP盒算法
private char[] SPbox(char[] src){
String s;
String sbox;
char[] chr;
char[] out = new char[32];
for(int i = 0;i<8;i++){
s = "";
BigInteger bi;
int n;
for(int j = 0;j<6;j++){
s = s+src[i*6+j];
s.trim();
}
bi = new BigInteger(s,2);
n = bi.intValue();
sbox = iVector.S[i][n];
chr = sbox.toCharArray();
for(int k = 0;k<4;k++){
out[iVector.PBoxId[i*4+k]-1] = chr[k];
}
}
return out;
}
protected byte[] getByte(){
char[] src = new char[64];
for(int i=0;i<64;i++){
if(iVector.IP_1[i]<33){
src[i] = Ri[iVector.IP_1[i]-1];
}else{
src[i] = Li[iVector.IP_1[i]-33];
}
}
String sl = new String(src);
BigInteger bi = new BigInteger(sl,2);
byte[] by = bi.toByteArray();
//System.out.print(by.length+" "+sl+" ");
if(by.length>8){
byte []byt=new byte[8];
for(int k=0;k<8;k++){
byt[k]=by[k+1];
}
return byt;
}else if(by.length<8){
byte []bb=new byte[8];
for(int l=8-by.length;l<8;l++){
bb[l]=by[l-(8-by.length)];
}
for(int d=0;d<8-by.length;d++){
bb[d]=0;
}
return bb;
}else{
return by;
}
}
/*public static void main(String args[]){
String s;
if(args.length>0){
s=args[0];
byte[] b=s.getBytes();
byte []d=new byte[((int)b.length/8+1)*8];
byte[] ke = new byte[]{0x03,0x0f,0x0e,0x01,0x01,0x0c,0x0d,0x01};
secretKey_DES key = new secretKey_DES(ke,"DES");
new DESAlgorithm("ENCRYPT",d,b,key);
System.out.println(new String(d));
new DESAlgorithm("DECRYPT",d,d,key);
System.out.println(new String(d));
}
else{
try{
DataInputStream d = new DataInputStream(new BufferedInputStream(new FileInputStream("F:/bysj/Security_DES/debug.doc")));
// fif.write(encoded);
;//= s.getBytes();
byte[] b=new byte[d.available()];
d.read(b,0,d.available());
byte[] out = new byte[((int)b.length/8+1)*8];
//System.out.println("加密前的字符串:"+s);
byte[] ke = new byte[]{0x03,0x0f,0x0e,0x01,0x01,0x0c,0x0d,0x01};//,0x0a,0x0b,0x01,0x01,0x0d,0x0f,0x01,0x01};
secretKey_DES key = new secretKey_DES(ke,"DES");
new DESAlgorithm("ENCRYPT",out,b,key);
new DESAlgorithm("DECRYPT",out,out,key);
DataOutputStream fif= new DataOutputStream(new BufferedOutputStream(new FileOutputStream("F:/bysj/Security_DES/debdddddug.doc")));
fif.write(out,0,b.length);
fif.flush();
}catch(Exception e){}finally{
//fif.close();
//d.close();
}
}*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -