[C#] Caesar Cipher凱薩編碼加強版

支援自訂密碼書,包含數字、符號,也可以亂數排列增加複雜度。
支援預先定義key.


Using:

using System;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using System.Linq;



 public static class CeasarCipher
    {
        // Default Key
        private const int Key = 3;
        // To increase the complexity of the ceaser cipher lower case"a-z", Numerics and Symbols also can be used
        private const string alphabets = @"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

        private static char cipher(char c, int key)
        {
            int indexOfChar = alphabets.IndexOf(c);
            return (indexOfChar < 0) ? c : alphabets[(indexOfChar + key) % alphabets.Length];
        }

        public static string Encipher(string input, int key = Key)
        {
            string output = string.Empty;

            foreach (char c in input)
            {
                if (c == ' ' || c == '\n') // keep white space / new line
                {
                    output += c;
                }
                else
                {
                    output += cipher(c, key);
                }
            }
            return output;
        }

        public static string Decipher(string input, int key = Key)
        {
            return Encipher(input, alphabets.Length - key);
        }
    }
相關系列文章:

留言

這個網誌中的熱門文章

[TCL] 基本語法與指令 - 3. 資料型態

[TCL] 基本語法與指令 - 2. TCL 語法

[TCL] 基本語法與指令 - 1. TCL 簡介