字符串加密和解密

    技术2024-07-13  66

    实现字符串加密和解密

    #define KEY 10 //偏移量/密钥 /** *加密传入的字符串 *参数1:要加密的字符串 *返回:返回加密后的字符串 */ char *encrypt(char []); /** *解密传入的字符串 *参数1:要解密的字符串 *返回:返回解密后的字符串 */ char *decrypt(char []); #include <stdio.h> #include <string.h> int main() { char password[50] = "ABCDEFG"; encrypt(password); printf("加密后的字符串为:%s\n",password); decrypt(password); printf("加密后的字符串为:%s\n",password); return 0; } char *encrypt(char password[]) { int count = strlen(password); for(int i=0;i<count;i++) { // 加密方式:将字符串中每个字符加上它在字符串中的位置(自己的位置)和一个偏移量10 password[i] = password[i] +KEY; } return password; // 字符串最后的\0是否需要替换?-不需要 } char *decrypt(char password[]) { int count = strlen(password); for(int i=0;i<count;i++) { password[i] = password[i] - KEY; } return password; }
    Processed: 0.011, SQL: 9