Simple encryption and decryption implementations---affine passwords (c + + uses string)

Source: Internet
Author: User

Using the String class in C + +, there is no limit to the length of the array compared to using the array, and the operation is convenient for adding

#include  <iostream> #include  <string>using namespace std;string jiami ( STRING&NBSP;STR,INT&NBSP;K,INT&NBSP;B); String jiemi (string pass,int k,int b);int  Canshu (int k,int b); Int main () {    string str;      //plaintext     string pass;    //ciphertext     string  res;        //Clear     int k,b;         //parameters of the cryptographic algorithm     cout<< "Please enter clear text:";     cin>>str;    cout<< "Please enter the parameters of the cryptographic algorithm (K&NBSP;B):";     cin>>k>>b;    while (!canshu (k,b))     {         cout<< "parameter is not in law (the parameters should be the prime number of mod 26), please re-enter:";         cin>>k>>b;    }    cout<< "encryption function: F (x) =" <<k<< "x+" << B<<endl;    pass=jiami (str,k,b);    cout<< "ciphertext:" << Pass<<endl;    res=jiemi (pass,k,b);    cout<< "decrypted plaintext after:" < <res<<endl;    return 0;} Int canshu (int k,int b) {    int c,t,mod = 26;     //die  26    if (k==1 | |  k%2==0)         return 0;    if (mod<k )     {        t=mod;         mod=k;        k=t;    }     while (c!=0)     {        c=mod%k;         mod=k;        k=c;     }    if (mod==1)         return  1;    else         return 0;} String jiami (string str,int k,int b) {    int i;     cout<< "\ n input the plaintext required to be encrypted:" <<str<<endl;    for (I=0;i<str.length (); i+ +)     {        int a;         a= (str[i]-97) *k+b;    //encryption          if (a>25)     //control for 0-25     mod to 26         {            do      //Guaranteed not to exceed domain             {                 a=a-26;                 }while (a>25);             a=a+97;        //the ASC code obtained after encryption;             str[i]= (char) a;         }        else         {            a=a+97;             str[i]= (char) a;         }    }        return  STR;} String jiemi (String&NBSP;PASS,INT&NBSP;K,INT&NBSP;B) {    int i;    for (i=0;i< Pass.length (); i++)                  //decryption Process     {        int d;         d= (pass[i]-97)-b;          while (d%k!=0)         {             d=d+26;        }         d=d/k;        pass[i]= (char) (d+97);     }    return pass;}


This article is from the "Creative Pilgrim" blog, so be sure to keep this source http://dearch.blog.51cto.com/10423918/1750314

Simple encryption and decryption implementations---affine passwords (c + + uses string)

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.