C#加密解密

C#常用加密解密方法(DES加密解密) 本节主要分享DES加密解密:DES,全称Data Encryption Standard,是一种对称加密算法。由于其安全性比较高(有限时间内,没有一种加密方法可以说是100%安全),很可能是最广泛的密钥系统(我们公司也在用,估计你们也有在用....),唯一一种方法可以破解该算法,那就是穷举法。DES(Data Encryption Standard)和TripleDES是对称加密的两种实现。DES和TripleDES基本算法一致,只是TripleDES算法提供的key位数更多,加密可靠性更高。 阅读详情

C#加密解密大全
1、方法一 (不可逆加密)

复制代码
复制代码

public string EncryptPassword(string PasswordString,string PasswordFormat ) 
{ 
   string  encryptPassword = null;
   if (PasswordFormat="SHA1")
   { 
      encryptPassword=FormsAuthortication.HashPasswordForStoringInConfigFile(PasswordString ,"SHA1"); 
   } 
   elseif (PasswordFormat="MD5") 
   { 
      encryptPassword=FormsAuthortication.HashPasswordForStoringInConfigFile(PasswordString ,"MD5"); 
   }
     return encryptPassword ;
}

复制代码
复制代码
2、方法二 (可逆加密)

复制代码
复制代码

public interface IBindesh
{
string encode(string str);
string decode(string str);
}

public class EncryptionDecryption : IBindesh
{
    public string encode(string str)
     {
        string htext = ""; 

        for ( int i = 0; i < str.Length; i++)
        {
            htext = htext + (char) (str[i] + 10 - 1 * 2);
        }
        return htext;
 }

    public string decode(string str)
    {
        string dtext = ""; 

        for ( int i=0; i < str.Length; i++)
        {
            dtext = dtext + (char) (str[i] - 10 + 1*2);
        }
        return dtext;
    }

复制代码
复制代码
3、方法三 (可逆加密)

复制代码
复制代码

const string KEY_64 = “VavicApp”;//注意了,是8个字符,64位

    const string IV_64 = "VavicApp"; 
    public string Encode(string data)
    {
        byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
        byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);

        DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
        int i = cryptoProvider.KeySize;
        MemoryStream ms = new MemoryStream();
        CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write);

        StreamWriter sw = new StreamWriter(cst);
        sw.Write(data);
        sw.Flush();
        cst.FlushFinalBlock();
        sw.Flush();
        return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);

    }

    public string Decode(string data)
     {
        byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
        byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);

        byte[] byEnc;
        try
        {
            byEnc = Convert.FromBase64String(data);
        }
        catch
        {
            return null;
        }

        DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
        MemoryStream ms = new MemoryStream(byEnc);
        CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Read);
        StreamReader sr = new StreamReader(cst);
        return sr.ReadToEnd();
    }

复制代码
复制代码

4、MD5不可逆加密 (32位加密)

复制代码
复制代码

 public string GetMD5(string s, string _input_charset)
 {

    /**//// <summary>
    /// 与ASP兼容的MD5加密算法
    /// </summary>

    MD5 md5 = new MD5CryptoServiceProvider();
    byte[] t = md5.ComputeHash(Encoding.GetEncoding(_input_charset).GetBytes(s));
    StringBuilder sb = new StringBuilder(32);
    for (int i = 0; i < t.Length; i++)
    {
        sb.Append(t[i].ToString("x").PadLeft(2, '0'));
    }
    return sb.ToString();
}

复制代码
复制代码
(16位加密)

复制代码
复制代码

public static string GetMd5Str(string ConvertString)
{
    MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
    string t2 = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(ConvertString)), 4, 8);
    t2 = t2.Replace("-", "");
    return t2;
} 

复制代码
复制代码

5、加解文本文件

复制代码
复制代码

     //加密文件
private static void EncryptData(String inName, String outName, byte[] desKey, byte[] desIV)
{
    //Create the file streams to handle the input and output files.
    FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);
    FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
    fout.SetLength(0);

    //Create variables to help with read and write.
    byte[] bin = new byte[100]; //This is intermediate storage for the encryption.
    long rdlen = 0;              //This is the total number of bytes written.
    long totlen = fin.Length;    //This is the total length of the input file.
    int len;                     //This is the number of bytes to be written at a time.

    DES des = new DESCryptoServiceProvider();
    CryptoStream encStream = new CryptoStream(fout, des.CreateEncryptor(desKey, desIV), CryptoStreamMode.Write);

    //Read from the input file, then encrypt and write to the output file.
    while (rdlen < totlen)
    {
        len = fin.Read(bin, 0, 100);
        encStream.Write(bin, 0, len);
        rdlen = rdlen + len;
    }

    encStream.Close();
    fout.Close();
    fin.Close();
}

