如何使用Base64进行加密和解密

 

// ----------------------------- Base64 class --------------------------------------
// ---------------------------------------------------------------------------------
// ---File:          clsBase64
// ---Description:   The class file to encode string or decode string in base algorithm 
// ---Author:        Knight
// ---Created Date:  Oct.8, 2005
// ---Modified Date: Jul.4, 2006
// ---------------------------------------------------------------------------------
// ----------------------------{ Base64 class }-------------------------------------

using  System;

namespace  Base64
{
    
using System.Text;
    
/// <summary>
    
/// Summary description for clsBase64.
    
/// </summary>

    public class clsBase64
    
{
        
protected clsBase64()
        
{
            
//Avoid to be inited
        }


        
Base64_Algorithm_Implement 
        
/// <summary>
        
/// Encrypt data based on specific key
        
/// </summary>
        
/// <param name="Data">the data to be encrypted</param>
        
/// <param name="Key">key data</param>
        
/// <returns>If successfully, return encrypted string; else return NULL</returns>

        public static string EncryptData( string Data, string Key )
        
{
            
if( Data == null || Data == "" ) return null;

            
if( Key == null || Key == "" ) return null;

            
char[] chrEncrypted = GetEncoded( Key.ToCharArray(), 
                Encoding.Unicode.GetBytes( Data ) );
            
if( chrEncrypted != null )
                
return new string( chrEncrypted );
            
else
                
return null;
        }

        
/// <summary>
        
/// Decrypt data based on specific key
        
/// </summary>
        
/// <param name="Data">the data to be decrypted</param>
        
/// <param name="Key">key data</param>
        
/// <returns>If successfully, return decrypted string; else return NULL</returns>

        public static string DecryptData( string Data, string Key )
        
{
            
if( Data == null || Data == "" ) return null;

            
if( Key == null || Key == "" ) return null;

            
byte[] bDecrypted = GetDecoded( Key.ToCharArray(),
                Data.ToCharArray() );
            
if( bDecrypted != null )
                
return Encoding.Unicode.GetString( bDecrypted );
            
else
                
return null;
        }

    }


}

//  调用如下:
  
//  Encrypt data 
     string  strEncryptedData  =  Base64.clsBase64.EncryptData(  yourData, yourKey );  
    
if ( strEncryptedData  !=   null  )
        MessageBox.Show( strEncryptedData );

    
//  Decrypt data
     string  strDecryptedData  =  Base64.clsBase64.DecryptData(  yourData, yourKey );
    
if ( strDecryptedData  !=   null  )
        MessageBox.Show( strDecryptedData );
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值