Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

encryption

What is Caesar Cipher code?

Rehan Butt

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code

Caesar Cipher code

The Caesar Cipher is one of the earliest encryption techniques. The data is encrypted by replacing each letter of the message with another letter after adding an integer key. This changes the ASCII value and converts the letter of the message into another letter.

Encryption rotation

The transformation of the data can be seen in the table below. We encrypt the data by adding a key n that is 23, which right rotates the letter to 23 places and assigns the value.

The result of the encrypted message looks like the following:

Plain

Caesar Cipher

A

X

B

Y

C

Z

D

A

E

B

F

C

G

D

H

E

I

F

J

G

K

H

L

I

M

J

N

K

O

L

P

M

Q

N

R

O

S

P

T

Q

U

R

V

S

W

T

X

U

Y

V

Z

W

Example

Plaintext: hello world
Ciphertext: ebiilQtloia

Example

The following program gives an encrypted text after taking a string and a shift value:

#include<iostream>
using namespace std;
string encrypt(string message,int key)
{
string enc_string="\0";
int i = 0;
for(;i<message.length();i++)
{
enc_string += char(int(message[i]+key-97)%26 +97);
}
return enc_string;
}
int main()
{
string message = "\0";
string encrypyted_string="\0";
int key = 23;
message = "hello world";
encrypyted_string = encrypt(message,key);
cout<<"Encrypted String : "<< encrypyted_string;
return 0;
}
Caesar Cipher encryption code

Explanation

  • Lines 6–9: We encrypt the values with the key character by character and place the new characters into another string variable called enc_string.
  • Line 12: We return the new string that is the encrypted string to the main function.
  • Lines 16 and 17: We initialize two string variables named message and encrypted_string.
  • Line 18: We initialize an integer variable named key to encrypt the string.
  • Line 19: We assign a sentence to our string variable, message.
  • Line 21: We call the function encrypt() and pass parameters to encrypt the string.
  • Line 23: We display the encrypted message on the console.

RELATED TAGS

encryption

CONTRIBUTOR

Rehan Butt
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code
Keep Exploring