netcfln 发表于 2011-12-21 08:43

DES算法

数据加密算法DES数据加密算法(Data Encryption
Algorithm,DEA)是一种对称加密算法,很可能是使用最广泛的密钥系统,特别是在保护金融数据的安全中,最初开发的DEA是嵌入硬件中的。通
常,自动取款机(Automated Teller
Machine,ATM)都使用DEA。它出自IBM的研究工作,IBM也曾对它拥有几年的专利权,但是在1983年已到期后,处于公有范围中,允许在特
定条件下可以免除专利使用费而使用。1977年被美国政府正式采纳。<br><br>数据加密标准DES
 <br> DES的原始思想可以参照二战德国的恩格玛机,其基本思想大致相同。传统的密码加密都是由古代的循环移位思想而来,恩格玛机在这个基础之上进行了扩散模糊。但是本质原理都是一样的。现代DES在二进制级别做着同样的事:替代模糊,增加分析的难度。<br><br><span class="headline-content">加密原理</span> 
 DES 使用一个 56 位的密钥以及附加的 8 位奇偶校验位,产生最大 64 位的分组大小。这是一个迭代的分组密码,使用称为 Feistel
的技术,其中将加密的文本块分成两半。使用子密钥对其中一半应用循环功能,然后将输出与另一半进行“异或”运算;接着交换这两半,这一过程会继续下去,但
最后一个循环不交换。DES 使用 16 个循环,使用异或,置换,代换,移位操作四种基本运算。<br><br><span class="headline-content">三重 DES</span>  DES 的常见变体是三重 DES,使用 168 位的密钥对资料进行三次加密的一种机制;它通常(但非始终)提供极其强大的安全性。如果三个 56 位的子元素都相同,则三重 DES 向后兼容 DES。<span class="headline-content">破解方法</span> 
 攻击 DES 的主要形式被称为蛮力的或彻底密钥搜索,即重复尝试各种密钥直到有一个符合为止。如果 DES 使用 56
位的密钥,则可能的密钥数量是 2 的 56 次方个。随着计算机系统能力的不断发展,DES
的安全性比它刚出现时会弱得多,然而从非关键性质的实际出发,仍可以认为它是足够的。不过 ,DES 现在仅用于旧系统的鉴定,而更多地选择新的加密标准
— 高级加密标准(Advanced Encryption Standard,AES)。
  新的分析方法有差分分析法和线性分析法两种<br><br><span class="headline-content">DES算法的安全性</span>  一.安全性比较高的一种算法,目前只有一种方法可以破解该算法,那就是穷举法.
  二.采用64位密钥技术,实际只有56位有效,8位用来校验的.譬如,有这样的一台PC机器,它能每秒计算一百万次,那么256位空间它要穷举的时间为2285年.所以这种算法还是比较安全的一种算法.
  TripleDES。该算法被用来解决使用 DES 技术的 56
