惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
V2EX
C
Check Point Blog
博客园_首页
B
Blog
D
Docker
U
Unit 42
量子位
I
InfoQ
有赞技术团队
有赞技术团队
Martin Fowler
Martin Fowler
GbyAI
GbyAI
L
LangChain Blog
云风的 BLOG
云风的 BLOG
博客园 - Franky
美团技术团队
T
The Blog of Author Tim Ferriss
阮一峰的网络日志
阮一峰的网络日志
月光博客
月光博客
Vercel News
Vercel News
Recent Announcements
Recent Announcements
雷峰网
雷峰网
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
Google DeepMind News
Google DeepMind News

博客园 - JieNet

MFC中的数据类型 HardwareSerialNumber(硬盘号,CPU号) - JieNet - 博客园 Windows 7 与 XP、Vista 特性对照表 typename 还是超级无敌的基础。。。 函数指针。。。超级无敌的基础了 可变参数的基本应用 关于const和函数 - JieNet - 博客园 iterator类对象和普通指针 委托与事件 事件 ID: 5603 将DAL层从Sql Server 迁移到 Access 时遇到的问题 ListView:How to.... CSS剪切图片 SQL Server TransAction 全部回滚 所有希腊字母及读音 Error:'Sys' is undefined. VS2008 下安装 AjaxControlToolkit-Framework3.5 JS,CSS 禁止复制,禁止打印,禁止…… ADSL拨号错误代码详解
.NET中的加密解密:私钥加密(对称加密):AES、DES、RC2、Ri...
JieNet · 2009-04-11 · via 博客园 - JieNet

namespace Encrypt_And_Decrypt_PrivateKey
{
    
public class AesSample
    {
        Rijndael aes 
= null;public AesSample()
        {
            aes 
= Rijndael.Create();

        }

public string Encrypt(string data,byte[] bKey)
        {
            
try
            {
                
byte[] bData = Encoding.UTF8.GetBytes(data);
                aes.Key 
= bKey;
                aes.Mode 
= CipherMode.ECB;
                aes.Padding 
= PaddingMode.PKCS7;

                ICryptoTransform iCryptoTransform 

= aes.CreateEncryptor();
                
byte[] bResult =
                    iCryptoTransform.TransformFinalBlock(bData, 
0, bData.Length);return Convert.ToBase64String(bResult);
            }
            
catch
            {
                
throw;
            }
        }
public string Decrypt(string data, byte[] bKey)
        {
            
try
            {
                
byte[] bData = Convert.FromBase64String(data);
                aes.Key 
= bKey;
                aes.Mode 
= CipherMode.ECB;
                aes.Padding 
= PaddingMode.PKCS7;

                ICryptoTransform iCryptoTransform 

= aes.CreateDecryptor();
                
byte[] bResult =
                    iCryptoTransform.TransformFinalBlock(bData, 
0, bData.Length);
                
return Encoding.UTF8.GetString(bResult);
            }
            
catch
            {
                
throw;
            }
        }

    }

//class AesSample

    
public class DesSample
    {
        DES des 
= null;public DesSample()
        {
            des 
= new DESCryptoServiceProvider();
        }
public string Encrypt(string data, byte[] bKey)
        {
            
try
            {
                
byte[] bData = Encoding.UTF8.GetBytes(data);
                des.Key 
= bKey;

                ICryptoTransform iCryptoTransform 

= des.CreateEncryptor();
                
byte[] bResult =
                    iCryptoTransform.TransformFinalBlock(bData, 
0, bData.Length);return Convert.ToBase64String(bResult);
            }
            
catch
            {
                
throw;
            }
        }
public string Decrypt(string data, byte[] bKey)
        {
            
try
            {
                
byte[] bData = Convert.FromBase64String(data);
                des.Key 
= bKey;

                ICryptoTransform iCryptoTransform 

= des.CreateDecryptor();
                
byte[] bResult =
                    iCryptoTransform.TransformFinalBlock(bData, 
0, bData.Length);
                
return Encoding.UTF8.GetString(bResult);
            }
            
catch
            {
                
throw;
            }
        }

    }

//class DesSample

    
public class Rc2Sample
    {
        RC2 rc2 
= null;public Rc2Sample()
        {
            rc2 
= new RC2CryptoServiceProvider();
        }
public string Encrypt(string data, byte[] bKey)
        {
            
try
            {
                
byte[] bData = Encoding.UTF8.GetBytes(data);
                rc2.Key 
= bKey;

                ICryptoTransform iCryptoTransform 

= rc2.CreateEncryptor();
                
byte[] bResult =
                    iCryptoTransform.TransformFinalBlock(bData, 
0, bData.Length);return Convert.ToBase64String(bResult);
            }
            
catch
            {
                
throw;
            }
        }
public string Decrypt(string data, byte[] bKey)
        {
            
try
            {
                
byte[] bData = Convert.FromBase64String(data);
                rc2.Key 
= bKey;

                ICryptoTransform iCryptoTransform 

= rc2.CreateDecryptor();
                
byte[] bResult =
                    iCryptoTransform.TransformFinalBlock(bData, 
0, bData.Length);
                
return Encoding.UTF8.GetString(bResult);
            }
            
catch
            {
                
throw;
            }
        }
    }
public class RijndaelSample
    {
        Rijndael rijndael 
= null;public RijndaelSample()
        {
            rijndael 
= new RijndaelManaged();
        }
public string Encrypt(string data, byte[] bKey)
        {
            
try
            {
                
byte[] bData = Encoding.UTF8.GetBytes(data);
                rijndael.Key 
= bKey;

                ICryptoTransform iCryptoTransform 

= rijndael.CreateEncryptor();
                
byte[] bResult =
                    iCryptoTransform.TransformFinalBlock(bData, 
0, bData.Length);return Convert.ToBase64String(bResult);
            }
            
catch
            {
                
throw;
            }
        }
public string Decrypt(string data, byte[] bKey)
        {
            
try
            {
                
byte[] bData = Convert.FromBase64String(data);
                rijndael.Key 
= bKey;

                ICryptoTransform iCryptoTransform 

= rijndael.CreateDecryptor();
                
byte[] bResult =
                    iCryptoTransform.TransformFinalBlock(bData, 
0, bData.Length);
                
return Encoding.UTF8.GetString(bResult);
            }
            
catch
            {
                
throw;
            }
        }

    }

public class TripleDESSample
    {
        TripleDES tDes 
= null;public TripleDESSample()
        {
            tDes 
= new TripleDESCryptoServiceProvider();
        }
public string Encrypt(string data, byte[] bKey)
        {
            
try
            {
                
byte[] bData = Encoding.UTF8.GetBytes(data);
                tDes.Key 
= bKey;

                ICryptoTransform iCryptoTransform 

= tDes.CreateEncryptor();
                
byte[] bResult =
                    iCryptoTransform.TransformFinalBlock(bData, 
0, bData.Length);return Convert.ToBase64String(bResult);
            }
            
catch
            {
                
throw;
            }
        }
public string Decrypt(string data, byte[] bKey)
        {
            
try
            {
                
byte[] bData = Convert.FromBase64String(data);
                tDes.Key 
= bKey;

                ICryptoTransform iCryptoTransform 

= tDes.CreateDecryptor();
                
byte[] bResult =
                    iCryptoTransform.TransformFinalBlock(bData, 
0, bData.Length);
                
return Encoding.UTF8.GetString(bResult);
            }
            
catch
            {
                
throw;
            }
        }
    }

}

