C#的3DES加密解密算法实例代码
发布时间:2023-09-23 01:10:19
C#类如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace ConsoleApplication1
{
/// <summary>
/// 加解密类
/// </summary>
public class EncryptHelper
{
//构造一个对称算法
private SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();
#region 加密解密函数
/// <summary>
/// 字符串的加密
/// </summary>
/// <param name="Value">要加密的字符串</param>
/// <param name="sKey">密钥,必须32位</param>
/// <param name="sIV">向量,必须是12个字符</param>
/// <returns>加密后的字符串</returns>
public string EncryptString(string Value, string sKey,string sIV)
{
try
{
ICryptoTransform ct;
MemoryStream ms;
CryptoStream cs;
byte[] byt;
mCSP.Key = Convert.FromBase64String(sKey);
mCSP.IV = Convert.FromBase64String(sIV);
//指定加密的运算模式
mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;
//获取或设置加密算法的填充模式
mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV);//创建加密对象
byt = Encoding.UTF8.GetBytes(Value);
ms = new MemoryStream();
cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
cs.Write(byt, 0, byt.Length);
cs.FlushFinalBlock();
cs.Close();
return Convert.ToBase64String(ms.ToArray());
}
catch (Exception ex)
{
//MessageBox.Show(ex.Message, "出现异常", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return ("Error in Encrypting " + ex.Message);
}
}
/// <summary>
/// 解密字符串
/// </summary>
/// <param name="Value">加密后的字符串</param>
/// <param name="sKey">密钥,必须32位</param>
/// <param name="sIV">向量,必须是12个字符</param>
/// <returns>解密后的字符串</returns>
public string DecryptString(string Value, string sKey, string sIV)
{
try
{
ICryptoTransform ct;//加密转换运算
MemoryStream ms;//内存流
CryptoStream cs;//数据流连接到数据加密转换的流
byte[] byt;
//将3DES的密钥转换成byte
mCSP.Key = Convert.FromBase64String(sKey);
//将3DES的向量转换成byte
mCSP.IV = Convert.FromBase64String(sIV);
mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;
mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV);//创建对称解密对象
byt = Convert.FromBase64String(Value);
ms = new MemoryStream();
cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
cs.Write(byt, 0, byt.Length);
cs.FlushFinalBlock();
cs.Close();
return Encoding.UTF8.GetString(ms.ToArray());
}
catch (Exception ex)
{
//MessageBox.Show(ex.Message, "出现异常", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return ("Error in Decrypting " + ex.Message);
}
}
#endregion
}
}
调用方法如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
EncryptHelper helper = new EncryptHelper();
//加密
string oldValue = "13800138000";
//加密后结果
//密钥,必须32位
string sKey = "qJzGEh6hESZDVJeCnFPGuxzaiB7NLQM5";
//向量,必须是12个字符
string sIV = "andyliu1234=";
//print
string newValue = helper.EncryptString(oldValue,sKey,sIV);
Console.WriteLine("加密后:"+ newValue);
//解密
string desValue = helper.DecryptString(newValue,sKey,sIV);
//
Console.WriteLine("解密后:"+ desValue);
Console.ReadLine();
}
}
}


猜你喜欢
- 本篇文章所涉及到的demo练习 使用的cloud 2021.0.3+ springboot2.6.8一、概述简介官网:https://doc
- RequestHeaders添加自定义参数在开发过程中有的时候,参数需要绑定到requestHeaders中,而并不是在body中进行传输。
- 1.pom.xml<?xml version="1.0" encoding="UTF-8"?&
- 前言兄弟们,刚刚又给seata社区修了一个BUG,有用户提了issue反应TransactionHook在某些情况下不会被调用:相关issu
- 背景众所周知,所有被打开的系统资源,比如流、文件或者Socket连接等,都需要被开发者手动关闭,否则随着程序的不断运行,资源泄露将会累积成重
- spring Boot 允许通过外部配置让你在不同的环境使用同一应用程序的代码,简单说就是可以通过配置文件来注入属性或者修改默认的配置。Sp
- SpringBoot分离打Jar包的两种方式方式一:基于maven-jar-plugin此方式基于这个小伙伴的配置改的:https://ww
- 对于本地图片我们可以通过selector来轻松的实现点击态。 但是在我们的项目中,一个关于对非本地图片的点击态实现还是难倒了不少人;因此专门
- 最近研究C#相关的ORC技术,图像识别一般C和C++这种底层语言做的比较多,C#主要是依托一些封装好的组件进行调用,这里介绍三种身份证识别的
- 引言从本篇文章开始,我们将介绍 Java AQS 的实现方式,本文先介绍 AQS 的内部数据是如何组织的,后面的文章中再分别介绍 AQS 的
- 用 Android studio软件写的一个 * 小游戏先上MainActivity.java 的代码。这里我用得定时器,本想用j
- 前言Activity是Android中一个很重要的概念,堪称四大组件之首,关于Activity有很多内容,比如生命周期和启动Flags,这二
- 需要装一个插件:File - Settings- Plugins - 搜索gson 安装GsonFromat;如下两张图安装完成后 ,新建一
- 最近看到一个考试系统,有个功能是用来监视进程的。一旦发现如Communicator.exe这样的违禁软件就立即杀死进程并上报给服务器。我稍
- 本文实例为大家分享了Android Studio实现帧动画的具体代码,供大家参考,具体内容如下按一定的顺序播放静态的图片1、几张联系的图片2
- (1)自定义泛型链表类。public class GenericList<T> { 
- 项目源码:https://gitee.com/tanwubo/jwt-spring-security-demo登录通过自定义的WxApple
- C# WinForm控件的拖动和缩放是个很有用的功能。实现起来其实很简单的,主要是设计控件的MouseDown、MouseLeave、Mou
- Java for循环几种写法整理概要:J2SE 1.5提供了另一种形式的for循环。借助这种形式的for循环,可以用更简单地方式来遍历数组和
- java 创建线程Java提供了线程类Thread来创建多线程的程序。其实,创建线程与创建普通的类的对象的操作是一样的,而线程就是Threa