This article is aimed at the data structure Basic Series Network course (4): String Practice Project.
"Project-String Encryption"
A text string can be encrypted using a pre-compiled character mapping table. For example, set the character mapping table to:
abcdefghijklmnopqrstuvwxyzngzqtcobmuhelkpdawxfyivrsj
The string "Lao he jiao Shu Ju Jie Gou" is encrypted as "ENP BT UMNP Xby uy umt opy".
Design a program to implement encryption, decryption algorithm, the input text is encrypted after the output, and then decrypted and output.
[Refer to answer] (header file sqstring.h see sequential string algorithm library)
#include <stdio.h>#include "sqString.h"Sqstring A, B;//For storing character mapping tablesSqstring EnCrypt (sqstring p) {int i=0, J; Sqstring q; while(I<p.length) { for(j=0; P.DATA[I]!=A.DATA[J]; j + +);if(J>=p.length)//P.data[i not found in string a)Q.data[i]=p.data[i];Else //Find the p.data[i] letter in a stringQ.DATA[I]=B.DATA[J]; i++; } q.length=p.length;returnQ;} Sqstring unencrypt (sqstring q) {int i=0, J; Sqstring p; while(I<q.length) { for(j=0; Q.DATA[I]!=B.DATA[J]; j + +);if(J>=q.length)//Q.data[i not found in string b)P.data[i]=q.data[i];Else //Find the Q.data[i] letter in string bP.DATA[I]=A.DATA[J]; i++; } p.length=q.length;returnP;} int main () {sqstring p,q; Strassign (A,"ABCDEFGHIJKLMNOPQRSTUVWXYZ");//Create a stringStrassign (B,"NGZQTCOBMUHELKPDAWXFYIVRSJ");//Build B stringChar Str[maxsize];printf("\ n");printf("input source string:"); Gets (str);//Get the source string of the user inputStrassign (P,STR);//Build P string printf("Encryption decrypted as follows: \ n");printf("source string:"); DISPSTR (P); Q=encrypt (P);//p string encryption generates Q string printf("Encrypt string:"); DISPSTR (q); P=unencrypt (q);//q string decryption generates P-string printf("decryption string:"); DISPSTR (P);printf("\ n");return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Data structure Practice--string encryption