• 凯撒加密/解密算法(C#) Mr


    /***************加密***************/
    public void Encryption()
    {
      FileStream fs1 = new FileStream(加密源文件的存放路径, FileMode.Open, FileAccess.Read);
      FileStream fs2 = new FileStream(加密后文件的存放路径, FileMode.Create);
      BinaryReader br1 = new BinaryReader(fs1);
      BinaryWriter br2 = new BinaryWriter(fs2);
      byte[] bys1 = br1.ReadBytes((int)fs1.Length);
      byte[] bys2 = new byte[(int)(bys1.Length) * 2];

      //加法加密
      if ()
      {

        for (int i = 0, k = 0, j = 1; i < bys1.Length; i++, k = k + 2, j = j + 2)
        {
          bys2[k] = (byte)((Convert.ToInt16(bys1[i]) + Convert.ToInt16(参数(密钥))) % 128 + 128);
          bys2[j] = (byte)((Convert.ToInt16(bys1[i]) + Convert.ToInt16(参数(密钥))) / 128);
        }
        br2.Write(bys2);
      }
      //乘法加密
      else
      {
        for (int i = 0, k = 0, j = 1; i < bys1.Length; i++, k = k + 2, j = j + 2)
        {
          bys2[k] = (byte)((Convert.ToInt16(bys1[i]) * Convert.ToInt16(参数(密钥))) % 128 + 128);
          bys2[j] = (byte)((Convert.ToInt16(bys1[i]) * Convert.ToInt16(参数(密钥))) / 128);
        }
        br2.Write(bys2);
      }
      br2.Close();
      br1.Close();
    }

    /***************解密***************/
    public void Decryption()
    {
      FileStream fs1 = new FileStream(解密源文件的存放路径, FileMode.Open, FileAccess.Read);
      FileStream fs2 = new FileStream(解密后文件的存放路径, FileMode.Create);
      BinaryReader br1 = new BinaryReader(fs1);
      BinaryWriter br2 = new BinaryWriter(fs2);
      byte[] bys1 = br1.ReadBytes((int)fs1.Length);
      byte[] bys2 = new byte[(int)(bys1.Length) / 2];
               
      //加法解密
      if ()
      {
        for (int i = 0, j = 1, k = 0; i < bys1.Length; i = i + 2, j = j + 2, k++)
        {
          bys2[k] = (byte)(((Convert.ToInt16(bys1[i]) - 128) + (Convert.ToInt16(bys1[j]) * 128)) - Convert.ToInt16(参数(密钥)));
        }
        br2.Write(bys2);
      }
      //乘法解密
      else
      {
        for (int i = 0,j=1,k=0; i < bys1.Length; i=i+2,j=j+2,k++)
        {
          bys2[k] = (byte)(((Convert.ToInt16(bys1[i]) - 128) + (Convert.ToInt16(bys1[j]) * 128)) / Convert.ToInt16(参数(密钥)));
        }
        br2.Write(bys2);
      }
      br2.Close();
      br1.Close();
    }

  • 相关阅读:
    总结:python
    Create form:class CreateWindow(Form)
    create sheets: ViewSheet.Create(doc, titleblock.Id)
    create a wall:Wall.Create(doc, line, baseLevel.Id, False)
    creat floor
    excel导入
    Vim正则通配符使用心得
    SVN仓库迁移到Git的完美解决办法
    SVN仓库迁移到Git遇到的两个问题和解决办法
    PGI Compiler for OpenACC Output Syntax Highlighting
  • 原文地址:https://www.cnblogs.com/miaohw/p/2147354.html
Copyright © 2020-2023  润新知