双轨加密算法

一、目的

将明文进行简单的加密并对该加密进行翻译(解密),所有明文、密文都保存在文件中。

二、加密原理

将明文中的奇数位和偶数位分别存放,对明文排序混乱,可实现无法直接读取明文内容。

三、文本预处理

原因:

因为原文是正常书写的自然语言(英语),但是计算机语言中有很多字符是自然语言无法书写的,所以对这些无用的字符进行消去处理,方便对内容的加密处理。

方法:

对从文件中读取的内容的每个字符进行判断,将ASCII码在33和126以外的字符舍弃。

四、实现代码(C#)

文件处理函数:
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace SSLHomework1.Service
{
    /// <summary>
    /// 文件操作
    /// </summary>
    class MyFile : IMyFile
    {
        /// <summary>
        /// 读取文件内容
        /// </summary>
        /// <param name="fileSrc"></param>
        /// <returns></returns>
        public string ReadFile(string fileSrc) {
            string content = string.Empty;
            using (StreamReader sr = new StreamReader(fileSrc))
            {
                string oneLine = sr.ReadLine();    //从当前流中读取一行字符并将数据作为字符串返回。
                while (oneLine != null)            //读到的行资料不是空时继续
                {
                    content += oneLine + "\n";            //保存内容
                    oneLine = sr.ReadLine();       //读下一行
                }
            }
            return content;
        }

        /// <summary>
        /// 读取文件内容(只保留英文字符)
        /// </summary>
        /// <param name="fileSrc"></param>
        /// <returns></returns>
        public string ReadCharacterOnlyOfFile(string fileSrc) {
            return Regularized(ReadFile(fileSrc)); //将文件内容中非英文字母全部去除
        }

        /// <summary>
        /// 将内容保存到文件中
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public void SaveFile(string content, string fileSrc) {
            try
            {
                Console.WriteLine(content);
                using (FileStream fs = new FileStream(fileSrc, FileMode.Create, FileAccess.Write))  //没有这个文件则新建
                {
                    StreamWriter sw = new StreamWriter(fs);
                    sw.Write(content);
                    sw.Close();
                    sw.Dispose();
                    fs.Dispose();
                }
                Console.WriteLine("文件保存成功");
            }
            catch (Exception ex)
            {
                Console.WriteLine(string.Format("写入文件出错:消息={0},堆栈={1}", ex.Message, ex.StackTrace));
                Console.WriteLine("文件保存错误");
            }

        }
        /// <summary>
        /// 将内容保存到文件中
        /// </summary>
        /// <param name="content"></param>
        /// <param name="fileSrc"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public string SaveFile(string content, string fileSrc, string fileName) {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 将内容的非英文字符进行消除
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        private string Regularized(string content) {
            string regularizedContent = string.Empty;
            var length = content.Length;        //读取内容总的字符长度
            if (length > 0) {
                var tag = true;                     //终止标记
                var location = 0;                   //当前读取文本所在位置
                while (tag) {
                    if ((int)content[location] > 32 && (int) content[location] < 127 ) { //只保留因为英文有效字符
                        regularizedContent += content[location];
                    }
                    location++;
                    if (!(location<length)) {       //已经读取到最后一个字符,退出循环
                        tag = false;
                    }
                }
            }
            return regularizedContent;
        }
    }
}

加密算法

using System;
using System.Net.Http.Headers;
using System.Text.RegularExpressions;

namespace SSLHomework1.Service
{
    /// <summary>
    /// 双轨加密
    /// </summary>
   class DualTrack : IDualTrack
    {
        /// <summary>
        /// 双轨密码加密
        /// </summary>
        /// <returns></returns>
        public string Encrypt(string content) {
            var first = string.Empty;
            var second = string.Empty;
            var length = content.Length;
            if (length>0) {
                var tag = true;                     //终止标记
                var location = 0;                   //当前读取字符所在位置
                while (tag)
                {
                    if (location % 2 < 1)
                        first += content[location];
                    else
                        second += content[location];
                    location++;
                    if (!(location < length))
                    {       //已经读取到最后一个字符,退出循环
                        tag = false;
                    }
                }
            } else {
                Console.WriteLine("没有加密内容");
            }
            return first+"\n"+second;
        }
        /// <summary>
        /// 双轨密码解密
        /// </summary>
        /// <returns></returns>
        public string Deciphering(string content) {
            string result = string.Empty;
            if (content.Length>0) {
                var encryptedContent = Regex.Split(content, "\n", RegexOptions.IgnoreCase);    //将内容分割
                int i;
                //标记哪部分字母更多,-1为后半部分,1为前半部分更多,0位一样多
                if (encryptedContent[0].Length - encryptedContent[1].Length > 0)
                {   //前半部分更多则应取后半部分的长度
                    for (i = 0; i < encryptedContent[1].Length; i++)
                    {
                        result += encryptedContent[0][i].ToString() + encryptedContent[1][i].ToString();//将前后两半部分拼接
                    }
                    result += encryptedContent[0][i];
                } else if (encryptedContent[0].Length - encryptedContent[1].Length < 0) {
                    //后半部分更多则应取前半部分的长度
                    for (i = 0; i < encryptedContent[0].Length; i++)
                    {
                        result += encryptedContent[0][i].ToString() + encryptedContent[1][i].ToString();//将前后两半部分拼接
                    }
                    result += encryptedContent[1][i];
                } else
                {
                    //后半部分更多则应取前半部分的长度
                    for (i = 0; i < encryptedContent[0].Length; i++)
                    {
                        result += encryptedContent[0][i].ToString() + encryptedContent[1][i].ToString();//将前后两半部分拼接
                    }
                }
            } else {
                Console.WriteLine("没有加密内容输入");
            }

            return result;
        }
    }
}

五、运行结果

在这里插入图片描述

六、开源地址

码云dualTrack分支

(https://gitee.com/plasma/Information-security-basics-study-notes.git)

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值