• .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();
            }

        }

  • 相关阅读:
    微信小游戏5.2.2 在子项目中使用EUI制作排行榜报错 wx.getFileSystemManager not function
    Egret5.2.2 微信小游戏行的示例排行榜
    Python翻译
    Python-docx库的使用
    用百度文字识别实现图片文本识别
    基于airtest的朋友圈自动点赞
    使用豆瓣源安装python包
    Appium 环境配置遇到的坑
    使用Pyppeteer进行gmail模拟登录
    异步爬虫
  • 原文地址:https://www.cnblogs.com/yans/p/1217231.html
Copyright © 2020-2023  润新知