A Caesar cipher (shift cipher) is one of the simplest encryption methods. It is a Substitution Cipher that involves replacing each letter of the secret message with a different letter of the alphabet which is a fixed number of positions further in the alphabet.
Example : Apple -------------Shift By 2----------> Crrng
Now write a C++ program to Encrypt data using shift cypher .
GET More Basic Crypto Algorithms: Github
Example : Apple -------------Shift By 2----------> Crrng
Now write a C++ program to Encrypt data using shift cypher .
- Take care that small letters encrypted to small letters and capital letters are encrypted to capital letters
- character except a...z and A...Z should not encripted
- keep in mind that shift key may be from 1 to large number.
Caesar Cipher (C++)
- #include<iostream>
- #include<string>
- using namespace std;
- int main(){
- int N,i,k;
- string st; //String To Encode
- string rst="";
- cout<<"Enter String :"<<endl;
- cin>>st;
- cout<<"Enter Shift Key :"<<endl;
- cin>>k;
- //Start
- i=0;
- k=k%26;
- //If shift is greter than 26 then it repreat itself
- //Just Like angle repeat after 360 degree
- int temp;
- while(i<st.size()){
- temp=int(st[i]);
- if(temp>96 && temp<123){
- if((temp+k)<123){
- temp=temp+k;
- }
- else{
- temp=96+(k-(122-temp));
- }
- }
- else if(temp>64 && temp< 91){
- if((temp+k)<91){
- temp=temp+k;
- }
- else{
- temp=64+(k-(90-temp));
- }
- }
- i++;
- rst +=char(temp);
- }
- cout<<rst<<endl;
- return 0;
- }
Python 2.7 (Using Modular Approach)
- #Shift Cipher
- # Additive Cipher
- #********************************************************
- def shift_cipher(st,key):
- '''
- It return shift cipher encoded string
- It does not Encode any char
- Not in range A..Z or a..z and 0..1
- '''
- numkey=key%10
- key=key%26
- rst=''
- for ch in st:
- asciiV=ord(ch)
- if asciiV>64 and asciiV<91:
- rst +=chr(((asciiV-64)+key)%26 +64)
- elif asciiV>96 and asciiV<123:
- rst +=chr(((asciiV-96)+key)%26 +96)
- elif asciiV>47 and asciiV<58:
- rst +=chr(((asciiV-47)+key)%10 +47)
- else:
- rst +=ch
- return rst
- if __name__=='__main__':
- st=raw_input('Enter String To encode : ')
- key =int(raw_input('Key ?? :'))
- print str(shift_cipher(st,key))
No comments:
Post a Comment
THANKS FOR UR GREAT COMMENT