//解密文件
private static void DecryptData(String inName, String outName, byte[] desKey, byte[] desIV)
{
    //Create the file streams to handle the input and output files.
    FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);
    FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
    fout.SetLength(0);

    //Create variables to help with read and write.
    byte[] bin = new byte[100]; //This is intermediate storage for the encryption.
    long rdlen = 0;              //This is the total number of bytes written.
    long totlen = fin.Length;    //This is the total length of the input file.
    int len;                     //This is the number of bytes to be written at a time.

    DES des = new DESCryptoServiceProvider();
    CryptoStream encStream = new CryptoStream(fout, des.CreateDecryptor(desKey, desIV), CryptoStreamMode.Write);

    //Read from the input file, then encrypt and write to the output file.
    while (rdlen < totlen)
    {
        len = fin.Read(bin, 0, 100);
        encStream.Write(bin, 0, len);
        rdlen = rdlen + len;
    }

    encStream.Close();
    fout.Close();
    fin.Close();
}

复制代码
复制代码
6.

复制代码
复制代码

using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace Component
{
public class Security
{
public Security()
{

    }

    //默认密钥向量
    private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
    /**//**//**//// <summary>
    /// DES加密字符串
    /// </summary>
    /// <param name="encryptString">待加密的字符串</param>
    /// <param name="encryptKey">加密密钥,要求为8位</param>
    /// <returns>加密成功返回加密后的字符串,失败返回源串</returns>
    public static string EncryptDES(string encryptString, string encryptKey)
     {
        try
         {
            byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
            byte[] rgbIV = Keys;
            byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
            DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
            MemoryStream mStream = new MemoryStream();
            CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
            cStream.Write(inputByteArray, 0, inputByteArray.Length);
            cStream.FlushFinalBlock();
            return Convert.ToBase64String(mStream.ToArray());
        }
        catch
         {
            return encryptString;
        }
    }

    /**//**//**//// <summary>
    /// DES解密字符串
    /// </summary>
    /// <param name="decryptString">待解密的字符串</param>
    /// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param>
    /// <returns>解密成功返回解密后的字符串,失败返源串</returns>
    public static string DecryptDES(string decryptString, string decryptKey)
     {
        try
         {
            byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);
            byte[] rgbIV = Keys;
            byte[] inputByteArray = Convert.FromBase64String(decryptString);
            DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
            MemoryStream mStream = new MemoryStream();
            CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
            cStream.Write(inputByteArray, 0, inputByteArray.Length);
            cStream.FlushFinalBlock();
            return Encoding.UTF8.GetString(mStream.ToArray());
        }
        catch
         {
            return decryptString;
        }
    } 


}

}

C#常用加密解密方法(AES加密解密) 本节主要分享AES加密解密: AES算法基于排列和置换运算,排列是对数据重新进行安排,置换是将一个数据单元替换为另一个。AES 使用几种不同的方法来执行排列和置换运算。AES是一个迭代的、对称密钥分组的密码,它可以使用128、192 和 256 位密钥,并且用 128 位(16字节)分组加密和解密数据。与公共密钥密码使用密钥对不同,对称密钥密码使用相同的密钥加密和解密数据。通过分组密码返回的加密数据的位数与输入数据相同。迭代加密使用一个循环结构,在该循环中重复置换和替换输入数据。 阅读详情

相关推荐

C#常用加密解密方法(Base64加密解密)

本节主要分享Base64加密解密:Base64是网络上最常见的用于传输8Bit字节码的编码方式之一,Base64就是一种基于64个可打印字符来表示二进制数据的方法。可查看RFC2045~RFC2049,上面有MIME的详细规范。Base64编码是从二进制到字符的过程,可用于在HTTP环境下传递较长的标识信息。采用Base64编码具有不可读性,需要解码后才能阅读。

MarcoPro的博客 1万+

C# 加密解码各种方法

C# 加密解码各种方法

againknow的博客 6032

C#高级编程:加密解密

