欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c# 几种常见的加密方法的实现

程序员文章站 2022-06-24 16:41:53
1.acsii码加密//acsii码加密 private static string acsiipwd(string rpwd) { string ret; byte[...

1.acsii码加密

//acsii码加密
    private static string acsiipwd(string rpwd)
    {
      string ret;
      byte[] array = system.text.encoding.ascii.getbytes(rpwd);
      byte[] bytearray = new byte[array.length];

      for (int i = 0; i < array.length; i++)
      {
        int asciicode = (int)(array[i]);
        asciicode = asciicode + 1;
        bytearray[i] = (byte)asciicode;
      }
      system.text.asciiencoding asciiencoding = new system.text.asciiencoding();
      string strcharacter = asciiencoding.getstring(bytearray);
      ret = strcharacter;
      return ret;
    }

2.md5加密

//md5加密
    private static string md5pwd(string rpwd)
    {
      string ret;

      md5 md5 = new md5cryptoserviceprovider();
      byte[] palindata = encoding.default.getbytes(rpwd);//将要加密的字符串转换为字节数组
      byte[] encryptdata = md5.computehash(palindata);//将字符串加密后也转换为字符数组
      ret = convert.tobase64string(encryptdata);
      return ret;
    }

3.rsa加密

//rsa加密
    private static string rsapwd1(string mykeycontainername)
    {
      string ret = "";     
      cspparameters cp = new cspparameters();
      cp.keycontainername = mykeycontainername;
      rsacryptoserviceprovider rsa = new rsacryptoserviceprovider(cp);
      
      ret = rsa.toxmlstring(true);
      console.writeline("key is : \n" + rsa.toxmlstring(true));
      return ret;

    }

4.des加密

//des加密 
    private static string despwd(string ciphertext)
    {
      string despwd = ""; 
      byte[] buffer;
      descryptoserviceprovider descsp = new descryptoserviceprovider();

      memorystream ms = new memorystream();//先创建 一个内存流
      cryptostream crystream = new cryptostream(ms, descsp.createencryptor(), cryptostreammode.write);//将内存流连接到加密转换流
      streamwriter sw = new streamwriter(crystream);
      sw.writeline(ciphertext);//将要加密的字符串写入加密转换流
      sw.close();
      crystream.close();
      buffer = ms.toarray();//将加密后的流转换为字节数组
      despwd = convert.tobase64string(buffer);//将加密后的字节数组转换为字符串

      return despwd;
    }

以上就是c# 几种常见的加密方法的实现的详细内容,更多关于c# 加密的资料请关注其它相关文章!

相关标签: c# 加密