加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

单码加密----简单移位加密法

(2011-07-18 14:42:08)
标签:

it

分类: 密码学

简单移位加密法:也是所谓的凯撒移位密码(只是凯撒的移位就是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!");
            }
            else
            {
                char[] str = plaintext.ToCharArray();
                for (int i = 0; i < str.Length; i++)
                {
                    if (str[i] == ' ')
                    {
                        ciphertext.Append(str[i]);
                    }
                    else if (str[i] >= 'A' && str[i] <= 'Z')
                    {
                       // 这其中有个规律,以小写为例,因为a-z有个循环,
                       //当一个字母加上移位数n大于z时则减去26(英文中总共26个字母),
                       //否则不用减。
                        

                        int inter = (int)str[i] + n;
                        if (inter > (int)('Z'))
                        {
                            str[i] = (char)(inter - 26);
                            ciphertext.Append(str[i]);
                        }
                        else
                        {
                            str[i] = (char)inter;
                            ciphertext.Append(str[i]);
                        }
                    }
                    else if (str[i] >= 'a' && str[i] <= 'z')
                    {
                        int inter = (int)str[i] + n;
                        if (inter > (int)('z'))
                        {
                            str[i] = (char)(inter - 26);
                            ciphertext.Append(str[i]);
                        }
                        else
                        {
                            str[i] = (char)inter;
                            ciphertext.Append(str[i]);
                        }
                    }
                    else
                    {
                        Console.WriteLine("Sorry,plaintext\'s convert not right!");
                    }
                }
            }
            return ciphertext.ToString();
        }
        #endregion
        #region 解密
        public static string getPlainText(string ciphertext, int n)
        {
            StringBuilder plaintext = new StringBuilder();
            if (ciphertext == "")
            {
                Console.WriteLine("Sorry,plaintext cann\'t be null");
            }
            else if (n < 1 || n > 25)
            {
                Console.WriteLine("Sorry,the n is not right!");
            }
            else
            {
                char[] str = ciphertext.ToCharArray();
                for (int i = 0; i < str.Length; i++)
                {
                    if (str[i] == ' ')
                    {
                        plaintext.Append(str[i]);
                    }
                    else if (str[i] >= 'A' && str[i] <= 'Z')
                                        
                        //这其中有个规律,以小写为例,因为a-z有个循环,
                        //当一个字母加上移位数-n小于a时则加上26(英文中总共26个字母),
                        //否则不用减。这是加密算法的逆过程
                        int inter = (int)str[i] - n;
                        if (inter < (int)('A'))
                        {
                            str[i] = (char)(inter + 26);
                            plaintext.Append(str[i]);
                        }
                        else
                        {
                            str[i] = (char)inter;
                            plaintext.Append(str[i]);
                        }
                    }
                    else if (str[i] >= 'a' && str[i] <= 'z')
                    {
                        int inter = (int)str[i] - n;
                        if (inter < (int)('a'))
                        {
                            str[i] = (char)(inter + 26);
                            plaintext.Append(str[i]);
                        }
                        else
                        {
                            str[i] = (char)inter;
                            plaintext.Append(str[i]);
                        }
                    }
                    else
                    {
                        Console.WriteLine("Sorry,plaintext\'s convert not right!");
                    }
                }
            }
            return plaintext.ToString();
            
        #endregion
       
    }
}

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有