C# 高级编程中的加密解密技术是保障数据安全的重要手段。通过对称加密、非对称加密和哈希算法的合理运用,以及在各种实际场景中的灵活部署,开发者能够有效保护数据的机密性、完整性和真实性。随着技术的不断发展,新的加密算法和安全标准也在不断涌现,开发者需要持续关注并学习,以应对日益复杂的安全挑战,为用户和企业构建更加安全可靠的应用程序。

hy_4377的博客 1452

C#开发中常用的加密解密方法

相信很多人在开发过程中经常会遇到需要对一些重要的信息进行加密处理,今天给大家分享我个人总结的一些加密算法: 常见的加密方式分为可逆和不可逆两种方式 可逆:RSA,AES,DES等 不可逆:常见的MD5,SHAD等 一、MD5消息摘要算法 我想这是大家都常听过的算法,可能也用的比较多。那么什么是MD5算法呢?MD5全称是message-digest algorit...

weixin_30367945的博客 507

C# Java间进行RSA加密解密交互

这里,讲一下RSA算法加解密在C#和Java之间交互的问题,这两天纠结了很久,也看了很多其他人写的文章,颇受裨益,但没能解决我的实际问题,终于,还是被我捣鼓出来了。 虽然经过数据验证,结果都正确,但是自认为缺少强有力的理论依据。   首先,介绍一下写这代码的目的:完成webService验证问题,服务器端采用C#开发,客户端采用Java开发。服务器端给客户端提供公钥,已进行数据加密,客户端加

dslinmy的专栏 1万+

C#常用加密解密方法(RAS加密解密)

本节主要分享RAS加密解密:RSA公钥加密算法是1977年由Ron Rivest、Adi Shamirh和LenAdleman在(美国麻省理工学院)开发的。RSA取名来自开发他们三者的名字。RSA是目前最有影响力的公钥加密算法,它能够抵抗到目前为止已知的所有密码攻击,已被ISO推荐为公钥数据加密标准。RSA算法基于一个十分简单的数论事实:将两个大素数相乘十分容易,但那时想要对其乘积进行因式分解却极其困难,因此可以将乘积公开作为加密密钥。RSA算法是第一个能同时用于加密和数字签名的算法,也易于理解和操作。

MarcoPro的博客 4295

C# Java 加密解密

C# AES加密解密 1 public static string Encrypt(string key, string clearText) 2 { 3 byte[] clearBytes = Encoding.Unicode.GetBytes(clearText); 4 using...

dianchou9307的博客 302

基于 C# 和 Windows Forms 的文件加密解密应用程序

这是一个基于 C# 和 Windows Forms 的文件加密解密应用程序,该程序具备文件和文件夹的加密解密功能,同时支持日志记录、日志密码管理以及日志查询功能,增强了数据的安全性和可追溯性。

weixin_38819580的博客 2423

C# 加密解密类库深入解析

C# 中的加密解密类库为开发者提供了多种常用的加密算法,包括对称加密(AES、DES)、非对称加密(RSA)和哈希算法(MD5、SHA)。这些加密方式在实际应用中有着广泛的应用场景,如数据加密传输、密码保护、数字签名和数据完整性校验等。理解这些加密算法的基本原理和使用方法,有助于提升系统的安全性,确保敏感信息得到有效保护。在开发时,根据具体需求选择合适的加密算法,并确保密钥的安全存储与管理,以最大限度地提高数据安全性。

2401_84068825的博客 976

C#实现AES加密解密技术详解

高级加密标准(AES)是由美国国家标准与技术研究院(NIST)在2001年正式公布的。它替代了之前的DES加密算法,并迅速成为全球认可的对称密钥加密标准。AES算法以简洁高效、安全性高著称,广泛应用于金融、政府及各种安全敏感领域。WPF应用程序以其丰富的交互性和美观的用户界面而著称。它允许开发者使用XAML(可扩展应用程序标记语言)来描述用户界面的布局和外观,同时使用C#或其他.NET支持的语言来编写业务逻辑。

weixin_35811662的博客 2262

Visual C#中的加密解密技术与实践

本文还有配套的精品资源,点击获取 简介:加密和解密在编程中是保障数据安全的核心技术,特别是在使用C#进行软件开发时。本文将深入探讨Visual C#中的加密解密机制,包括对称加密(如AES、DES、3DES)和非对称加密(如RSA、ECC)算法的实现。通过C#中内置的加密类库,如 System.Security.Cryptography.Aes 和 R...

