一、目的
将明文进行简单的加密并对该加密进行翻译(解密),所有明文、密文都保存在文件中。
二、加密原理
将明文中的奇数位和偶数位分别存放,对明文排序混乱,可实现无法直接读取明文内容。
三、文本预处理
原因:
因为原文是正常书写的自然语言(英语),但是计算机语言中有很多字符是自然语言无法书写的,所以对这些无用的字符进行消去处理,方便对内容的加密处理。
方法:
对从文件中读取的内容的每个字符进行判断,将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)