simple-des算法的java实现

来源:互联网 发布:图片大小编辑软件 编辑:程序博客网 时间:2024/05/29 13:01

下面的代码是simple-des算法的java完整实现,可以直接运行。

[java] view plaincopy
  1. import java.io.IOException;  
  2. public class SimpleDES {  
  3.     /** 
  4.      * @param args 
  5.      */  
  6.     // two keys K1 and K2  
  7.     static int K1=0;  
  8.     static int K2=0;  
  9.     /*some parameters*/  
  10.     static int P10[]=new int[]{3,5,2,7,4,10,1,9,8,6};  
  11.     static int P8[]=new int[]{6,3,7,4,8,5,10,9};  
  12.     static int P4[]=new int[]{2,4,3,1};  
  13.     static int IP[]=new int[]{2,6,3,1,4,8,5,7};  
  14.     static int IPI[]=new int[]{4,1,3,5,7,2,8,6};  
  15.     static int EP[]=new int[]{4,1,2,3,2,3,4,1};  
  16.     static int S0[][]={  
  17.          {1,0,3,2},  
  18.          {3,2,1,0},  
  19.          {0,2,1,3},  
  20.          {3,1,3,2},  
  21.     };  
  22.     static int S1[][]={  
  23.          {0,1,2,3},  
  24.          {2,0,1,3},  
  25.          {3,0,1,0},  
  26.          {2,1,0,3},  
  27.     };  
  28.     //根据数组交换  
  29.     static int Permute(int num,int p[],int pmax){  
  30.         int result=0;  
  31.         for(int i=0;i<p.length;i++){  
  32.             result<<=1;  
  33.             result|=(num>>(pmax-p[i]))&1;  
  34.         }  
  35.         return result;  
  36.     }  
  37.     //生成k1,k2  
  38.     static void SDES(String Key){  
  39.         int K=Integer.parseInt(Key,2);  
  40.         K=Permute(K,P10,10);  
  41.         int th=0,tl=0;  
  42.         th=(K>>5)&0x1f;//取Key的高5位  
  43.         tl=K&0x1f;     //取Key的低5位  
  44.         //LS-1  
  45.         th=((th&0xf)<<1) | ((th & 0x10) >> 4);//循环左移一位  
  46.         tl=((tl&0xf)<<1) | ((tl & 0x10) >> 4);//循环左移一位  
  47.         K1=Permute(( th << 5)| tl,P8,10);     //生成K1  
  48.         System.out.println("K1:"+Integer.toString(K1,2));  
  49.         //LS-2  
  50.         th=((th & 0x07)<< 2) |((th & 0x18) >> 3);//循环左移二位  
  51.         tl=((tl & 0x07)<< 2) |((tl & 0x18) >> 3);//循环左移二位  
  52.         K2=Permute((th<<5) | tl,P8,10);          //生成K2  
  53.         System.out.println("K2:"+Integer.toString(K2,2));  
  54.     }  
  55.     //f函数  
  56.     static int F(int R,int K){  
  57.         int t=Permute(R,EP,4)^K;  
  58.         int t0=(t>>4)& 0xf;  
  59.         int t1=t&0xf;  
  60.         t0= S0[((t0 & 0x8)>>2) | (t0 & 1)] [(t0>>1)&0x3];  
  61.         t1= S1[((t1 & 0x8)>>2) | (t1 & 1)] [(t1>>1)&0x3];  
  62.         t= Permute((t0<<2)|t1,P4,4);  
  63.         return t;  
  64.     }  
  65.     //fk函数  
  66.     static int fk(int input,int k){  
  67.         int l=(input>>4) & 0xf;  
  68.         int r=input & 0xf;  
  69.         return ((l^F(r,k))<< 4)| r;  
  70.     }  
  71.     //switch function  
  72.     static int SW(int x)  
  73.     {  
  74.          return ((x&0xf)<<4) | ((x>>4)&0xf);  
  75.     }  
  76.     //加密  
  77.     static String encrypt(String input){  
  78.         int m=Integer.parseInt(input, 2);  
  79.         m=Permute(m,IP,8);  
  80.         m=fk(m,K1);  
  81.         m=SW(m);  
  82.         m=fk(m,K2);  
  83.         m=Permute(m,IPI,8);  
  84.         return Integer.toString(m, 2);  
  85.     }  
  86.     //解密  
  87.     static String decrypt(String input){  
  88.         int m=Integer.parseInt(input, 2);  
  89.         m=Permute(m,IP,8);  
  90.         m=fk(m,K2);  
  91.         m=SW(m);  
  92.         m=fk(m,K1);  
  93.         m=Permute(m,IPI,8);  
  94.         return Integer.toString(m, 2);  
  95.     }  
  96.     public static void main(String[] args) throws IOException {  
  97.         // TODO Auto-generated method stub  
  98.         String plaintext,ciphertext,key;  
  99.         java.util.Scanner scan = new java.util.Scanner(System.in);  
  100.         System.out.println("1:加密,2:解密,3:退出,请输入一个数字来选择:");  
  101.         int  mode=scan.nextInt();  
  102.         while(true){  
  103.             if(mode==1){  
  104.                 System.out.println("请输入明文:");  
  105.                 plaintext=scan.next();  
  106.                 System.out.println("请输入密钥:");  
  107.                 key=scan.next();  
  108.                 SDES(key);  
  109.                 ciphertext=encrypt(plaintext);  
  110.                 //如果密文不足8位,补足8位  
  111.                 if(ciphertext.length()<8){  
  112.                     for(int i=0;i<8-ciphertext.length();i++)  
  113.                         ciphertext="0"+ciphertext;  
  114.                 }  
  115.                 System.out.println("加密后的密文为:"+ciphertext);  
  116.             }  
  117.             else if(mode==2){  
  118.                 System.out.println("请输入密文:");  
  119.                 ciphertext=scan.next();  
  120.                 System.out.println("请输入密钥:");  
  121.                 key=scan.next();  
  122.                 SDES(key);  
  123.                 plaintext=decrypt(ciphertext);  
  124.                 if(plaintext.length()<8){  
  125.                     for(int i=0;i<8-plaintext.length();i++)  
  126.                         plaintext="0"+plaintext;  
  127.                 }  
  128.                 System.out.println("解密后的明文为:"+plaintext);  
  129.             }  
  130.             else if(mode==3break;  
  131.             System.out.println("1:加密,2:解密,3:退出,请输入一个数字来选择:");  
  132.             mode=scan.nextInt();  
  133.         }  
  134.     }  
  135. }  

0 0
原创粉丝点击