欢迎来到飞鸟慕鱼博客,开始您的技术之旅!
当前位置: 首页知识笔记正文

Title: An example of private key encryption

墨初 知识笔记 135阅读

Use the system; Use the system. Security; Use the system. Safe. Cryptography; Use the system. Io; The abstract of the namespace cry {/* *//summary//cryop. Overview of solid rgba(128, 128, 128, 1); Display : None; The background-color: rgba(255, 255, 255, 1)' {//key is equal to the length of the injected vein, privatestaticByte[]Keys=System. Text.Encoding.Unicode.GetBytes ('Everyday Up'); privatestaticByte[]IV=new byte[]{0x 12,0x34,0x56,0x78,0x90,

"color: rgba(0, 0, 0, 1)">0xAB0xCD0xEF};
        

        
/// <summary>
        
/// 加密
        
/// </summary>

        public static Byte[] Enc(String test)
        
{            
            System.Security.Cryptography.RC2CryptoServiceProvider rc2 
= new RC2CryptoServiceProvider();
            MemoryStream msStream 
= new MemoryStream();    
            
byte[] toEncrypt;    
            
try
            
{
                ICryptoTransform encrypt 
= rc2.CreateEncryptor(Keys,IV);
                
                CryptoStream csStream 
= new CryptoStream(msStream,encrypt,CryptoStreamMode.Write);

                toEncrypt 
= System.Text.Encoding.Unicode.GetBytes(test);
                csStream.Write(toEncrypt,
0,toEncrypt.Length);
                csStream.FlushFinalBlock();
            }

            
catch(Exception ex)
            
{
                
throw new Exception(ex.Message,ex);
            }

            
return msStream.ToArray();

        }
 

        
/// <summary>
        
/// 解密
        
/// </summary>

        public static String Dec(Byte[] enc)
        
{
            System.Security.Cryptography.RC2CryptoServiceProvider rc2 
= new RC2CryptoServiceProvider();
            
try
            
{
                ICryptoTransform decrypt 
= rc2.CreateDecryptor(Keys,IV);
                MemoryStream msDec 
= new MemoryStream(enc);
                CryptoStream csDec 
= new CryptoStream(msDec,decrypt,CryptoStreamMode.Read);
                
byte[] fromEncpty = new Byte[enc.Length];
                csDec.Read(fromEncpty,
0,fromEncpty.Length);
                
return System.Text.Encoding.Unicode.GetString(fromEncpty);
            }

            
catch(Exception ex)
            
{
                
throw new Exception(ex.Message,ex);
            }

            
        }

    }

}

标签:
声明:无特别说明,转载请标明本文来源!