RC2加密算法在C#的应用

2024-02-17 10:58

本文主要是介绍RC2加密算法在C#的应用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 

using System;
using System.Security.Cryptography;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace Curllion
{
 public class Crypt
 {
  /// <summary>
  /// 新建一个大小为10261B的文件,以便将加密数据写入固定大小的文件。
  /// </summary>
  /// <param name="filePath">文件保存的地址,包含文件名</param>
  public static void InitBinFile(string filePath)
  {
   byte[] tmp = new byte[10261];
   try  //创建文件流,将其内容全部写入0
   {
    System.IO.FileStream writeFileStream = new FileStream(filePath,
     System.IO.FileMode.Create,
     System.IO.FileAccess.Write,
     System.IO.FileShare.None,512,false);

    for(int i = 0 ;i< 10261;i++)
     tmp[i] = 0; 
    writeFileStream.Write(tmp,0,10261);
    writeFileStream.Flush();
    writeFileStream.Close();
   }
   catch(System.IO.IOException)
   {
    MessageBox.Show("文件操作错误!","错误!",MessageBoxButtons.OK,MessageBoxIcon.Error);
   }
  }
  /// <summary>
  /// 将文本数据加密后写入一个文件,其中,这个文件是用InitBinFile建立的,这个文件将被分成十块,
  /// 用来分别保存10组不同的数据,第一个byte位保留,第2位到第21位分别用来存放每块数据的长度,但
  /// 一个byte的取值为0-127,所以,用两个byte来存放一个长度。
  /// </summary>
  /// <param name="toEncryptText">要加密的文本</param>
  /// <param name="filePath">要写入的文件</param>
  /// <param name="dataIndex">写入第几块,取值为1--10</param>
  /// <returns>是否操作成功</returns>
  public static bool EncryptToFile(string toEncryptText,string filePath,int dataIndex)
  {
   bool r = false;
   if(dataIndex > 10 && dataIndex < 1)
   {
    MessageBox.Show("数据索引的取值范围在1至10之间!","错误!",
     MessageBoxButtons.OK,MessageBoxIcon.Error);
    return r;
   }
   byte[] encrypted;  
   //初始化向量
   byte[] key = {106,51,25,141,157,142,23,111,234,159,187,154,215,34,37,204};   
   //密匙
   byte[] IV = {135,186,133,136,184,149,153,144};
   //创建UTF-16 编码,用来在byte[]和string之间转换
   System.Text.UnicodeEncoding textConverter = new UnicodeEncoding();
   //创建RC2服务
      RC2CryptoServiceProvider rc2CSP = new RC2CryptoServiceProvider();
   //打开要写入的文件,主要是为了保持原文件的内容不丢失
   System.IO.FileStream tmpFileStream= new FileStream(filePath,
    System.IO.FileMode.Open,
    System.IO.FileAccess.Read,
    System.IO.FileShare.None,1024,true);

   byte[] index = new byte[10261];
   //将读取的内容写到byte数组
   tmpFileStream.Read(index,0,10261);
   tmpFileStream.Close();
   //定义基本的加密转换运算
   System.Security.Cryptography.ICryptoTransform Encryptor = rc2CSP.CreateEncryptor(key,IV);
   System.IO.MemoryStream msEncrypt = new MemoryStream();
   //在此加密转换流中,加密将从csEncrypt,加密后,结果在msEncrypt流中。
   System.Security.Cryptography.CryptoStream csEncrypt = new CryptoStream(msEncrypt,
    Encryptor,CryptoStreamMode.Write);
   //将要加密的文本转换成UTF-16 编码,保存在tmp数组。
   byte[] tmp = textConverter.GetBytes(toEncryptText);
   //将tmp输入csEncrypt,将通过Encryptor来加密。
   csEncrypt.Write(tmp,0,tmp.Length);
   //输出到msEnctypt
   csEncrypt.FlushFinalBlock();
   //将流转成byte[]
   encrypted = msEncrypt.ToArray();
   if(encrypted.Length>1024)
   {
    MessageBox.Show("加密后,数据长度大于1KB,无法保存");
    return false;
   }
   //得到加密后数据的大小,将结果存在指定的位置。
   index[dataIndex*2 - 1] = Convert.ToByte(Convert.ToString(encrypted.Length/128));
   index[dataIndex*2]     = Convert.ToByte(Convert.ToString(encrypted.Length%128));
   //将加密后的结果写入index(覆盖)
   for(int i=0;i<encrypted.Length;i++)
    index[1024*(dataIndex-1)+21+i]=encrypted[i];
   //建立文件流
   tmpFileStream = new FileStream(filePath,
    System.IO.FileMode.Truncate,
    System.IO.FileAccess.Write,
    System.IO.FileShare.None,1024,true);
   //写文件
   tmpFileStream.Write(index,0,10261);
   tmpFileStream.Flush();
   r = true;
   tmpFileStream.Close();
   return r;
  }
  /// <summary>
  /// 从一个文件中解密出一段文本,其中,这个文件是由InitBinFile建立的,并且由 EncryptToFile加密的
  /// </summary>
  /// <param name="filePath">要解密的文件</param>
  /// <param name="dataIndex">要从哪一个块中解密</param>
  /// <returns>解密后的文本</returns>
  public static string DecryptFromFile(string filePath,int dataIndex)
  {
   string r = "";
   if(dataIndex > 10 && dataIndex < 1)
   {
    MessageBox.Show("数据索引的取值范围在1至10之间!","错误!",
     MessageBoxButtons.OK,MessageBoxIcon.Error);
    return r;
   }
   byte[] decrypted;
   byte[] key = {106,51,25,141,157,142,23,111,234,159,187,154,215,34,37,204};
   byte[] IV = {135,186,133,136,184,149,153,144};
   System.Text.UnicodeEncoding textConverter = new UnicodeEncoding();
   RC2CryptoServiceProvider rc2CSP = new RC2CryptoServiceProvider();
   System.IO.FileStream tmpFileStream = new FileStream(filePath,
    System.IO.FileMode.Open,
    System.IO.FileAccess.Read,
    System.IO.FileShare.None,1024,true);

   System.Security.Cryptography.ICryptoTransform Decryptor = rc2CSP.CreateDecryptor(key,IV);
   System.IO.MemoryStream msDecrypt = new MemoryStream();
   System.Security.Cryptography.CryptoStream csDecrypt = new CryptoStream(msDecrypt,
    Decryptor,CryptoStreamMode.Write);
   byte[] index = new byte[10261];

   tmpFileStream.Read(index,0,10261);
   int startIndex = 1024*(dataIndex-1)+21;
   int count      = index[dataIndex*2 - 1]*128 + index[dataIndex*2];
   byte[] tmp     = new byte[count];

   Array.Copy(index,1024*(dataIndex-1)+21,tmp,0,count);
   csDecrypt.Write(tmp,0,count);
   csDecrypt.FlushFinalBlock();
   decrypted = msDecrypt.ToArray();
   r = textConverter.GetString(decrypted,0,decrypted.Length);
   tmpFileStream.Close();
   return r;
  }
  /// <summary>
  /// 将一段文本加密后保存到一个文件
  /// </summary>
  /// <param name="toEncryptText">要加密的文本数据</param>
  /// <param name="filePath">要保存的文件</param>
  /// <returns>是否加密成功</returns>
  public static bool EncryptToFile(string toEncryptText,string filePath)
  {
   bool r = false;
   byte[] encrypted;
   byte[] key = {106,51,25,141,157,142,23,111,234,159,187,154,215,34,37,204};
   byte[] IV = {135,186,133,136,184,149,153,144};
   System.Text.UnicodeEncoding textConverter = new UnicodeEncoding();
   RC2CryptoServiceProvider rc2CSP = new RC2CryptoServiceProvider();
   System.IO.FileStream tmpFileStream = new FileStream(filePath,
    System.IO.FileMode.OpenOrCreate,
    System.IO.FileAccess.Write,
    System.IO.FileShare.None,1024,true);

   System.Security.Cryptography.ICryptoTransform Encryptor = rc2CSP.CreateEncryptor(key,IV);
   System.IO.MemoryStream msEncrypt = new MemoryStream();
   System.Security.Cryptography.CryptoStream csEncrypt = new CryptoStream(msEncrypt,
    Encryptor,CryptoStreamMode.Write);

   byte[] tmp = textConverter.GetBytes(toEncryptText);
   csEncrypt.Write(tmp,0,tmp.Length);
   csEncrypt.FlushFinalBlock();   
   encrypted = msEncrypt.ToArray();
   tmpFileStream.Write(encrypted,0,encrypted.Length);
   tmpFileStream.Flush();
   r = true;
   tmpFileStream.Close();
   return r;
  }
  /// <summary>
  /// 将一个被加密的文件解密
  /// </summary>
  /// <param name="filePath">要解密的文件</param>
  /// <returns>解密后的文本</returns>
  public static string DecryptFromFile(string filePath)
  {
   string r = "";
   byte[] decrypted;
   byte[] key = {106,51,25,141,157,142,23,111,234,159,187,154,215,34,37,204};
   byte[] IV = {135,186,133,136,184,149,153,144};
   System.Text.UnicodeEncoding textConverter = new UnicodeEncoding();
   RC2CryptoServiceProvider rc2CSP = new RC2CryptoServiceProvider();
   System.IO.FileStream tmpFileStream = new FileStream(filePath,
    System.IO.FileMode.Open,
    System.IO.FileAccess.Read,
    System.IO.FileShare.None,1024,true);
   System.Security.Cryptography.ICryptoTransform Decryptor = rc2CSP.CreateDecryptor(key,IV);
   System.IO.MemoryStream msDecrypt = new MemoryStream();
   System.Security.Cryptography.CryptoStream csDecrypt = new CryptoStream(msDecrypt,
    Decryptor,CryptoStreamMode.Write);

   byte[] tmp = new byte[tmpFileStream.Length];
   tmpFileStream.Read(tmp,0,tmp.Length);
   csDecrypt.Write(tmp,0,tmp.Length);
   csDecrypt.FlushFinalBlock();
   decrypted = msDecrypt.ToArray();    
   r = textConverter.GetString(decrypted,0,decrypted.Length);
   tmpFileStream.Close();
   return r;
  }
 }
}

