大佬教程收集整理的这篇文章主要介绍了Caesar算法的3种实现,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_197_5@1、Ruby实现:caesar.rb
def encode(plaintext,offset) ciphertext = plaintext.dup for i in 0..(plaintext.length-1) if plaintext[i]>=?a && plaintext[i]<=?z ciphertext[i] = ?a + ((plaintext[i] - ?a + offset) % 26) elsif plaintext[i]>=?A && plaintext[i]<=?Z ciphertext[i] = ?A + ((plaintext[i] - ?A + offset) % 26) elsif plaintext[i]>=?0 && plaintext[i]<=?9 ciphertext[i] = ?0 + ((plaintext[i] - ?0 + offset) % 10) end end print "Ciphertext: ",ciphertext end def decode(ciphertext,offset) plaintext = ciphertext.dup for i in 0..(ciphertext.length-1) if ciphertext[i]>=?a && ciphertext[i]<=?z plaintext[i] = ?a + ((ciphertext[i] - ?a - offset) % 26) elsif ciphertext[i]>=?A && ciphertext[i]<=?Z plaintext[i] = ?A + ((ciphertext[i] - ?A - offset) % 26) elsif ciphertext[i]>=?0 && ciphertext[i]<=?9 plaintext[i] = ?0 + ((ciphertext[i] - ?0 - offset) % 10) end end print "Plaintext: ",plaintext end print "what do you want to do? (E)ncode or (d)ecode a text?[e/d]" choose = gets print "Please input offset value: " offset = gets offset = offset[0] - ?0 if choose == "e\n" || choose == "E\n" print "Please input the plaintext:" text = gets encode(text,offset) else print "Please input the ciphertext:" text = gets decode(text,offset) end print "Press ENTER to return." $stdin.gets
@H_197_5@2、Groovy实现:caesar.groovy
import java.io.*; def encode(plaintext,offset) { ciphertext = ""; for (i in plaintext) switch(i) { case 'a'..'z': ciphertext += (char)((char)'a' + (((char)i - (char)'a' + offset) % 26)); break; case 'A'..'Z': ciphertext += (char)((char)'A' + (((char)i - (char)'A' + offset) % 26)); break; case '0'..'9': ciphertext += Integer.valueOf('0') + (((char)i - (char)'0' + offset) % 10); } println("Ciphertext: " + ciphertext); } def decode(ciphertext,offset) { plaintext = ""; for (i in ciphertext) switch(i) { case 'a'..'z': plaintext += (char)((char)'a' + (((char)i - (char)'a' - offset) % 26)); break; case 'A'..'Z': plaintext += (char)((char)'A' + (((char)i - (char)'A' - offset) % 26)); break; case '0'..'9': plaintext += Integer.valueOf('0') + (((char)i - (char)'0' - offset) % 10); } println("Plaintext: " + plaintext); } BufferedReader br = new BufferedReader(new InputStreamReader(system.in)); print "what to you want to do? (E)ncode or Decode?[e/d]"; action = br.readLine(); print "Please input offset value: "; int offset = Integer.valueOf(br.readLine()); print "Please input the " if (action=='d' || action=='D') { print "ciphertext: "; decode(br.readLine(),offset); } else { print "plaintext: "; encode(br.readLine(),offset); } println "Press ENTER..." system.in.read();
@H_197_5@3、Java实现:Caesar.java
import java.io.*; public class Caesar { public static void encode(String Plaintext,int Offset) { String CipherText = ""; for (int i = 0; i < Plaintext.length(); i++) { if (Plaintext.charAt(i) >= 'a' && Plaintext.charAt(i) <= 'z') CipherText += (char)('a' + ((Plaintext.charAt(i) - 'a' + Offset) % 26)); else if (Plaintext.charAt(i) >= 'A' && Plaintext.charAt(i) <= 'Z') CipherText += (char)('A' + ((Plaintext.charAt(i) - 'A' + Offset) % 26)); else if (Plaintext.charAt(i) >= '0' && Plaintext.charAt(i) <= '9') CipherText += (char)('0' + ((Plaintext.charAt(i) - '0' + Offset) % 10)); } System.out.println("Ciphertext: " + CipherText); } public static void decode(String CipherText,int Offset) { String Plaintext = ""; for (int i = 0; i < CipherText.length(); i++) { if (CipherText.charAt(i) >= 'a' && CipherText.charAt(i) <= 'z') Plaintext += (char)('a' + ((CipherText.charAt(i) - 'a' - Offset) % 26)); else if (CipherText.charAt(i) >= 'A' && CipherText.charAt(i) <= 'Z') Plaintext += (char)('A' + ((CipherText.charAt(i) - 'A' - Offset) % 26)); else if (CipherText.charAt(i) >= '0' && CipherText.charAt(i) <= '9') Plaintext += (char)('0' + ((CipherText.charAt(i) - '0' - Offset) % 10)); } System.out.println("Plaintext: " + Plaintext); } public static void main(String[] args) { try { BufferedReader br = new BufferedReader(new InputStreamReader(system.in)); System.out.print ("what to you want to do? (E)ncode or Decode?[e/d]"); String action = br.readLine(); System.out.print ("Please input offset value: "); int offset = Integer.valueOf(br.readLine()); System.out.print ("Please input the "); if (action.equalsIgnoreCase("d")) { System.out.print("ciphertext: "); decode(br.readLine(),offset); } else { System.out.print("plaintext: "); encode(br.readLine(),offset); } System.out.println("Press ENTER..."); system.in.read(); } catch(IOException eX) { System.out.println(ex.getmessage()); } } }
以上是大佬教程为你收集整理的Caesar算法的3种实现全部内容,希望文章能够帮你解决Caesar算法的3种实现所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。