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());
}
}
}
分享到:
相关推荐
JAVA实现的Caesar加密算法,使用SWT插件堆积界面做了初步的设计,使用简单,附有源代码,对学习和实用都很方便
Caesa加密、解密算法,用c/c++语言实现
php php_使用php开发的cipher加密算法之CaesarCipher_加密算法实现
用MATLAB实现了Caesar密码的加密与解密,分别写成函数文件,并附带测试文件,可以正常运行。
凯撒密码算法的实现,程序完全可以实现,帮助大家学习和参考
Carsar加密算法是最简单的加密算法,原理是把一个字母在字母表中移动相应的位置,比如输入a,将其移动3位,经过Caesar加密后输出的d,位置可以循环移动,输入x,则输出a
古典密码算法Caesar密码 3.古典密码算法Playfair密码 4.对称密码算法DES 5.对称密码算法AES 6.非对称密码算法RSA 7.HASH算法MD5 的实验报告,里面有详细的实验分析。其中大部分源码都是可以直接粘贴过去运行的。。
在密码学中,恺撒密码(英语:Caesar cipher),或称恺撒加密、恺撒变换、变换加密,是一种最简单且最...本资源使用C语言实现凯撒算法加密和解密,可以实现数据的加密和文件的加密,不借助任何的第三方工具,便于移植。
使用控制台参数来运行Caesar加密及解密的java小算法。
用于教育目的,C# 实现的各种算法的集合。存储库是用 C# 实现的各种算法的集合。这些算法涵盖各种主题 从计算机科学、数学和统计学、数据科学、机器学习、工程学等。实现 及其相关文档旨在为教育工作者和学生提供...
它的主要应用对象是文字信息,利用密码算法实现文字信息的加密和解密。下面介绍两 种常见的具有代表性的古典密码算法,以帮助读者对密码算法建立一个初步的印象。 1.替代密码(循环移位密码) 替代密码算法的原理是...
信息安全中,包括DES,RSA,Caesar,DH,密钥分配等算法的java实现,还包括图形界面设计。
凯撒密码c语言
用js在web上实现Caesar密码编程实验
实验一:Caesar算法实现加密解密,使用tkinter、string库,有完整操作界面; 实验二:RSA算法实现加密解密,使用rsa、sys、tkinter、binascii库,有完整操作界面; 实验三:数字图像水印加密检测,使用tkinter、PIL...
概述算法数据压缩Huffman压缩机Shannon-Fano压缩机编码器Caesar Vigenere Hill背包问题朴素求解器动态规划求解器数值分解LU最大公约数欧几里德GCD二进制GCD分解试验除法高斯-约旦消除搜索A-星二进制线性快速搜索...
Caesar-cipher是Julius Caesar开发的一种简单加密算法,用于加密他与古代世界中Cicero的通信。 因此,它使用简单的字符替换方法将给定字符替换为另一个。 十三个按键的移位称为ROT13。 可以通过再次使用ROT13对其...
encrypt-decrypt-pyqt5项目简介一个带有 GUI 界面的密码学工具功能特性实现了 11 种古典密码、2 种流密码、2 种分组密码、2 种公钥密码、和 1 种哈希算法。可对字符串和文件进行加密和解密。附带 PyQt 实现的 GUI ...
Caesar Vigenere Hill NYSIIS Soundex Feistel Blowfish Graph Minimum Spanning Tree Prim's Algorithm (Adjacency Matrix) Kruskal's Algorithm BreadthFirstTreeTraversal BreadthFirstSearch DepthFirstSearch ...
本代码在VC++6.0的平台上实现了凯撒密码的加密解密过程,并包含出错提示等信息,希望大家批评指正,谢谢!