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

推荐订阅源

爱范儿
爱范儿
博客园_首页
W
WeLiveSecurity
S
Secure Thoughts
S
Security @ Cisco Blogs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Hugging Face - Blog
Hugging Face - Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
H
Hacker News: Front Page
Project Zero
Project Zero
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
U
Unit 42
N
News and Events Feed by Topic
N
News and Events Feed by Topic
Hacker News - Newest:
Hacker News - Newest: "LLM"
Forbes - Security
Forbes - Security
T
Tor Project blog
I
Intezer
B
Blog
F
Full Disclosure
Security Archives - TechRepublic
Security Archives - TechRepublic
F
Fortinet All Blogs
Schneier on Security
Schneier on Security
T
Threat Research - Cisco Blogs
AI
AI
Google DeepMind News
Google DeepMind News
L
LINUX DO - 最新话题
Cloudbric
Cloudbric
L
Lohrmann on Cybersecurity
WordPress大学
WordPress大学
博客园 - 聂微东
雷峰网
雷峰网
P
Privacy International News Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
PCI Perspectives
PCI Perspectives
Y
Y Combinator Blog
Spread Privacy
Spread Privacy
Simon Willison's Weblog
Simon Willison's Weblog
罗磊的独立博客
Vercel News
Vercel News
A
Arctic Wolf
The Register - Security
The Register - Security
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Microsoft Azure Blog
Microsoft Azure Blog
H
Heimdal Security Blog
Know Your Adversary
Know Your Adversary
P
Proofpoint News Feed
C
Cybersecurity and Infrastructure Security Agency CISA
P
Proofpoint News Feed

博客园 - 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、Rijindael、TripleDES
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();
        }
   
    }
}