加密解密,字符窜排序问题
要求是在客服端输入一段字符,将其中的小写字母按如下改变:a->b,b->c...z->a其他字符不变。在服务器端收到加密后的字符窜后按源加密规则解密。小弟不才,求高手指点~
程序代码:#include <stdio.h>
#include <string.h>
char* encrypt(char* buffer, int length) {
int i;
for (i = 0; i < length; ++i)
if (buffer[i] >= 'a' && buffer[i] < 'z')
++buffer[i];
else if (buffer[i] == 'z')
buffer[i] = 'a';
return buffer;
}
char* decrypt(char* buffer, int length) {
int i;
for (i = 0; i < length; ++i)
if (buffer[i] > 'a' && buffer[i] <= 'z')
--buffer[i];
else if (buffer[i] == 'a')
buffer[i] = 'z';
return buffer;
}
int main(void) {
char str1[] = "Hello, world!";
char str2[] = "abcdefgHijklmnopqRstuvwxyz";
int len1 = strlen(str1);
int len2 = strlen(str2);
printf("str1 encrypted: %s\n", encrypt(str1, len1));
printf("str2 encrypted: %s\n", encrypt(str2, len2));
printf("str1 decrypted: %s\n", decrypt(str1, len1));
printf("str2 decrypted: %s\n", decrypt(str2, len2));
return 0;
}
/*
str1 encrypted: Hfmmp, xpsme!
str2 encrypted: bcdefghHjklmnopqrRtuvwxyza
str1 decrypted: Hello, world!
str2 decrypted: abcdefgHijklmnopqRstuvwxyz
*/

程序代码:#include <stdio.h>
#define ASC_LEN (26)
#define ASC_INC(asc) (asc = 'a' + ((++asc-'a')%ASC_LEN))
//#define ASC_INC(asc) (asc-'a'%ASC_LEN, asc = (asc+1)) //error!!!
int main(int argc, char *argv[])
{
char s[] = "szzzlkjflskdjfsdfxcvxcvaa";
int i;
for (i = 0; i < sizeof(s) - 1; i++) {
putchar(ASC_INC(s[i]));
}
return 0;
}


程序代码:#include <stdio.h>
#define INCR(ch) ('a' + ((((ch) + 1) - 'a') % 26))
#define DECR(ch) ('a' + ((((ch) - 'a' - 1) + 26) % 26))
char* encrypt(char* buffer, int length) {
int i;
for (i = 0; i < length; ++i)
buffer[i] = INCR(buffer[i]);
return buffer;
}
char* decrypt(char* buffer, int length) {
int i;
for (i = 0; i < length; ++i)
buffer[i] = DECR(buffer[i]);
return buffer;
}
int main(void) {
char str1[] = "helloworld";
char str2[] = "abcdefghijklmnopqrstuvwxyz";
int len1 = sizeof str1 - 1;
int len2 = sizeof str2 - 1;
printf("str1 encrypted: %s\n", encrypt(str1, len1));
printf("str2 encrypted: %s\n", encrypt(str2, len2));
printf("str1 decrypted: %s\n", decrypt(str1, len1));
printf("str2 decrypted: %s\n", decrypt(str2, len2));
return 0;
}
/*
str1 encrypted: ifmmpxpsme
str2 encrypted: bcdefghijklmnopqrstuvwxyza
str1 decrypted: helloworld
str2 decrypted: abcdefghijklmnopqrstuvwxyz
Process returned 0 (0x0) execution time : 0.437 s
Press any key to continue.
*/
