C + + implementation of simple encryption and decryption

Source: Internet
Author: User

Substitution is an operation commonly used in the encryption process. You can design a simple replacement as follows: The uppercase letter A~z in alphabetical order into a circle, followed by the letter A after the letter A, for any one of the uppercase letters to be replaced, after the nth letter to replace it, that is, complete the replacement process. For example, when n=2, A is replaced by C, B is replaced by D, C is replaced by E, ..., y is replaced by Z, X is replaced by a, z is replaced by B. For lowercase letters, numbers can also be processed by analogy. Requires that you write a program that the user enters N (similar to a key) and replaces the user-entered string (similar to plaintext, composed of uppercase and lowercase letters) by letter to output (the output is similar to redaction). Think about how to make this program both complete the replacement (the encryption process) and complete the reverse replacement (decryption process).

#include <iostream.h>

#include <string.h>

void Main ()

{

Char str[100];

int n,c,i;

cout<< "Please enter a string:" <<endl;

CIN >> str;

cout<< "Please enter the key:" <<endl;

CIN >>n;

cout<< "If you need encryption, please enter 1" <<endl;

cout<< "If you need to decrypt, please enter 2" <<endl;

cin>>c;

if (c==1)

{

for (I=0;i<strlen (str); i++)

{

str[i]+= N;

if (str[i]> ' Z ' | | (str[i]> ' Z ' &&str[i]< ' a '))

str[i]-= (' z '-' a ') +1;

}

cout<<str<<endl;

}

else if (c==2)

{

for (I=0;i<strlen (str); i++)

{

str[i]-= N;

if (str[i]< ' A ' | | (str[i]> ' Z ' &&str[i]< ' a '))

str[i]+= (' z '-' a ') +1;

}

cout<<str<<endl;

}

}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.