I need a simple code for the shift cipher
i find this wb page , it explains the encryption and decryption
process but i dont know how to implement this in Matlab
https://www.khanacademy.org/computing/computer-science/cryptography/ciphers/a/shift-cipher
Please write the code and i need in 2 hours
Caesar's cypher is the simplest encryption algorithm. It adds a fixed value to the ASCII (unicode) value of each character of a text. In other words, it shifts the characters. Decrypting a text is simply shifting it back by the same amount,
Process Plain Text
function output = encCsar(word,key)
numWord = uint8(word);
numKey = uint8(lower(key)) - 96;
for i = 1:numel(numWord)
if ( numWord>=65 & numWord<=90 )
numWord=mod(numWord-65+numKey,26);
output(i,1)=char(numWord+65);
elseif ( numWord>=97 & numWord<=122 )
numWord=mod(numWord-97+numKey,26);
output(i,1)=char(numWord+97);
else
error('Please enter a string and a number key.')
end
end
Pseudo Code for Caesar Cipher
try once understanding in java
import java.util.*; public class CaesarCipherProgram { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println(" Input the plaintext message : "); String plaintext = sc.nextLine(); System.out.println(" Enter the value by which each character in the plaintext message gets shifted : "); int shift = sc.nextInt(); String ciphertext = ""; char alphabet; for(int i=0; i < plaintext.length();i++) { // Shift one character at a time alphabet = plaintext.charAt(i); // if alphabet lies between a and z if(alphabet >= 'a' && alphabet <= 'z') { // shift alphabet alphabet = (char) (alphabet + shift); // if shift alphabet greater than 'z' if(alphabet > 'z') { // reshift to starting position alphabet = (char) (alphabet+'a'-'z'-1); } ciphertext = ciphertext + alphabet; } // if alphabet lies between 'A'and 'Z' else if(alphabet >= 'A' && alphabet <= 'Z') { // shift alphabet alphabet = (char) (alphabet + shift); // if shift alphabet greater than 'Z' if(alphabet > 'Z') { //reshift to starting position alphabet = (char) (alphabet+'A'-'Z'-1); } ciphertext = ciphertext + alphabet; } else { ciphertext = ciphertext + alphabet; } } System.out.println(" ciphertext : " + ciphertext); } }
output:
Input the plaintext message : Java Hungry Blog
Enter the value by which each character in the plaintext message
gets shifted : 2
ciphertext : Lcxc Jwpita Dnqi
Decryption
import java.util.*; public class CaesarCipherProgram { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println(" Input the ciphertext message : "); String ciphertext = sc.nextLine(); System.out.println(" Enter the shift value : "); int shift = sc.nextInt(); String decryptMessage = ""; for(int i=0; i < ciphertext.length();i++) { // Shift one character at a time char alphabet = ciphertext.charAt(i); // if alphabet lies between a and z if(alphabet >= 'a' && alphabet <= 'z') { // shift alphabet alphabet = (char) (alphabet - shift); // shift alphabet lesser than 'a' if(alphabet < 'a') { //reshift to starting position alphabet = (char) (alphabet-'a'+'z'+1); } decryptMessage = decryptMessage + alphabet; } // if alphabet lies between A and Z else if(alphabet >= 'A' && alphabet <= 'Z') { // shift alphabet alphabet = (char) (alphabet - shift); //shift alphabet lesser than 'A' if (alphabet < 'A') { // reshift to starting position alphabet = (char) (alphabet-'A'+'Z'+1); } decryptMessage = decryptMessage + alphabet; } else { decryptMessage = decryptMessage + alphabet; } } System.out.println(" decrypt message : " + decryptMessage); } }
output:
Input the ciphertext message : Lcxc Jwpita Dnqi
Enter the shift value : 2
decrypt message : Java Hungry Blog
Get Answers For Free
Most questions answered within 1 hours.