凯撒加密的改进算法(C#版)

来源:互联网 发布:女性按摩棒原理知乎 编辑:程序博客网 时间:2024/06/06 04:01

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace move
{
    class Program
    {
        #region//定义移动任意位函数
        static void move(string s,int n)
        {
            int m = n % 26;
            char[] move = new char[s.Length];
            string s1 = "abcdefghijklmnopqrstuvwxyz";
            for (int i = 0; i < s.Length;i++ )
            {
                if (s[i] >= 97 && s[i] <= 122)
                {
                    for (int j = 0; j < 26;j++ )
                    {
                        if(s1[j] == s[i])
                        {
                            if (s[i] + m <= 122)
                            {
                                move[i] = (char)(s1[j] + n);
                                break;
                            }
                            else
                            {
                                move[i] = (char)(s1[j] + m - 122 + 96);
                                break;
                            }
                        }
                    }
                }        
            }
            for (int k = 0; k < s.Length; k++)
            {
                Console.Write(move[k]);
            }
           Console.ReadKey();
        }
        #endregion
        #region//主函数体
        static void Main(string[] args)
        {
            string s;
            int n;
            Console.WriteLine("Input the plaintext.");
            s = Console.ReadLine().ToLower();
            Console.WriteLine("Input the number.");
            n = Convert.ToInt32(Console.ReadLine());
            move(s,n);
        }
        #endregion
    }
}

原创粉丝点击