加密技术(学习笔记三)Replace
非常感谢鬼谷谷主 的支持(呵呵本人人气不好..很少有人支持- -)
顺便说说beerhouse是个不错的程序- -运用设计模式得当...当初刚毕业的时候就是从,这个程序上收获不少
----Replace顾名思义就是替换
比如有字符串 abc,可以将他替换成 zxd,zxd就是密钥看
一个简单的例子
Code
//--这是要加密的字符串(或者说要替换的字符串)
StringBuilder alphabet =new StringBuilder("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
//--这是你要得到的钥匙
StringBuilder key = new StringBuilder();
Random rand = new Random();
//---
while (key.Length<26)
{
//---取得随机数
int index = rand.Next(0, alphabet.Length);
//---从字符串数组中随机取得一个字符
char letter = alphabet[index];
//--首先要从取得密钥字符串中删除刚找到的字符串 ~~防止重复
alphabet.Remove(index, 1);
//--将刚找到的字符加入到钥匙字符串中
key.Append(letter);
}
//--这是要加密的字符串(或者说要替换的字符串)
StringBuilder alphabet =new StringBuilder("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
//--这是你要得到的钥匙
StringBuilder key = new StringBuilder();
Random rand = new Random();
//---
while (key.Length<26)
{
//---取得随机数
int index = rand.Next(0, alphabet.Length);
//---从字符串数组中随机取得一个字符
char letter = alphabet[index];
//--首先要从取得密钥字符串中删除刚找到的字符串 ~~防止重复
alphabet.Remove(index, 1);
//--将刚找到的字符加入到钥匙字符串中
key.Append(letter);
}
上面的例子从明文中随机取得密钥.下面是我的例子,我首先将字符串转换成字节数组利用有限的数组空间,然后用
guid替换字节数组的相应位.(我觉得不好应该用字节数组替换或者用随机字符串替换)
比如 byte 1 被替换成 kjdadklfjaskldjfklasjklfajds的guid 依次类推呵呵
Code
--随机替换,首先要知道出现的原文所有的字符的个数,--然后,每一位替换成另外一个字符
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
namespace Password
{
/// <summary>
/// 随机替换类
/// </summary>
public class ReplaceRandom : ABEncryptDecrypt
{
/// <summary>
/// 产生一组密钥
/// </summary>
/// <returns></returns>
public override Object CreateKey(int State, int KeyCount)
{
System.Collections.Generic.List<String> Keys = new List<string>();
for (int i = 0; i < KeyCount; )
{
Guid tepGuid = Guid.NewGuid();
if (Keys.Contains(tepGuid.ToString()))
{
//--如果存在就不存储保证唯一性
continue;
}
else
{
Keys.Add(tepGuid.ToString());
i++;
}
}
return Keys;
}
/// <summary>
///验证
/// </summary>
/// <returns></returns>
public override bool Validate(System.Text.StringBuilder sb)
{
return false;
}
/// <summary>
/// 加密
/// </summary>
/// <param name="InputEncrypt"></param>
/// <returns></returns>
public override void Encrypt(Context InputEncrypt)
{
//----得到字符串要加密的信息
System.Text.StringBuilder sb = new StringBuilder();
//---将明文-转换成子节数祖
byte[] key = System.Text.Encoding.UTF8.GetBytes(InputEncrypt.OriginalText);
//---生成一个不重复的290位密钥
System.Collections.Generic.List<String> Keys = this.CreateKey(0, 290) as System.Collections.Generic.List<String>;
//---将指定的字节数祖------转换------成随机加密数祖
for (int i = 0; i < key.Length; i++)
{
//-----------将字符串的子节组中--正在循环的位转换成 int类型的代表索引
int CollectionIndex = key[i];
//---寻找替换位
String oneKey = Keys[CollectionIndex];
//---写入加密流
sb.Append(oneKey);
if (i < (key.Length - 1))
sb.Append("%");
}
InputEncrypt.OriginalText = String.Empty;
InputEncrypt.CryptographText = sb;
InputEncrypt.Key = Keys;
}
/// <summary>
/// 解密
/// </summary>
/// <param name="InputEncrypt"></param>
/// <returns></returns>
public override void Decrypt(Context InputDecrypt)
{
//---得到钥匙
System.Collections.Generic.List<String> CollectionKeys = InputDecrypt.Key as System.Collections.Generic.List<String>;
//--将秘文分割成字符串数祖
String[] Pawwords = InputDecrypt.CryptographText.ToString().Split('%');
//-----生成和上面 分割的字符串数祖相应的字节数祖
byte[] bytes = new byte[Pawwords.Length];
//---将 Pawwords 转换成对应的子节
for (int i = 0; i < bytes.Length; i++)
{
bytes[i] = new byte();
bytes[i] = (byte)CollectionKeys.IndexOf(Pawwords[i]);
}
InputDecrypt.OriginalText = System.Text.Encoding.UTF8.GetString(bytes);
}
}
}
//--这是测试用的代码- -
//Console.WriteLine("请输入要加密的信息");
//System.Text.StringBuilder IfAdd = new StringBuilder(Console.ReadLine());
//ABEncryptDecrypt AED = new ReplaceRandom();
//Context C = new Context();
//C.OriginalText = IfAdd.ToString();
//AED.Encrypt(C);
//Console.WriteLine(C.CryptographText.ToString());
//AED.Decrypt(C);
//Console.WriteLine(C.OriginalText as String);
--随机替换,首先要知道出现的原文所有的字符的个数,--然后,每一位替换成另外一个字符
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
namespace Password
{
/// <summary>
/// 随机替换类
/// </summary>
public class ReplaceRandom : ABEncryptDecrypt
{
/// <summary>
/// 产生一组密钥
/// </summary>
/// <returns></returns>
public override Object CreateKey(int State, int KeyCount)
{
System.Collections.Generic.List<String> Keys = new List<string>();
for (int i = 0; i < KeyCount; )
{
Guid tepGuid = Guid.NewGuid();
if (Keys.Contains(tepGuid.ToString()))
{
//--如果存在就不存储保证唯一性
continue;
}
else
{
Keys.Add(tepGuid.ToString());
i++;
}
}
return Keys;
}
/// <summary>
///验证
/// </summary>
/// <returns></returns>
public override bool Validate(System.Text.StringBuilder sb)
{
return false;
}
/// <summary>
/// 加密
/// </summary>
/// <param name="InputEncrypt"></param>
/// <returns></returns>
public override void Encrypt(Context InputEncrypt)
{
//----得到字符串要加密的信息
System.Text.StringBuilder sb = new StringBuilder();
//---将明文-转换成子节数祖
byte[] key = System.Text.Encoding.UTF8.GetBytes(InputEncrypt.OriginalText);
//---生成一个不重复的290位密钥
System.Collections.Generic.List<String> Keys = this.CreateKey(0, 290) as System.Collections.Generic.List<String>;
//---将指定的字节数祖------转换------成随机加密数祖
for (int i = 0; i < key.Length; i++)
{
//-----------将字符串的子节组中--正在循环的位转换成 int类型的代表索引
int CollectionIndex = key[i];
//---寻找替换位
String oneKey = Keys[CollectionIndex];
//---写入加密流
sb.Append(oneKey);
if (i < (key.Length - 1))
sb.Append("%");
}
InputEncrypt.OriginalText = String.Empty;
InputEncrypt.CryptographText = sb;
InputEncrypt.Key = Keys;
}
/// <summary>
/// 解密
/// </summary>
/// <param name="InputEncrypt"></param>
/// <returns></returns>
public override void Decrypt(Context InputDecrypt)
{
//---得到钥匙
System.Collections.Generic.List<String> CollectionKeys = InputDecrypt.Key as System.Collections.Generic.List<String>;
//--将秘文分割成字符串数祖
String[] Pawwords = InputDecrypt.CryptographText.ToString().Split('%');
//-----生成和上面 分割的字符串数祖相应的字节数祖
byte[] bytes = new byte[Pawwords.Length];
//---将 Pawwords 转换成对应的子节
for (int i = 0; i < bytes.Length; i++)
{
bytes[i] = new byte();
bytes[i] = (byte)CollectionKeys.IndexOf(Pawwords[i]);
}
InputDecrypt.OriginalText = System.Text.Encoding.UTF8.GetString(bytes);
}
}
}
//--这是测试用的代码- -
//Console.WriteLine("请输入要加密的信息");
//System.Text.StringBuilder IfAdd = new StringBuilder(Console.ReadLine());
//ABEncryptDecrypt AED = new ReplaceRandom();
//Context C = new Context();
//C.OriginalText = IfAdd.ToString();
//AED.Encrypt(C);
//Console.WriteLine(C.CryptographText.ToString());
//AED.Decrypt(C);
//Console.WriteLine(C.OriginalText as String);