首页 > 编程语言 >C#实现简单的异或加密,方便处理

C#实现简单的异或加密,方便处理

时间:2022-12-18 20:44:35浏览次数:37  
标签:Console string C# 异或 file using var 加密

将本地的mp4和ts文件加密为“dj”文件,无法播放。解密则是将“dj”文件解密为mp4或ts文件。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("请输入操作方式(1加密、0解密):");
            var key = Console.ReadLine();
            if (key == "1")
            {
                Encrypt();
            }
            else
            {
                Dencrypt();
            }
            Console.ReadKey();
        }

        /// <summary>
        /// 加密处理
        /// </summary>
        static void Encrypt()
        {
            string path = AppDomain.CurrentDomain.BaseDirectory;
            string[] extends = new string[] { "*.ts", "*.mp4" };
            foreach (var ex in extends)
            {
                foreach (var file in Directory.GetFiles(path, ex))
                {
                    Encrypt(file, file + ".dj");
                    Console.WriteLine($"[{file}]加密成功");
                    File.Delete(file);
                }
            }
            Console.WriteLine("所有文件加密成功");
        }

        static void Dencrypt()
        {
            string path = AppDomain.CurrentDomain.BaseDirectory;
            foreach (var file in Directory.GetFiles(path, "*.dj"))
            {
                Encrypt(file, file.Replace(".dj", ""));
                Console.WriteLine($"[{file}]解密成功");
                File.Delete(file);
            }
            Console.WriteLine("所有文件解密成功");
        }

        static void Encrypt(string sourceFileName, string targetFileName)
        {
            using (var writeStream = File.OpenWrite(targetFileName))
            {
                //int start = 0;
                int len = 0;
                byte[] readBytes = new byte[1024];
                using (var readStream = File.OpenRead(sourceFileName))
                {
                    var totalLenth = readStream.Length;
                    while ((len = readStream.Read(readBytes, 0, readBytes.Length)) > 0)
                    {
                        writeStream.Write(Encry(readBytes), 0, len);
                        Console.WriteLine($"[{sourceFileName}]读取中[{readStream.Position}/{totalLenth}]");
                    }
                }
            }
        }

        static byte[] Encry(byte[] bs)
        {
            for (int i = 0; i < bs.Length; i++)
            {
                bs[i] = (byte)(bs[i] ^ 0x12);
            }
            return bs;
        }
    }
}

 

标签:Console,string,C#,异或,file,using,var,加密
From: https://www.cnblogs.com/duanjt/p/16990892.html

相关文章

  • DNS用的是TCP协议还是UDP协议
    DNS占用53号端口,同时使用TCP和UDP协议。那么DNS在什么情况下使用这两种协议?DNS在区域传输的时候使用TCP协议,其他时候使用UDP协议。(一)TCP与UDP简介TCP---传输控制协议,是......
  • (摘抄)Defining Application Servers in IntelliJ IDEA
     ThisfeatureissupportedintheUltimateeditiononly.TodefineaserverinIntelliJIDEA,inmostofthecases,allyouhavetodoistospecifywherethec......
  • (转)logback 常用配置详解(二) <appender>
     logback常用配置详解(二)标签: ​​thread​​​​file​​​​活动​​​​date​​​​logging​​​​正则表达式​​2011-09-2017:21 21632人阅读 ......
  • (转)expect学习笔记及实例详解
    expect学习笔记及实例详解1.expect是基于tcl演变而来的,所以很多语法和tcl类似,基本的语法如下所示:1.1首行加上/usr/bin/expect1.2spawn:后......
  • (转)jmeter 测试webservice
    地址:http://itindex.net/detail/45270-jmeter-webservice-%E6%B5%8B%E8%AF%95 1. BuildingaWebServiceTestPlan参考​​http://jmeter.apache.org/usermanual/build-w......
  • Springmvc构造RESTful详细讲解
    ​​Springmvc构造RESTful详细讲解​​Rest介绍 /blog/1HTTPGET=>得到id=1的blog/blog/1HTTPDELETE......
  • m基于simulink的WCDMA通信链路仿真
    1.算法概述W-CDMA由ETSINTTDoCoMo作为无线介面为他们的3G网路FOMA开发。后来NTTDocomo提交给ITU一个详细规范作为一个象IMT-2000一样作为一个候选的国际3G标准。国际电信......
  • C++ Web(HTTP)开发 10 大利器
    众所周知,C++并不是一种流行的Web开发语言,究其原因有很多:语言门槛高、使用难度大、开发效率低......话虽如此,​​但随着Emscripten的成熟,未来C++在Web方面会发挥......
  • Docker部署OpenWRT-旁路由
    1、确认网卡名称命令ipaddr显示如下图的enp2s0就是我们准备进行链接的网卡名称。2、打开网卡的混杂模式sudoiplinksetenp2s0promiscon3、创建macvlancrea......
  • RDLC 报表使用
    RDLC报表使用  RDLC报表使用笔记1.        RDLC介绍2.        RDLC常用的功能介绍1)       WebLogAnalyzer2)       子报表3)  ......