凯撒密码-移位密码C#算法实现
凯撒密码简介:凯撒密码作为一种最为古老的对称加密体制,在古罗马的时候都已经很流行,他的基本思想是:通过把字母移动一定的位数来实现加密和解密。例如,如果密匙是把明文字母的位数向后移动三位,那么明文字母B就变成了密文的E,依次类推,X将变成A,Y变成B,Z变成C,由此可见,位数就是凯撒密码加密和解密的密钥。 它是一种代换密码。据说恺撒是率先使用加密函的古代将领之一,因此这种加密方法被称为恺撒密码。 在密码学中,恺撒密码(或称恺撒加密、恺撒变换、变换加密)是一种最简单且最广为人知的加密技术。它是一种替换加密的技术,明文中的所有字母都在字母表上向后(或向前)按照一个固定数目进行偏移后被替换成密文。例如,当偏移量是3的时候,所有的字母A将被替换成D,B变成E,以此类推。这个加密方法是以恺撒的名字命名的,当年恺撒曾用此方法与其将军们进行联系。 恺撒密码通常被作为其他更复杂的加密方法中的一个步骤,例如维吉尼亚密码。恺撒密码还在现代的ROT13系统中被应用。但是和所有的利用字母表进行替换的加密技术一样,恺撒密码非常容易被破解,而且在实际应用中也无法保证通信安全。
程序代码:
StringBuilder sb = new StringBuilder();//用来保存计算后的密文或者明文 string k;//用来保存密钥 string m;//明文或者密文 int resultLN;//保存字母对应的数值
程序代码:
#region 循环 for (int i = 0; i < m.Length; i++) { switch (m[i].ToString()) { #region 大写 #region A case "A": { resultLN = (0 + Int32.Parse(k))%26; switch (resultLN) { case 0: sb.Append("A"); break; case 1: sb.Append("B"); break; case 2: sb.Append("C"); break; case 3: sb.Append("D"); break; case 4: sb.Append("E"); break; case 5: sb.Append("F"); break; case 6: sb.Append("G"); break; case 7: sb.Append("H"); break; case 8: sb.Append("I"); break; case 9: sb.Append("J"); break; case 10: sb.Append("K"); break; case 11: sb.Append("L"); break; case 12: sb.Append("M"); break; case 13: sb.Append("N"); break; case 14: sb.Append("O"); break; case 15: sb.Append("P"); break; case 16: sb.Append("Q"); break; case 17: sb.Append("R"); break; case 18: sb.Append("S"); break; case 19: sb.Append("T"); break; case 20: sb.Append("U"); break; case 21: sb.Append("V"); break; case 22: sb.Append("W"); break; case 23: sb.Append("X"); break; case 24: sb.Append("Y"); break; case 25: sb.Append("Z"); break; default: break; } } break; #endregion #region B case "B": { resultLN = (1 + Int32.Parse(k))%26; switch (resultLN) { case 0: sb.Append("A"); break; case 1: sb.Append("B"); break; case 2: sb.Append("C"); break; case 3: sb.Append("D"); break; case 4: sb.Append("E"); break; case 5: sb.Append("F"); break; case 6: sb.Append("G"); break; case 7: sb.Append("H"); break; case 8: sb.Append("I"); break; case 9: sb.Append("J"); break; case 10: sb.Append("K"); break; case 11: sb.Append("L"); break; case 12: sb.Append("M"); break; case 13: sb.Append("N"); break; case 14: sb.Append("O"); break; case 15: sb.Append("P"); break; case 16: sb.Append("Q"); break; case 17: sb.Append("R"); break; case 18: sb.Append("S"); break; case 19: sb.Append("T"); break; case 20: sb.Append("U"); break; case 21: sb.Append("V"); break; case 22: sb.Append("W"); break; case 23: sb.Append("X"); break; case 24: sb.Append("Y"); break; case 25: sb.Append("Z"); break; default: break; } } break; #endregion #region C case "C": { resultLN = (2 + Int32.Parse(k))%26; switch (resultLN) { case 0: sb.Append("A"); break; case 1: sb.Append("B"); break; case 2: sb.Append("C"); break; case 3: sb.Append("D"); break; case 4: sb.Append("E"); break; case 5: sb.Append("F"); break; case 6: sb.Append("G"); break; case 7: sb.Append("H"); break; case 8: sb.Append("I"); break; case 9: sb.Append("J"); break; case 10: sb.Append("K"); break; case 11: sb.Append("L"); break; case 12: sb.Append("M"); break; case 13: sb.Append("N"); break; case 14: sb.Append("O"); break; case 15: sb.Append("P"); break; case 16: sb.Append("Q"); break; case 17: sb.Append("R"); break; case 18: sb.Append("S"); break; case 19: sb.Append("T"); break; case 20: sb.Append("U"); break; case 21: sb.Append("V"); break; case 22: sb.Append("W"); break; case 23: sb.Append("X"); break; case 24: sb.Append("Y"); break; case 25: sb.Append("Z"); break; default: break; } } break; #endregion #region D case "D": { resultLN = (3 + Int32.Parse(k))%26; switch (resultLN) { case 0: sb.Append("A"); break; case 1: sb.Append("B"); break; case 2: sb.Append("C"); break; case 3: sb.Append("D"); break; case 4: sb.Append("E"); break; case 5: sb.Append("F"); break; case 6: sb.Append("G"); break; case 7: sb.Append("H"); break; case 8: sb.Append("I"); break; case 9: sb.Append("J"); break; case 10: sb.Append("K"); break; case 11: sb.Append("L"); break; case 12: sb.Append("M"); break; case 13: sb.Append("N"); break; case 14: sb.Append("O"); break; case 15: sb.Append("P"); break; case 16: sb.Append("Q"); break; case 17: sb.Append("R"); break; case 18: sb.Append("S"); break; case 19: sb.Append("T"); break; case 20: sb.Append("U"); break; case 21: sb.Append("V"); break; case 22: sb.Append("W"); break; case 23: sb.Append("X"); break; case 24: sb.Append("Y"); break; case 25: sb.Append("Z"); break; default: break; } } break; #endregion #region E case "E": { resultLN = (4 + Int32.Parse(k))%26; switch (resultLN) { case 0: sb.Append("A"); break; case 1: sb.Append("B"); break; case 2: sb.Append("C"); break; case 3: sb.Append("D"); break; case 4: sb.Append("E"); break; case 5: sb.Append("F"); break; case 6: sb.Append("G"); break; case 7: sb.Append("H"); break; case 8: sb.Append("I"); break; case 9: sb.Append("J"); break; case 10: sb.Append("K"); break; case 11: sb.Append("L"); break; case 12: sb.Append("M"); break; case 13: sb.Append("N"); break; case 14: sb.Append("O"); break; case 15: sb.Append("P"); break; case 16: sb.Append("Q"); break; case 17: sb.Append("R"); break; case 18: sb.Append("S"); break; case 19: sb.Append("T"); break; case 20: sb.Append("U"); break; case 21: sb.Append("V"); break; case 22: sb.Append("W"); break; case 23: sb.Append("X"); break; case 24: sb.Append("Y"); break; case 25: sb.Append("Z"); break; default: break; } } break; #endregion