简单移位加密法:也是所谓的凯撒移位密码(只是凯撒的移位就是3位),最早追
溯到古罗马时期,《高卢战记》描述到它是使用替代密码,通过将字母按顺序推
后3位起到加密的作用,如将字母A换做D,将字母B换做E。
可以看到确实很简答,要想破解此中类型的密码做个最多25次的推测循环就能得
到移位的位数。
下面我们做个程序例子来进行加密和解密的算法实现。
我们就以英文为例,中文的是比较复杂,以后再讨论。
这个移位算法中a-z是个循环。
下面是用c#语言写的一个加密解密程序:
using System;
using System.Collections.Generic;
using System.Text;
namespace 密码学
{
class
Program
{
static void Main(string[] args)
{
string plaintext = "";//明文
string ciphertext = "";//密文
int n = 0;//位数
plaintext = Console.ReadLine();
n = int.Parse(Console.ReadLine());
ciphertext=getCipherText(plaintext,n);
Console.WriteLine("密文为:"+ciphertext);
Console.WriteLine("明文:"+getPlainText(ciphertext,n));
}
#region 加密
public static string getCipherText(string plaintext, int n)
{
StringBuilder ciphertext = new StringBuilder();
if (plaintext == "")
{
Console.WriteLine("Sorry,plaintext cann\'t be null");
}
else if (n < 1 || n > 25)
{
Console.WriteLine("Sorry,the n is not right!");
}