这篇关于RC2加密算法在C#的应用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/717561

相关文章

Python结合PyWebView库打造跨平台桌面应用

《Python结合PyWebView库打造跨平台桌面应用》随着Web技术的发展,将HTML/CSS/JavaScript与Python结合构建桌面应用成为可能,本文将系统讲解如何使用PyWebView... 目录一、技术原理与优势分析1.1 架构原理1.2 核心优势二、开发环境搭建2.1 安装依赖2.2 验

C#实现将Excel表格转换为图片(JPG/ PNG)

《C#实现将Excel表格转换为图片(JPG/PNG)》Excel表格可能会因为不同设备或字体缺失等问题,导致格式错乱或数据显示异常,转换为图片后,能确保数据的排版等保持一致,下面我们看看如何使用C... 目录通过C# 转换Excel工作表到图片通过C# 转换指定单元格区域到图片知识扩展C# 将 Excel

Java字符串操作技巧之语法、示例与应用场景分析

《Java字符串操作技巧之语法、示例与应用场景分析》在Java算法题和日常开发中,字符串处理是必备的核心技能,本文全面梳理Java中字符串的常用操作语法,结合代码示例、应用场景和避坑指南,可快速掌握字... 目录引言1. 基础操作1.1 创建字符串1.2 获取长度1.3 访问字符2. 字符串处理2.1 子字

