天天看點

Caesar算法的3種實作

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

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();

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());

  }

 }

}

繼續閱讀