该算法属于对称加密算法,与以前算法的区别在于,此算法中的置换表是随机生成的,并且对应位所采用的采用的置换码是根据密码中对应位的ASC码和上次使用的置换码的相对位置决定的.因此,可以认为每次使用的置换表都是不同的.因此,如果想根据加密结果寻找规律基本上是徒劳的.而采用暴力破解的方式,由于计算机很难判解密后的文字是否还有对应的含义,因此也是难以破截的.只有输入正确的密码后才可以获得正确的明文.
不知道现在是不是有类似的算法存在,也不敢保证自己的论证是正确的,所以放到这里大家评论一下!
代码如下:
using System;
namespace SecFileLib
{
/// <summary>
/// MySec 的摘要说明。
/// </summary>
public class MySec
{
/// <summary>
/// 加密钥最长长度
/// </summary>
public const int MaxLongEncryptCodeLength=128;
public MySec()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 对文件进行加密的函数
/// </summary>
/// <param name="inputstr">源文件输入流</param>
/// <param name="outputstr">密文输出流</param>
/// <param name="pwd">密码</param>
static public void Encrypt(System.IO.Stream inputstr,System.IO.Stream outputstr,string pwd)
{
byte[] Ec = GetLongEncryptCode();
byte[] pw = System.Text.Encoding.Default.GetBytes(pwd);
for(int i=0;i<Ec.Length;i++)
{
byte c = Ec[i];
c = (byte)(c ^ pw[i%pw.Length]);
outputstr.WriteByte(c);
}
int ecpos = 0;//加密钥当前位
for(int i=0;i<inputstr.Length;i++)
{
byte c = (byte)inputstr.ReadByte();
ecpos = (ecpos + (int)pw[i%pw.Length])%Ec.Length;
byte ecval = Ec[ecpos];
c = (byte)(c ^ ecval);
outputstr.WriteByte(c);
}
}
/// <summary>
/// 解密函数
/// </summary>
/// <param name="inputstr">加密内容输入流</param>
/// <param name="outputstr">明文输出流</param>
/// <param name="pwd">密码</param>
static public void Decrypt(System.IO.Stream inputstr,System.IO.Stream outputstr,string pwd)
{
byte[] Ec = new byte[MaxLongEncryptCodeLength];
inputstr.Read(Ec,0,Ec.Length);
byte[] pw = System.Text.Encoding.Default.GetBytes(pwd);
for(int i=0;i<Ec.Length;i++)
Ec[i] = (byte)(Ec[i] ^ pw[i%pw.Length]);
int ecpos = 0;//加密钥当前位
for(int i=0;i<inputstr.Length-Ec.Length;i++)
{
byte c = (byte)inputstr.ReadByte();
ecpos = (ecpos + (int)pw[i%pw.Length])%Ec.Length;
byte ecval = Ec[ecpos];
c = (byte)(c ^ ecval);
outputstr.WriteByte(c);
}
}
/// <summary>
/// 动态密码生成函数
/// </summary>
/// <returns>0-255之间的数组成的字节数组</returns>
static public byte[] GetLongEncryptCode()
{
System.Random r = new Random();
byte[] cc = new byte[MaxLongEncryptCodeLength];
for(int i=0;i<MaxLongEncryptCodeLength;i++)
cc[i] = (byte)r.Next(0,255);
return cc;
}
}
}