C#中async await异步关键字用法和异步的底层原理全解析

《C#中asyncawait异步关键字用法和异步的底层原理全解析》:本文主要介绍C#中asyncawait异步关键字用法和异步的底层原理全解析,本文给大家介绍的非常详细,对大家的学习或工作具有一... 目录C#异步编程一、异步编程基础二、异步方法的工作原理三、代码示例四、编译后的底层实现五、总结C#异步编程

C#TextBox设置提示文本方式(SetHintText)

《C#TextBox设置提示文本方式(SetHintText)》:本文主要介绍C#TextBox设置提示文本方式(SetHintText),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑... 目录C#TextBox设置提示文本效果展示核心代码总结C#TextBox设置提示文本效果展示核心代

SpringShell命令行之交互式Shell应用开发方式

《SpringShell命令行之交互式Shell应用开发方式》本文将深入探讨SpringShell的核心特性、实现方式及应用场景,帮助开发者掌握这一强大工具,具有很好的参考价值,希望对大家有所帮助,如... 目录引言一、Spring Shell概述二、创建命令类三、命令参数处理四、命令分组与帮助系统五、自定

SpringBoot应用中出现的Full GC问题的场景与解决

《SpringBoot应用中出现的FullGC问题的场景与解决》这篇文章主要为大家详细介绍了SpringBoot应用中出现的FullGC问题的场景与解决方法,文中的示例代码讲解详细,感兴趣的小伙伴可... 目录Full GC的原理与触发条件原理触发条件对Spring Boot应用的影响示例代码优化建议结论F

C#中DrawCurve的用法小结

《C#中DrawCurve的用法小结》本文主要介绍了C#中DrawCurve的用法小结,通常用于绘制一条平滑的曲线通过一系列给定的点,具有一定的参考价值,感兴趣的可以了解一下... 目录1. 如何使用 DrawCurve 方法(不带弯曲程度)2. 如何使用 DrawCurve 方法(带弯曲程度)3.使用Dr

MySQL 分区与分库分表策略应用小结

《MySQL分区与分库分表策略应用小结》在大数据量、复杂查询和高并发的应用场景下,单一数据库往往难以满足性能和扩展性的要求,本文将详细介绍这两种策略的基本概念、实现方法及优缺点,并通过实际案例展示如... 目录mysql 分区与分库分表策略1. 数据库水平拆分的背景2. MySQL 分区策略2.1 分区概念

Spring Shell 命令行实现交互式Shell应用开发

《SpringShell命令行实现交互式Shell应用开发》本文主要介绍了SpringShell命令行实现交互式Shell应用开发,能够帮助开发者快速构建功能丰富的命令行应用程序,具有一定的参考价... 目录引言一、Spring Shell概述二、创建命令类三、命令参数处理四、命令分组与帮助系统五、自定义S