"ACM International College Student Program Design Competition"--6.11

Source: Internet
Author: User

PKU 1107:

Description

Weird Wally ' s Wireless Widgets, Inc. manufactures an eclectic assortment of small, Wireless, network capable devices, rang ing from the dog collars, to pencils, to fishing bobbers. All these devices has very small memories. Encryption algorithms like Rijndael, the candidate for the Advanced Encryption Standard (AES) is demonstrably secure but They don ' t fit in such a tiny memory. In order to provide some security for transmissions to and from the devices, WWWW uses the following algorithm, which is to implement.
Encrypting a message requires three integer keys, K1, K2, and K3. The letters [A-i] form one group, [J-r] a second group, and everything else ([s-z] and underscore) the third group. Within each group the letters is rotated left by Ki positions in the message. Each group was rotated independently of the other. Decrypting the message means doing a right rotation by Ki positions within each group.
Consider the message The_quick_brown_fox encrypted with Ki values of 2, 3 and 1. The encrypted string is _icuo_bfnwhoq_kxert. The figure below shows the decrypting right rotations for one character in each of the three character groups. Looking at all the letters in the group [a-i] we see {i,c,b,f,h,e} appear at positions {2,3,7,8,11,17} within the Encrypte D message. After a right rotation of k1=2, these positions contain the letters {H,E,I,C,B,F}. The table below shows the intermediate strings that come from doing all the rotations in the first group and then all Rotatio NS in the second group and then all the rotations in the third group. The rotating letters in one group would not be the change to any letters in any of the other groups. All input strings contain only lowercase letters and underscores (_). Each string would be at the most characters long. The ki is all positive integers in the range 1-100.

The main topic: Give a way of encryption, now give clear text let you decrypt the ciphertext. The details of this encryption method are, for a ciphertext string, divide it into three groups and then correspond to three keys K1, K2, K3. The encryption process is a rotation process, with the first set as an example, whose character set is {a,b,c,d}, and the corresponding location collection is {2,4,6,7} The elements of these two sets form a one by one map, set the key K1 = 2, then complete the left rotation K1 times for the character set, and the position set is the same, that is, after the encryption character set programming {C, D, a, b}. (You can also refer to the explanation given in the topic description). The three sets of strings are rotated separately, and then the one by one mappings based on the character set and position set are followed to get clear text.

Analysis:

Data:

Obviously we should set up three string arrays to record three sets of characters separately, and for each character, it is very important to understand the way it is positioned in the string by understanding the encryption method, so here we should build the structure to record each character parameter.

Decryption method: Can see, this encryption method is very simple to reveal the way, considering that encryption is the completion of the left rotation, decryption is naturally rotated to the right.

Program Language Implementation decryption: Here we first use an empty string of STR to record the decrypted ciphertext, now we have the first set of character set c = {c1,c2,c3,c4...cn}, and the location of the one by one mapping with the set num = {Num1,num2,num3...numn}, the key is K1 , it iterates through the NUM array, like the corresponding relationship:

Str[numi + K1] = ci.

Also for the second group and the third group, it is the same puzzle idea.

The next step is the simulation programming implementation.

#include <cstdio>#include<cstring>using namespacestd;Const intMAXN = -; structele{CharC; intp;};intMain () {CharSTR[MAXN]; intK1, K2, K3; structele GROUP1[MAXN], GROUP2[MAXN], GROUP3[MAXN];  while(SCANF ("%d%d%d", &AMP;K1,&AMP;K2,&AMP;K3)! =EOF)          {GetChar (); scanf ("%s", str); intindex1, Index2, index3; Index1=0; Index2=0; Index3=0;  for(inti =0; i < strlen (str); i++)          {               if(Str[i] >='a'&& Str[i] <='I') GROUP1[INDEX1].P= i, group1[index1++].c =Str[i]; Else if(Str[i] >='J'&& Str[i] <='R') GROUP2[INDEX2].P= i, group2[index2++].c =Str[i]; ElseGROUP3[INDEX3].P= i, group3[index3++].c =Str[i]; }//grouping and recording parameters for characters//decryption             for(inti =0; I < index1;i++)           {                intj = i +K1; if(J >= Index1) J%= Index1;//because it's a rotation, don't forget to take the remainder.STR[GROUP1[J].P]=group1[i].c; }            for(inti =0; I < index2;i++)           {                intj = i +K2; if(J >= Index2) J%=Index2; STR[GROUP2[J].P]=group2[i].c; }            for(inti =0; I < index3;i++)           {                intj = i +K3; if(J >= Index3) J%=index3; STR[GROUP3[J].P]=group3[i].c; } printf ("%s\n", str); }}

"ACM International College Student Program Design Competition"--6.11

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.