字符串加密方法

None.gifusing System;
None.gif
using System.Collections.Generic;
None.gif
using System.Text;
None.gif
using System.Security.Cryptography;
None.gif
using System.IO;
None.gif
None.gif
namespace Component
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
public class Security
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public Security()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif        
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//默认密钥向量
ExpandedSubBlockStart.gifContractedSubBlock.gif
        private static byte[] Keys = dot.gif0x120x340x560x780x900xAB0xCD0xEF };
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// DES加密字符串
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="encryptString">待加密的字符串</param>
InBlock.gif        
/// <param name="encryptKey">加密密钥,要求为8位</param>
ExpandedSubBlockEnd.gif        
/// <returns>加密成功返回加密后的字符串,失败返回源串</returns>

InBlock.gif        public static string EncryptDES(string encryptString, string encryptKey)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(08));
InBlock.gif                
byte[] rgbIV = Keys;
InBlock.gif                
byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
InBlock.gif                DESCryptoServiceProvider dCSP 
= new DESCryptoServiceProvider();
InBlock.gif                MemoryStream mStream 
= new MemoryStream();
InBlock.gif                CryptoStream cStream 
= new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
InBlock.gif                cStream.Write(inputByteArray, 
0, inputByteArray.Length);
InBlock.gif                cStream.FlushFinalBlock();
InBlock.gif                
return Convert.ToBase64String(mStream.ToArray());
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return encryptString;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// DES解密字符串
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="decryptString">待解密的字符串</param>
InBlock.gif        
/// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param>
ExpandedSubBlockEnd.gif        
/// <returns>解密成功返回解密后的字符串,失败返源串</returns>

InBlock.gif        public static string DecryptDES(string decryptString, string decryptKey)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);
InBlock.gif                
byte[] rgbIV = Keys;
InBlock.gif                
byte[] inputByteArray = Convert.FromBase64String(decryptString);
InBlock.gif                DESCryptoServiceProvider DCSP 
= new DESCryptoServiceProvider();
InBlock.gif                MemoryStream mStream 
= new MemoryStream();
InBlock.gif                CryptoStream cStream 
= new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
InBlock.gif                cStream.Write(inputByteArray, 
0, inputByteArray.Length);
InBlock.gif                cStream.FlushFinalBlock();
InBlock.gif                
return Encoding.UTF8.GetString(mStream.ToArray());
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return decryptString;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }
 
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值