在 c# 中加密和解密一个字符串-ag捕鱼王app官网

在 c# 中加密和解密一个字符串

作者:迹忆客 最近更新:2024/01/03 浏览次数:

本教程将讨论使用 c# 加密和解密字符串的方法。


使用 c# 中的 aesmanaged 类加密字符串

加密是将数据转换为密文的过程,因此任何未经授权的个人都无法访问数据。在本教程中,我们将使用高级加密标准(aes)算法对 c# 中的字符串进行加密和解密。aesmanaged 类提供了使用 aes 算法对字符串进行加密和解密的方法。createencryptor() 函数可以使用密钥加密数据。我们必须将字符串键传递给 createencryptor() 函数。以下代码示例向我们展示了如何使用 c# 中的 createencryptor() 函数根据 aes 算法对字符串进行加密。

using system;
using system.io;
using system.security.cryptography;
using system.text;
namespace encrypt_decrypt_string {
  class program {
    static string encrypt() {
      try {
        string texttoencrypt = "waterworld";
        string toreturn = "";
        string publickey = "12345678";
        string secretkey = "87654321";
        byte[] secretkeybyte = {};
        secretkeybyte = system.text.encoding.utf8.getbytes(secretkey);
        byte[] publickeybyte = {};
        publickeybyte = system.text.encoding.utf8.getbytes(publickey);
        memorystream ms = null;
        cryptostream cs = null;
        byte[] inputbytearray = system.text.encoding.utf8.getbytes(texttoencrypt);
        using (descryptoserviceprovider des = new descryptoserviceprovider()) {
          ms = new memorystream();
          cs = new cryptostream(ms, des.createencryptor(publickeybyte, secretkeybyte),
                                cryptostreammode.write);
          cs.write(inputbytearray, 0, inputbytearray.length);
          cs.flushfinalblock();
          toreturn = convert.tobase64string(ms.toarray());
        }
        return toreturn;
      } catch (exception ex) {
        throw new exception(ex.message, ex.innerexception);
      }
    }
    static void main(string[] args) {
      string encrypted = encrypt();
      console.writeline(encrypted);
    }
  }
}

输出:

6 pxxvwlbqcunidqsmyuha==

在上面的代码中,我们以 publickeysecretkey 作为密钥对字符串 waterworld 进行了加密,并以加密字符串的形式返回了 6 pxxvwlbqcunidqsmyuha==。两个密钥的长度必须至少为 8 个字符。


使用 c# 中的 aesmanaged 类解密字符串

解密是将密文转换回原始数据,以便授权个人可以访问数据的过程。createdecryptor() 函数可以使用密钥解密数据。我们必须将字符串键传递给 createencryptor() 函数。密钥必须与 createdecryptor() 函数中使用的密钥相同。以下代码示例向我们展示了如何使用 c# 中的 createdecryptor() 函数根据 aes 算法对字符串进行加密。

using system;
using system.io;
using system.security.cryptography;
using system.text;
namespace encrypt_decrypt_string {
  class program {
    static string decrypt() {
      try {
        string texttodecrypt = "6 pxxvwlbqcunidqsmyuha==";
        string toreturn = "";
        string publickey = "12345678";
        string secretkey = "87654321";
        byte[] privatekeybyte = {};
        privatekeybyte = system.text.encoding.utf8.getbytes(secretkey);
        byte[] publickeybyte = {};
        publickeybyte = system.text.encoding.utf8.getbytes(publickey);
        memorystream ms = null;
        cryptostream cs = null;
        byte[] inputbytearray = new byte[texttodecrypt.replace(" ", " ").length];
        inputbytearray = convert.frombase64string(texttodecrypt.replace(" ", " "));
        using (descryptoserviceprovider des = new descryptoserviceprovider()) {
          ms = new memorystream();
          cs = new cryptostream(ms, des.createdecryptor(publickeybyte, privatekeybyte),
                                cryptostreammode.write);
          cs.write(inputbytearray, 0, inputbytearray.length);
          cs.flushfinalblock();
          encoding encoding = encoding.utf8;
          toreturn = encoding.getstring(ms.toarray());
        }
        return toreturn;
      } catch (exception ae) {
        throw new exception(ae.message, ae.innerexception);
      }
    }
    static void main(string[] args) {
      string decrypted = decrypt();
      console.writeline(decrypted);
    }
  }
}

输出:

waterworld

在上面的代码中,我们将上一示例中的加密字符串 6 pxxvwlbqcunidqsmyuha== 转换回其原始形式 waterworld,并以 publickeysecretkey 作为密钥。该键必须与上一个示例中使用的键相同,此方法才能起作用。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

发布时间:2024/03/16 浏览次数:198 分类:编程语言

在 c# 中,有两种主要方法可用于将 list转换为字符串变量,linq 方法和 string.join()函数。

发布时间:2024/03/16 浏览次数:171 分类:编程语言

在 c# 中,有两种主要方法可用于将 list转换为字符串变量,linq 方法和 string.join()函数。

发布时间:2024/03/16 浏览次数:187 分类:编程语言

在 c# 中,有两种主要方法可用于将 list转换为字符串变量,linq 方法和 string.join()函数。

在 c# 中发出 http post web 请求

发布时间:2024/02/04 浏览次数:131 分类:编程语言

在 c# 中,可以使用 3 种主要方法来发出 http post web 请求:webclient 类,httpwebrequest 类和 httpclient 类。本教程将讨论在 c# 中发出 http post web 请求的方法。使用 c# 中的 webclient 类发出 http post web 请求

发布时间:2024/02/04 浏览次数:130 分类:编程语言

process 类可用于在 c# 中运行命令提示符命令。在 c# 中使用 process.start() 函数运行命令提示符命令

发布时间:2024/02/04 浏览次数:203 分类:编程语言

有两种主要方法可用于在 c# 中调整图像的大小,bitmap 类构造函数和 graphics.drawimage()函数。在本教程中,我们将讨论在c#中调整图像大小的方法。我们将带您完成整个过程,从加载原始图像到保

发布时间:2024/02/04 浏览次数:138 分类:编程语言

有 3 种主要方法可用于下载 c# 中的图片,webclient.downloadfile()函数,bitmap 类和 image.fromstream()函数。在 c# 中使用 webclient 类下载图片 webclient 类提供了用于向 c# 中的 url 发送数据和从 url 接收数据

发布时间:2024/02/04 浏览次数:139 分类:编程语言

我们可以使用 stopwatch 类来计算 c# 中的经过时间。使用 c# 中的秒表类计算经过时间 stopwatch 类在 c# 中准确测量经过的时间。

发布时间:2024/02/04 浏览次数:200 分类:编程语言

有 3 种主要方法可用于获取 c# 中程序的可执行路径,即 assembly 类,appdomain 类和 path 类。本教程将介绍获取 c# 代码的可执行路径的方法。使用 c# 中的 assembly 类获取可执行路径

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便
网站地图