//ns

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;namespace Encrypt_And_Decrypt_PrivateKey
{
    
class Program
    {
        
static void Main(string[] args)
        {
            
byte[] bKey8 = UTF8Encoding.UTF8.GetBytes("12345678");
            
byte[] bKey16 = UTF8Encoding.UTF8.GetBytes("1234567890123456");
            
byte[] bKey32 = UTF8Encoding.UTF8.GetBytes("12345678901234567890123456789012");string data = "Hello,I am Jie.";

            Console.WriteLine(

"Aes Sample");
            AesSample aes 
= new AesSample();
            
string aesData = "";
            aesData 
= aes.Encrypt(data, bKey32);
            Console.WriteLine(
"AES Encrypt:{0}", aesData);
            Console.WriteLine(
"AES Decrypt:{0}", aes.Decrypt(aesData, bKey32));
            Console.WriteLine();

            Console.WriteLine(

"Des Sample");
            DesSample des 
= new DesSample();
            
string desData = "";
            desData 
= des.Encrypt(data, bKey8);
            Console.WriteLine(
"DES Encrypt:{0}", desData);
            Console.WriteLine(
"DES Decrypt:{0}", des.Decrypt(desData, bKey8));
            Console.WriteLine();

            Console.WriteLine(

"RC2 Sample");
            Rc2Sample rc2 
= new Rc2Sample();
            
string rc2Data = "";
            rc2Data 
= rc2.Encrypt(data, bKey8);
            Console.WriteLine(
"RC2 Encrypt:{0}", rc2Data);
            Console.WriteLine(
"RC2 Decrypt:{0}", rc2.Decrypt(rc2Data, bKey8));
            Console.WriteLine();

            Console.WriteLine(

"Rijndael Sample");
            RijndaelSample rijndael 
= new RijndaelSample();
            
string rijndaelData = "";
            rijndaelData 
= rijndael.Encrypt(data, bKey32);
            Console.WriteLine(
"Rijndael Encrypt:{0}", rijndaelData);
            Console.WriteLine(
"Rijndael Decrypt:{0}", rijndael.Decrypt(rijndaelData, bKey32));
            Console.WriteLine();

            Console.WriteLine(

"TripleDES Sample");
            TripleDESSample tripleDES 
= new TripleDESSample();
            
string tripleDESData = "";
            tripleDESData 
= tripleDES.Encrypt(data, bKey16);
            Console.WriteLine(
"TripleDES Encrypt:{0}", tripleDESData);
            Console.WriteLine(
"TripleDES Decrypt:{0}", tripleDES.Decrypt(tripleDESData, bKey16));
            Console.WriteLine();

            Console.Read();
        }
   
    }
}