weixin_31938351的博客 1207

[C#]C#实现RSA加密解密

RSA公钥加密算法是1977年由Ron Rivest、Adi Shamirh和LenAdleman在(美国麻省理工学院)开发的。RSA取名来自开发他们三者的名字。RSA的缺点产生密钥很麻烦,受到素数产生技术的限制,因而难以做到一次一密。分组长度太大,为保证安全性,n 至少也要 600bits以上,使运算代价很高,尤其是速度较慢,较对称密码算法慢几个数量级;且随着大数分解技术的发展,这个长度还在增加,不利于数据格式的标准化。

FL1623863129的博客 6139

C# 10种加密解密方式

1、MD5加密 2、根据CPU硬盘获取机器码 3、加密数据报 4、口令加密BAT、EXE、RAR 5、使用强名称标识软件 6、伪随机数加密 7、文件加密 8、异或加密数字 9、注册码注册机制

C# AES加密解密

public class MainController : Controller { public ActionResult Index() { return View(); } private static string Key { ...

要疯了的博客 4421

C#的几种加密和解密的方法

这是C#的几种加密和解密的方法,里面包含Base64的加密和解密,MD5的加密,SHA1的加密和解密,文件夹的加密和解密

基于机器学习和辐射传输的地表温度模拟方法对比

对于不同的地表类型和不同的年份,经验方法是实用的,虽然整体精度不如物理方法,但具有计算速度快、对模型内部结构考虑较少等优点。尽管深度学习等回归的经验方法不需要考虑辐射传输和能量平衡的具体过程, 但在其输入参数的选择上也不能是无序和随机的,而是应选择有关相应物理过程的参数。经验方法在处理不同年份相同地表类型或表现相对较好, RMSE 约为 1.50K,但在不同地表类型且不同年份模拟结果较差, RMSE 约为 3.0K, 虽然精度不如物理方法,但它具有计算速度快,不需过多考虑模型内部结构的优点。

2301_77907355的博客 1021

C# 下简单的进行加密解密问题

<br />今天用到了C# 下的文本加密解密问题,从网上找了很多素材都不合适 MD5原来是不可逆的加密,所以用了一个相对来说比较简单的加密方法,既可以加密也可以解密 记录一下以后方便大家使用<br /> using System.Security.Cryptography; <br /> //加密 <br /> public static String JiaMi(String str) <br /> { <br /> String encryptKey = "Oyea"; <br /> DESC

pridescc的专栏 966

RU.EXE实用指南:从基础操作到高级调试技巧

本文是RU.EXE(RW Everything)的实用指南,详细介绍了这款硬件调试工具从基础操作到高级技巧的全过程。文章阐述了RU.EXE作为直接读写硬件寄存器、内存和PCI配置空间的强大工具,如何帮助硬件工程师和开发人员高效进行底层系统调试与故障排查,并强调了安全操作的重要性。

weixin_29228203的博客 278

C# 256位AES加密解密

AES加密解密 直接上代码 /// <summary> /// 256位AES加密 /// </summary> /// <param name="toEncrypt"></param> /// <returns></returns> pub...

weixin_43035482的博客 1077

17 指挥AI写Mamba相关模型代码,快速适配大模型场景

本文为《30天掌控AI编程:从指令到落地,手把手教你指挥AI写代码》系列第十七篇,属于第三阶段多场景实战核心内容。本篇聚焦当下大模型领域热门的**Mamba架构**,针对零基础大模型开发、无深度学习基础的使用者,拆解指挥AI生成Mamba基础模型、微调适配、推理部署代码的核心方法,覆盖Mamba基础模块搭建、简易序列建模、小样本微调、本地推理等高频场景,提供可直接复用的高质量指令模板、完整可运行代码、环境配置与避坑指南,无需精通Transformer、状态空间模型等底层原理,轻松上手Mamba大模型开发。

coreopt的博客 61

原理之(〇)3D Gaussian Splatting 数学原理与推导

3D Gaussian Splatting源于EWA的理论基础,本文重点从EWA模型出发,详细推导3D GS中的数学原理,加深对鱼眼相机畸变场景下的理解。

scott198512的博客 4157
上一篇: c#自定义控件(2)-修改属性
下一篇: WPF系列(1)----简单窗体
奇点码农
博客等级 码龄19年 60粉丝 140原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值