当前位置:编程学习 > 网站相关 >>

.Net下的加密解密大全(5):玩转对称加密

 上篇博文http://www.zzzyk.com/kf/201204/125950.html 介绍了怎么玩转Hash算法,不过那篇文章没有交代清楚。这篇博文我将力争将那些没交代清楚的东东都说明白了。当然本文的主题是玩转对称加密。

    .NET下的对称加密技术主要有这么几种:DES,AES,Rijndael,TripleDES(3DES)。现在就来玩转它们吧!与上篇博文一样,首先创建一个接口:“IEncryptAndDecrypt”,然后为DES,AES,Rijnael,TripleDES(3DES)建立对应的类并实现接口“IEncryptAndDecrypt”。它们的结果如下图:

  \

这样我们在后期的编程中只需要简单的调用接口IEncryptAndDecrypt中的方法“Encrypt”和“Decrypt”就可以对数据进行加密解密啦。当然要实现这样的愿望我们还得继续努力,当前我的目标就是为它们找一个合格的lead,这样我们如果要找DES干活就可以直接对lead说,而不用满世界的去找咱们的DES。来瞧瞧咱们的Lead吧:
 
 \


 

咱们来瞧瞧这个Lead的能耐吧,看看它是否能胜任它的工作:


[csharp] EncryptAndDecryptInvoker leader;// = new EncryptAndDecryptInvoker(null, null);  
 
 
//执行环境  
CryptogramSetting setting = new CryptogramSetting() { }; 
string data="快去干活"; 
byte[] bData;//加密处理前  
byte[] aData;//机密处理后  
byte[] tmp; 
Encoding encoding=System.Text.Encoding.UTF8; 
 
 
 
//叫DES干活  
IEncrytAndDecrypt myDES = new DES(); 
setting.IV = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; 
setting.Key = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; 
setting.CryptogramType = CryptogramType.SymmetryEncryptAndDecrypt; 
//给lead发送要求  
leader = new EncryptAndDecryptInvoker(myDES, setting); 
 
Console.WriteLine("DES:"); 
tmp=leader.Encrypt(encoding.GetBytes(data)); 
Console.WriteLine("加密:"+Convert.ToBase64String(tmp)); 
tmp = leader.Decrypt(tmp); 
Console.WriteLine("解密:" + encoding.GetString(tmp)); 
 
 
 
 
 
 
//叫AES干活  
IEncrytAndDecrypt myAES = new AES(); 
setting.IV = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8,9,10,11,12,13,14,15,16 }; 
setting.Key = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 ,9,10,11,12,13,14,15,16}; 
setting.CryptogramType = CryptogramType.SymmetryEncryptAndDecrypt; 
//给lead发送要求  
leader = new EncryptAndDecryptInvoker(myAES, setting); 
 
Console.WriteLine("AES:"); 
tmp = leader.Encrypt(encoding.GetBytes(data)); 
Console.WriteLine("加密:" + Convert.ToBase64String(tmp)); 
tmp = leader.Decrypt(tmp); 
Console.WriteLine("解密:" + encoding.GetString(tmp)); 
 
 
 
 
 
 
 
 
//叫Rijndael干活  
IEncrytAndDecrypt myRijndael = new Rijndael(); 
setting.IV = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; 
setting.Key = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; 
setting.CryptogramType = CryptogramType.SymmetryEncryptAndDecrypt; 
//给lead发送要求   
leader = new EncryptAndDecryptInvoker(myRijndael, setting); 
 
Console.WriteLine("Rijndael:"); 
tmp = leader.Encrypt(encoding.GetBytes(data)); 
Console.WriteLine("加密:" + Convert.ToBase64String(tmp)); 
tmp = leader.Decrypt(tmp); 
Console.WriteLine("解密:" + encoding.GetString(tmp)); 
 
 
//叫3DES干活  
IEncrytAndDecrypt my3DES = new TripleDES(); 
setting.IV = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; 
setting.Key = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; 
setting.CryptogramType = CryptogramType.SymmetryEncryptAndDecrypt; 
//给lead发送要求  
leader = new EncryptAndDecryptInvoker(my3DES, setting); 
 
Console.WriteLine("3DES:"); 
tmp = leader.Encrypt(encoding.GetBytes(data)); 
Console.WriteLine("加密:" + Convert.ToBase64String(tmp)); 
tmp = leader.Decrypt(tmp); 
Console.WriteLine("解密:" + encoding.GetString(tmp)); 
 
 
Console.ReadKey(); 
            EncryptAndDecryptInvoker leader;// = new EncryptAndDecryptInvoker(null, null);
           
           
            //执行环境
            CryptogramSetting setting = new CryptogramSetting() { };
            string data="快去干活";
            byte[] bData;//加密处理前
            byte[] aData;//机密处理后
            byte[] tmp;
            Encoding encoding=System.Text.Encoding.UTF8;

 

            //叫DES干活
            IEncrytAndDecrypt myDES = new DES();
            setting.IV = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
            setting.Key = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
            setting.CryptogramType = CryptogramType.SymmetryEncryptAndDecrypt;
            //给lead发送要求
            leader = new EncryptAndDecryptInvoker(myDES, setting);
          
            Console.WriteLine("DES:");
            tmp=leader.Encrypt(encoding.GetBytes(data));
            Console.WriteLine("加密:"+Convert.ToBase64String(tmp));
            tmp = leader.Decrypt(tmp);
            Console.WriteLine("解密:" + encoding.GetString(tmp));

 

 


            //叫AES干活
            IEncrytAndDecrypt myAES = new AES();
            setting.IV = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8,9,10,11,12,13,14,15,16 };
            setting.Key = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 ,9,10,11,12,13,14,15,16};
          &n

补充:综合编程 , 安全编程 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,