Caesar Block Cipher C++ Reads the Standard Input
The Caesar Cipher technique is one of the primeval and simplest method of encryption technique. It's only a type of exchange goose egg, i.due east., each letter of a given text is replaced by a letter some stock-still number of positions down the alphabet. For example with a shift of 1, A would be replaced by B, B would become C, and so on. The method is evidently named later on Julius Caesar, who manifestly used it to communicate with his officials.
Thus to nix a given text we demand an integer value, known equally shift which indicates the number of position each letter of the text has been moved down.
The encryption can exist represented using modular arithmetic past offset transforming the letters into numbers, according to the scheme, A = 0, B = one,…, Z = 25. Encryption of a letter of the alphabet by a shift n tin be described mathematically as.
(Encryption Phase with shift n)
(Decryption Phase with shift n)
Examples :
Text : ABCDEFGHIJKLMNOPQRSTUVWXYZ Shift: 23 Nada: XYZABCDEFGHIJKLMNOPQRSTUVW Text : ATTACKATONCE Shift: 4 Cipher: EXXEGOEXSRGI
Algorithm for Caesar Cipher:
Input:
- A String of lower case letters, called Text.
- An Integer between 0-25 cogent the required shift.
Procedure:
- Traverse the given text 1 graphic symbol at a time .
- For each character, transform the given graphic symbol every bit per the dominion, depending on whether we're encrypting or decrypting the text.
- Return the new string generated.
Program that receives a Text (string) and Shift value( integer) and returns the encrypted text.
C++
#include <iostream>
using namespace std;
string encrypt(string text, int s)
{
cord result = "" ;
for ( int i=0;i<text.length();i++)
{
if ( isupper (text[i]))
result += char ( int (text[i]+s-65)%26 +65);
else
result += char ( int (text[i]+s-97)%26 +97);
}
return result;
}
int main()
{
string text= "ATTACKATONCE" ;
int south = four;
cout << "Text : " << text;
cout << "\nShift: " << due south;
cout << "\nCipher: " << encrypt(text, south);
return 0;
}
Java
course CaesarCipher
{
public static StringBuffer encrypt(Cord text, int s)
{
StringBuffer result= new StringBuffer();
for ( int i= 0 ; i<text.length(); i++)
{
if (Graphic symbol.isUpperCase(text.charAt(i)))
{
char ch = ( char )((( int )text.charAt(i) +
s - 65 ) % 26 + 65 );
outcome.suspend(ch);
}
else
{
char ch = ( char )((( int )text.charAt(i) +
s - 97 ) % 26 + 97 );
effect.append(ch);
}
}
return outcome;
}
public static void master(Cord[] args)
{
String text = "ATTACKATONCE" ;
int south = 4 ;
Organization.out.println( "Text : " + text);
System.out.println( "Shift : " + s);
Arrangement.out.println( "Cipher: " + encrypt(text, southward));
}
}
Python3
def encrypt(text,s):
issue = ""
for i in range ( len (text)):
char = text[i]
if (char.isupper()):
consequence + = chr (( ord (char) + s - 65 ) % 26 + 65 )
else :
result + = chr (( ord (char) + s - 97 ) % 26 + 97 )
return consequence
text = "ATTACKATONCE"
s = 4
print ( "Text : " + text)
print ( "Shift : " + str (s))
print ( "Nix: " + encrypt(text,southward))
C#
using System;
using Organization.Text;
public class CaesarCipher
{
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] +
due south - 65) % 26 + 65);
result.Suspend(ch);
}
else
{
char ch = ( char )((( int )text[i] +
south - 97) % 26 + 97);
consequence.Append(ch);
}
}
render result;
}
public static void Main(String[] args)
{
String text = "ATTACKATONCE" ;
int s = 4;
Panel.WriteLine( "Text : " + text);
Console.WriteLine( "Shift : " + s);
Panel.WriteLine( "Cypher: " + encrypt(text, s));
}
}
PHP
<?php
office encrypt( $text , $s )
{
$effect = "" ;
for ( $i = 0; $i < strlen ( $text ); $i ++)
{
if (ctype_upper( $text [ $i ]))
$result = $result . chr ((ord( $text [ $i ]) +
$s - 65) % 26 + 65);
else
$event = $result . chr ((ord( $text [ $i ]) +
$south - 97) % 26 + 97);
}
return $result ;
}
$text = "ATTACKATONCE" ;
$s = 4;
repeat "Text : " . $text ;
echo "\nShift: " . $s ;
repeat "\nCipher: " . encrypt( $text , $s );
?>
Javascript
<script>
function encrypt(text, s)
{
allow outcome= ""
for (permit 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 = Cord.fromCharCode((char.charCodeAt(0) + s-97) % 26 + 97);
result += ch;
}
}
render result;
}
let text = "ATTACKATONCE" ;
let s = iv;
document.write( "Text : " + text + "<br>" );
document.write( "Shift : " + s + "<br>" );
document.write( "Cipher: " + encrypt(text, s) + "<br>" );
</script>
Output:
Text : ATTACKATONCE Shift: iv Cipher: EXXEGOEXSRGI
How to decrypt?
We can either write another role decrypt like to encrypt, that'll apply the given shift in the contrary direction to decrypt the original text. Withal we can employ the cyclic property of the cipher nether modulo , hence we can simply detect
Nil(n) = De-cipher(26-north)
Hence, we can utilize the same function to decrypt, instead we'll modify the shift value such that shift = 26-shift (Refer this for a sample run in C++).
https://www.youtube.com/lookout?v=S472gPqwF
-o
This commodity is contributed by Ashutosh Kumar. If you similar GeeksforGeeks and would like to contribute, you can also write an commodity and mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks master page and help other Geeks.
Please write comments if you find anything incorrect, or you desire to share more than information about the topic discussed above
Source: https://www.geeksforgeeks.org/caesar-cipher-in-cryptography/
0 Response to "Caesar Block Cipher C++ Reads the Standard Input"
Post a Comment