位时密钥日益减弱的强度,其方法是:使用两个独立密钥对明文运行 DES 算法三次,从而得到 112 位有效密钥强度。TripleDES 有时称为
DESede(表示加密、解密和加密这三个阶段)。<br><br>NETC C实现:<br>#include&lt;iostream.h&gt;
<br>class SubKey{                     //定义子密钥为一个类<br>&nbsp;public:
<br>int key;
<br>}subkey;                        //定义子密钥对象数组
<br><br>class DES{ &nbsp;<br><br>&nbsp;int encipher_decipher; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;          //判断加密还是解密<br>&nbsp;int key_in;   &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; //用户原始输入的64位二进制数<br>&nbsp;int key_out; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;                   //除去每行的最后一位校验位<br>&nbsp;int c0_d0; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;                      //存储经PC-1转换后的56位数据<br>&nbsp;int c0,d0; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;               //分别存储c0,d0<br>&nbsp;int text; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;                     //64位明文<br>&nbsp;int text_ip; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;                  //经IP转换过后的明文 <br>&nbsp;int A,B; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;                  //A,B分别存储经IP转换过后明文的两部分,便于交换<br>&nbsp;int temp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;                      //存储经扩展置换后的48位二进制值<br>&nbsp;int temp1;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //存储和子密钥异或后的结果<br>&nbsp;int s_result; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;                   //存储经S变换后的32位值<br>&nbsp;int text_p; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;                     //经P置换后的32位结果<br>&nbsp;int secret_ip; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;                  //经逆IP转换后的密文<br>public:<br>&nbsp;void Key_Putting();<br>&nbsp;void PC_1();<br>&nbsp;int function(int,int);    //异或<br>&nbsp;void SubKey_Production();<br>&nbsp;void IP_Convert();<br>&nbsp;void f();      
void _IP_Convert();            
void Out_secret();<br>&nbsp;};<br>void DES::Key_Putting()         //得到密钥中对算法有用的56位<br>&nbsp;{<br>&nbsp;cout&lt;&lt;"请输入64位的密钥(8行8列且每行都得有奇数个1):\n";<br>&nbsp;for(int i=0;i&lt;8;i++)<br>&nbsp;for(int j=0;j&lt;8;j++){<br>&nbsp;cin&gt;&gt;key_in;<br>&nbsp;if(j!=7) key_out=key_in;<br>&nbsp;}<br>&nbsp;}<br><br>void DES::PC_1()                     //PC-1置换函数<br>&nbsp;{<br>&nbsp;int pc_1={                        //PC-1<br>&nbsp; {57, 49, 41, 33, 25, 17, 9},<br>&nbsp; {1, 58, 50, 42, 34, 26, 18},
{10,2, 59, 51, 43, 35, 27},<br>&nbsp; {19, 11,3, 60, 52, 44, 36},
{63, 55, 47, 39, 31, 23, 15},<br>&nbsp; {7, 62, 54, 46, 38, 30, 22},
{14,6, 61, 53, 45, 37, 29},<br>&nbsp; {21, 13,5, 28, 20, 12,4}<br>&nbsp;};<br>int i,j;<br>&nbsp;for(i=0;i&lt;8;i++)<br>&nbsp;for(j=0;j&lt;7;j++)<br>&nbsp; &nbsp; c0_d0=key_out[ (pc_1-1)/8 ][ (pc_1-1)%8 ];<br>&nbsp;}<br>&nbsp;int DES::function(int a,int b)    //模拟二进制数的异或运算,a和b为整型的0和1,返回值为整型的0或1<br>&nbsp;{<br>&nbsp;if(a!=b)return 1;<br>&nbsp;else return 0;<br>&nbsp;}<br><br>void DES::SubKey_Production()            //生成子密钥<br>&nbsp;{<br>&nbsp;int move={       //循环左移的位数<br>&nbsp; 1 , 1 , 2 , 1 ,<br>&nbsp; 3 , 2 , 4 , 2 ,
5 , 2 , 6 , 2 ,<br>&nbsp; 7 , 2 , 8 , 2 ,<br>&nbsp; 9 , 1, 10 , 2,
11 , 2, 12 , 2,<br>&nbsp; 13 , 2, 14 , 2,<br>&nbsp; 15 , 2, 16 , 1<br>&nbsp;};<br>&nbsp;int pc_2={                     //PC-2<br>&nbsp;14, 17 ,11 ,24 , 1 , 5,<br>&nbsp;3 ,28 ,15 , 6 ,21 ,10,<br>&nbsp;23, 19, 12,4, 26,8,<br>&nbsp;16, 7, 27, 20 ,13 , 2,<br>&nbsp;41, 52, 31, 37, 47, 55,<br>&nbsp;30, 40, 51, 45, 33, 48,<br>&nbsp;44, 49, 39, 56, 34, 53,<br>&nbsp;46, 42, 50, 36, 29, 32<br>&nbsp;};<br><br>for(int i=0;i&lt;16;i++)   //生成子密钥<br>&nbsp;{<br>&nbsp;int j,k;<br>&nbsp;int a,b;   
int bb,cc;<br>&nbsp;for(j=0;j&lt;4;j++)<br>&nbsp;for(k=0;k&lt;7;k++)<br>&nbsp;c0=c0_d0;<br>&nbsp;for(j=4;j&lt;8;j++)<br>&nbsp;for(k=0;k&lt;7;k++)<br>&nbsp;d0=c0_d0;<br>&nbsp;for(j=0;j&lt;4;j++)
   for(k=0;k&lt;7;k++){<br>&nbsp;bb=c0;<br>&nbsp;cc=d0;<br>&nbsp;}<br>&nbsp;for(j=0;j&lt;move;j++){<br>&nbsp;a=bb;<br>&nbsp;b=cc;<br>&nbsp;}<br>&nbsp;for(j=0;j&lt;28-move;j++){<br>&nbsp;bb=bb;<br>&nbsp;cc=cc;<br>&nbsp;}<br>&nbsp;for(j=0;j&lt;move;j++){<br>&nbsp;bb=a;<br>&nbsp;cc=b;<br>&nbsp;}<br>&nbsp;for(j=0;j&lt;28;j++){<br>&nbsp;c0=bb;<br>&nbsp;d0=cc;<br>&nbsp;}<br>&nbsp;for(j=0;j&lt;4;j++)          //L123--L128是把c0,d0合并成c0_d0<br>&nbsp;for(k=0;k&lt;7;k++)<br>&nbsp;c0_d0=c0;<br>&nbsp;for(j=4;j&lt;8;j++)<br>&nbsp;for(k=0;k&lt;7;k++)<br>&nbsp;c0_d0=d0;<br>&nbsp;for(j=0;j&lt;8;j++)          //对Ci,Di进行PC-2置换<br>&nbsp;for(k=0;k&lt;6;k++)<br>&nbsp;subkey.key=c0_d0[(pc_2-1)/7][(pc_2-1)%7];<br>&nbsp;}<br>&nbsp;}<br>&nbsp;void DES::IP_Convert()<br>&nbsp;{<br>&nbsp;int IP={               //初始置换IP矩阵<br>&nbsp;58, 50, 42, 34, 26, 18, 10,2,<br>&nbsp;60, 52, 44, 36, 28, 20, 12,4,<br>&nbsp;62, 54, 46, 38, 30, 22, 14,6,
64, 56, 48, 40, 32, 24, 16,8,<br>&nbsp;57, 49, 41, 33, 25, 17,9,1,<br>&nbsp;59, 51, 43, 35, 27, 19, 11,3,
61, 53, 45, 37, 29, 21, 13,5,<br>&nbsp;63, 55, 47, 39, 31, 23, 15,7<br>&nbsp;};<br>&nbsp;cout&lt;&lt;"你好,你要加密还是解密?加密请按1号键(输入1),解密请按2号键,并确定."&lt;&lt;'\n';<br>&nbsp;cin&gt;&gt;encipher_decipher;<br>&nbsp;char * s;<br>&nbsp;if(encipher_decipher==1) s="明文";<br>&nbsp;elses="密文";<br>&nbsp;cout&lt;&lt;"请输入64位"&lt;&lt;s&lt;&lt;"(二进制):\n";<br>&nbsp;int i,j;<br>&nbsp;for(i=0;i&lt;8;i++)<br>&nbsp;for(j=0;j&lt;8;j++)<br>&nbsp;cin&gt;&gt;text;<br>&nbsp;for(i=0;i&lt;8;i++)                        //进行IP变换<br>&nbsp;for(j=0;j&lt;8;j++)<br>&nbsp;text_ip=text[(IP-1)/8][(IP-1)%8];<br>&nbsp;}<br><br><br><br><br><br><br>
页: [1]
查看完整版本: DES算法