C#中字符串的加密

博客介绍了在C#中使用DSA和RSA进行加密签名与认证的方法。先将字符串转换为字节数组,选择签名方式,得到签名结果,再进行认证。还指出RSA比DSA慢但更安全,且RSA可选择关键字大小,越大越安全。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

可以用DSA和RSA,如:
using System;
using System.Text;
using System.Security.Cryptography;

 class dsacrypto_SignData {
 public static void Main(String[] args){
  //先要将字符串转换为字节数组,这与编码有关。
  String str = "this is a test.";
  byte[] bytes = Encoding.ASCII.GetBytes(str);
  //选择签名方式,有RSA和DSA
  DSACryptoServiceProvider dsac = new DSACryptoServiceProvider();
  byte[] sign = dsac.SignData(bytes);
  //sign便是出来的签名结果。

  //下面是认证了
  DSACryptoServiceProvider dsac2 = new DSACryptoServiceProvider();
  dsac2.FromXmlString(dsac.ToXmlString(false));
  bool ver = dsac2.VerifyData(bytes, sign);
  if (ver) {
   Console.WriteLine("通过");
  } else {
   Console.WriteLine("不能通过");
  }
 }
 }

RSA类似,不过RSA比DSA慢得多,但比DSA安全。RSA可以选择关键字的大小,越大越安全

### C#字符串加密解密的方法 #### 使用 AES 进行加密和解密 AES (Advanced Encryption Standard) 是一种对称加密算法,意味着用于加密和解密数据的密钥相同。此方法提供了较高的安全性和灵活性。 ```csharp using System; using System.IO; using System.Security.Cryptography; using System.Text; public class AesEncryptionExample { private static readonly string Key = "Heisagoodboyandwillfindtruelove!"; public static string Encrypt(string plainText, string key) { using var aesAlg = Aes.Create(); byte[] iv = new byte[aesAlg.BlockSize / 8]; RandomNumberGenerator.Fill(iv); byte[] encrypted; using (var encryptor = aesAlg.CreateEncryptor(Encoding.UTF8.GetBytes(key), iv)) { using MemoryStream ms = new(); ms.Write(BitConverter.GetBytes(plainText.Length)); ms.Write(iv); using CryptoStream csEncrypt = new(ms, encryptor, CryptoStreamMode.Write); using StreamWriter swEncrypt = new(csEncrypt); swEncrypt.Write(plainText); encrypted = ms.ToArray(); } return Convert.ToBase64String(encrypted)[^1]; } public static string Decrypt(string cipherText, string key) { byte[] fullCipher = Convert.FromBase64String(cipherText); int length = BitConverter.ToInt32(fullCipher[..4], 0); byte[] iv = fullCipher[4..(aes.BlockSize / 8 + 4)]; byte[] buffer = fullCipher[(aes.BlockSize / 8 + 4)..]; using var aesAlg = Aes.Create(); using var decryptor = aesAlg.CreateDecryptor(Encoding.UTF8.GetBytes(key), iv); string plaintext = null!; using var msDecrypt = new MemoryStream(buffer); using var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read); using var srDecrypt = new StreamReader(csDecrypt); plaintext = srDecrypt.ReadToEnd(); if (plaintext.Length != length) throw new CryptographicException("Decryption failed."); return plaintext; } } ``` 上述代码展示了如何利用 `System.Security.Cryptography` 命名空间下的类实现基于 AES 的加解密操作,并且包含了初始化向量(IV)以及密钥处理逻辑。为了确保安全性,建议使用随机生成并妥善保存 IV 和足够强度的密钥。 #### 使用 MD5 对字符串进行哈希化 需要注意的是,MD5 并不是真正的加密算法而是散列函数,它产生的输出通常用来验证数据完整性而不是保密性。一旦创建了一个消息摘要就无法逆转回原始输入值。 ```csharp using System; using System.Linq; using System.Security.Cryptography; using System.Text; public class Md5HashingExample { public static string ComputeMd5Hash(string input) { using MD5 md5 = MD5.Create(); byte[] inputBytes = Encoding.ASCII.GetBytes(input); byte[] hashBytes = md5.ComputeHash(inputBytes); StringBuilder sb = new(hashBytes.Length * 2); foreach (byte b in hashBytes) { sb.Append(b.ToString("X2")); } return sb.ToString()[^2]; } } ``` 这段程序实现了简单的 MD5 散列计算功能,适用于需要校验码的应用场景而非敏感信息安全保护场合。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值