凯撒密码技术是最早也是最简单的加密技术之一。它只是一种替换密码,即给定文本的每个字母都被字母表中某个固定位置的字母替换。例如,移位为1时,a将被B取代,B将变成C,依此类推。这种方法显然是以朱利叶斯·凯撒(Julius Caesar)的名字命名的,他显然是用这种方法与他的官员交流的。 因此,要对给定的文本进行加密,我们需要一个整数值,称为移位,它指示文本中每个字母的下移位置。 根据方案A=0,B=1,…,Z=25,可以使用模块化算法表示加密,首先将字母转换为数字。通过移位n对字母进行加密可以在数学上描述为。
(带移位n的加密阶段)
(带移位n的解密阶段)
例如:
Text : ABCDEFGHIJKLMNOPQRSTUVWXYZShift: 23Cipher: XYZABCDEFGHIJKLMNOPQRSTUVWText : ATTACKATONCEShift: 4Cipher: EXXEGOEXSRGI
凯撒密码算法: 输入:
- 一组小写字母,称为文本。
- 0-25之间的整数,表示所需的移位。
程序:
- 每次遍历给定文本一个字符。
- 对于每个字符,根据规则转换给定字符,这取决于我们是加密还是解密文本。
- 返回生成的新字符串。
接收文本(字符串)和移位值(整数)并返回加密文本的程序。
C++
// A C++ program to illustrate Caesar Cipher Technique #include <iostream> using namespace std; // This function receives text and shift and // returns the encrypted text string encrypt(string text, int s) { string result = "" ; // traverse text for ( int i=0;i<text.length();i++) { // apply transformation to each character // Encrypt Uppercase letters if ( isupper (text[i])) result += char ( int (text[i]+s-65)%26 +65); // Encrypt Lowercase letters else result += char ( int (text[i]+s-97)%26 +97); } // Return the resulting string return result; } // Driver program to test the above function int main() { string text= "ATTACKATONCE" ; int s = 4; cout << "Text : " << text; cout << "Shift: " << s; cout << "Cipher: " << encrypt(text, s); return 0; } |
JAVA
//A Java Program to illustrate Caesar Cipher Technique class CaesarCipher { // Encrypts text using a shift od s public static StringBuffer encrypt(String text, int s) { StringBuffer result= new StringBuffer(); for ( int i= 0 ; i<text.length(); i++) { if (Character.isUpperCase(text.charAt(i))) { char ch = ( char )((( int )text.charAt(i) + s - 65 ) % 26 + 65 ); result.append(ch); } else { char ch = ( char )((( int )text.charAt(i) + s - 97 ) % 26 + 97 ); result.append(ch); } } return result; } // Driver code public static void main(String[] args) { String text = "ATTACKATONCE" ; int s = 4 ; System.out.println( "Text : " + text); System.out.println( "Shift : " + s); System.out.println( "Cipher: " + encrypt(text, s)); } } |
Python3
#A python program to illustrate Caesar Cipher Technique def encrypt(text,s): result = "" # traverse text for i in range ( len (text)): char = text[i] # Encrypt uppercase characters if (char.isupper()): result + = chr (( ord (char) + s - 65 ) % 26 + 65 ) # Encrypt lowercase characters else : result + = chr (( ord (char) + s - 97 ) % 26 + 97 ) return result #check the above function text = "ATTACKATONCE" s = 4 print ( "Text : " + text) print ( "Shift : " + str (s)) print ( "Cipher: " + encrypt(text,s)) |
C#
// A C# Program to illustrate Caesar Cipher Technique using System; using System.Text; public class CaesarCipher { // Encrypts text using a shift od s public static StringBuilder encrypt(String text, int s) { StringBuilder result= new StringBuilder(); for ( int i=0; i<text.Length; i++) { if ( char .IsUpper(text[i])) { char ch = ( char )((( int )text[i] + s - 65) % 26 + 65); result.Append(ch); } else { char ch = ( char )((( int )text[i] + s - 97) % 26 + 97); result.Append(ch); } } return result; } // Driver code public static void Main(String[] args) { String text = "ATTACKATONCE" ; int s = 4; Console.WriteLine( "Text : " + text); Console.WriteLine( "Shift : " + s); Console.WriteLine( "Cipher: " + encrypt(text, s)); } } /* This code contributed by PrinciRaj1992 */ |
PHP
<?php // A PHP program to illustrate Caesar // Cipher Technique // This function receives text and shift // and returns the encrypted text function encrypt( $text , $s ) { $result = "" ; // traverse text for ( $i = 0; $i < strlen ( $text ); $i ++) { // apply transformation to each // character Encrypt Uppercase letters if (ctype_upper( $text [ $i ])) $result = $result . chr ((ord( $text [ $i ]) + $s - 65) % 26 + 65); // Encrypt Lowercase letters else $result = $result . chr ((ord( $text [ $i ]) + $s - 97) % 26 + 97); } // Return the resulting string return $result ; } // Driver Code $text = "ATTACKATONCE" ; $s = 4; echo "Text : " . $text ; echo "Shift: " . $s ; echo "Cipher: " . encrypt( $text , $s ); // This code is contributed by ita_c ?> |
Javascript
<script> //A Javascript Program to illustrate Caesar Cipher Technique // Encrypts text using a shift od s function encrypt(text, s) { let result= "" for (let i = 0; i < text.length; i++) { let char = text[i]; if (char.toUpperCase(text[i])) { let ch = String.fromCharCode((char.charCodeAt(0) + s-65) % 26 + 65); result += ch; } else { let ch = String.fromCharCode((char.charCodeAt(0) + s-97) % 26 + 97); result += ch; } } return result; } // Driver code let text = "ATTACKATONCE" ; let s = 4; document.write( "Text : " + text + "<br>" ); document.write( "Shift : " + s + "<br>" ); document.write( "Cipher: " + encrypt(text, s) + "<br>" ); // This code is contributed by avanitrachhadiya2155 </script> |
输出:
Text : ATTACKATONCEShift: 4Cipher: EXXEGOEXSRGI
如何解密? 我们可以编写另一个类似于encrypt的函数decrypt,它将应用给定的反方向移位来解密原始文本。然而,我们可以利用密码在模下的循环性质,因此我们可以简单地观察
Cipher(n) = De-cipher(26-n)
因此,我们可以使用相同的函数来解密,我们将修改移位值,使移位=26移位(参见 这 对于在C++中运行的示例)。
https://www.youtube.com/watch?v=S472gPqwF
-o
本文由 阿什图什·库马尔。 如果你喜欢GeekSforgeks,并且想贡献自己的力量,你也可以写一篇文章,然后把你的文章邮寄给评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写评论