zoukankan      html  css  js  c++  java
  • .net加密解密


    前几天因为要做点加密解密的东西来保存配置信息,找了很久终于找到一个比较常用的类,贴出来.共享...
    里面已经有两种加密解密的算法:DES, Rijndael
    加需要的,去查一下msdn,自己加啦..
        public class SymmCrypto
        

            
    public enum SymmProvEnum : int
            
    {
                DES, Rijndael
            }


            
    private static SymmetricAlgorithm mobjCryptoService;
     
            
    public static void ChangeCryptoService(SymmProvEnum NetSelected)
            
    {
                
    switch (NetSelected)
                
    {
                    
    case SymmProvEnum.DES:
                        mobjCryptoService 
    = new DESCryptoServiceProvider();
                        
    break
                    
    case SymmProvEnum.Rijndael:
                        mobjCryptoService 
    = new RijndaelManaged();
                        
    break;
                }

            }


            
    public static void ChangeCryptoService(SymmetricAlgorithm ServiceProvider)
            
    {
                mobjCryptoService 
    = ServiceProvider;
            }


            
    private static byte[] GetLegalKey(string Key)
            
    {
                
    string sTemp;
                
    if (mobjCryptoService.LegalKeySizes.Length > 0)
                
    {
                    
    int lessSize = 0, moreSize = mobjCryptoService.LegalKeySizes[0].MinSize; 
                    
    while (Key.Length * 8 > moreSize)
                    
    {
                        lessSize 
    = moreSize;
                        moreSize 
    += mobjCryptoService.LegalKeySizes[0].SkipSize;
                    }

                    sTemp 
    = Key.PadRight(moreSize / 8' ');
                }

                
    else
                    sTemp 
    = Key;
     
                
    return ASCIIEncoding.ASCII.GetBytes(sTemp);
            }


            
    static void SetmobjCryptoService()
            
    {
                
    if (null == mobjCryptoService)
                    mobjCryptoService 
    = new RijndaelManaged();
            }


            
    public static string Encrypting(string Source, string Key)
            
    {
                SetmobjCryptoService();

                
    byte[] bytIn = System.Text.ASCIIEncoding.ASCII.GetBytes(Source); 
                System.IO.MemoryStream ms 
    = new System.IO.MemoryStream();

                
    byte[] bytKey = GetLegalKey(Key);
     
                mobjCryptoService.Key 
    = bytKey;
                mobjCryptoService.IV 
    = bytKey;
     
                ICryptoTransform encrypto 
    = mobjCryptoService.CreateEncryptor();
     
                CryptoStream cs 
    = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
     
                cs.Write(bytIn, 
    0, bytIn.Length);
                cs.FlushFinalBlock();
     
                
    byte[] bytOut = ms.GetBuffer();
                
    int i = 0;
                
    for (i = 0; i < bytOut.Length; i++)
                    
    if (bytOut[i] == 0)
                        
    break;
     
                
    return System.Convert.ToBase64String(bytOut, 0, i);
            }


            
    public static string Decrypting(string Source, string Key)
            
    {
                SetmobjCryptoService();

                
    byte[] bytIn = System.Convert.FromBase64String(Source); 
                System.IO.MemoryStream ms 
    = new System.IO.MemoryStream(bytIn, 0, bytIn.Length);

                
    byte[] bytKey = GetLegalKey(Key);

                mobjCryptoService.Key 
    = bytKey;
                mobjCryptoService.IV 
    = bytKey;
     
                ICryptoTransform encrypto 
    = mobjCryptoService.CreateDecryptor();
     
                CryptoStream cs 
    = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
     
                System.IO.StreamReader sr 
    = new System.IO.StreamReader(cs);
                
    return sr.ReadToEnd();
            }

        }

  • 相关阅读:
    安全编码1
    VPP tips
    VPP概述汇总
    C语言安全编码摘录
    TCP-proxy
    Scipy Lecture Notes学习笔记(一)Getting started with Python for science 1.4. Matplotlib: plotting
    Scipy Lecture Notes学习笔记(一)Getting started with Python for science 1.3. NumPy: creating and manipulating numerical data
    Scipy Lecture Notes学习笔记(一)Getting started with Python for science 1.2. The Python language
    Scipy Lecture Notes学习笔记(一)Getting started with Python for science 1.1. Python scientific computing ecosystem
    25马5跑道,求最快的五匹马的需要比赛的次数
  • 原文地址:https://www.cnblogs.com/yans/p/1217231.html
Copyright © 2011-2022 走看看