使用少量字符串操作加密,仅使用<stdio.h>加密

时间:2015-04-26 13:44:58

标签: c arrays encryption char

我试图只使用一个库,但我遇到了一些问题。该计划应执行以下操作:

  • 它应该具有加密功能,另一个用于解密。
  • 字符串必须是8个字母或字母(A-Z 0-9
  • 这个过程应该是这样的:

    • 答: 将给定的字符串向后移动2个字符
      之前:abcdefg
      之后:cdefgab

    • B:反转A功能的结果。
      之前:cdefgab
      之后:bagfedc

    • C:交换使用不成对字符的字符对 之前:ereaeedf
      之后:reaeeefd

    • D:浏览加密表 两个数组[Original][Encrypted]
      取字符串并将其除以字符,每个字符在表格中运行并取其原始位置(A = 1,B = 2,C = 3等等)并转到[加密]数组并取出其中的字符同样的立场。

      array[Original] = {"A","B","C","D","D","F");
      array[Encrypted]= {"%","a","h","k","c","^");
      

      之前:FACD
      之后:^%hk

我现在的问题是Enc_Table,它无法正常工作。 我不知道为什么。我尝试了一些没有任何问题的东西。

我的代码来源:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define charsize 8

bool isStringEncrypted(char[]);
void Encryption();
char* Rotate_R(char str[]);
char* Reverse(char str[]);
char* Swap(char str[]);
char* Enc_Table(char str[]);
int getPos(const char *array, size_t size, char c);

void main() {

    int choose = 0;
    while (choose != 3){
        printf("Menu: \n\n  Please choose an option: \n\n 1 - Encrypt \n 2 - Decrypt [Not Implemented Yet] \n 3 - Exit \n\nYour selection ? :");
        scanf("%d", &choose);
        switch (choose)                                
        {

            case 1:
                Encryption();
                break;

            case  2:
                //Decryption();
                break;

            case 3:
                break;

            default:
                printf("Wrong choice, try again!\n");

        }

    }
}


void Encryption(){

    char str[charsize];
    char result[charsize];
    do {
        printf("please enter the string with 8 characters\n =>");
        scanf("%s", str);
        printf("\n Variable Str Value  = %s \n", str);
    } while (isStringEncrypted(str) == false);


    printf("Swap = %s \n", Swap(str));
    printf("Rotate_R =  %s \n", Rotate_R(str));
    printf("Reverse = %s \n", Reverse(str));
    printf("Enc_Table = %s \n", Enc_Table(str));
    printf("\nEncrypted String : %s \n", Enc_Table(Reverse(Rotate_R(Swap(str)))));
    scanf("%s", result);
}


bool isStringEncrypted(char str[])
{
    int i = 0;
    int Letters = 0;
    int Numbers = 0;


    unsigned int len = 0;
    while (*(str + len)) len++;

    if (len != charsize)
    {
        printf(" \n %d = len != %d = Size \n", len, charsize);
        return false;
    }


    while (str[i] != NULL && i < charsize) {
        if (str[i] >= 'A' && str[i] <= 'Z' || str[i] >= '0' && str[i] <= '9') {

            if (str[i] >= 'A' && str[i] <= 'Z')
                Letters++;
            if (str[i] >= '0' && str[i] <= '9')
                Numbers++;
        }

        i++;
    }

    if (i == charsize && (Letters >= 2) && (Numbers >= 2))
        return true;
    else return true;
}

char* Rotate_R(char str[])
{
    char x, y;
    int i;
    x = str[7];
    y = str[6];

    for (i = 5; i>-1; i--)
    {
        str[i + 2] = str[i];
    }
    str[0] = y;
    str[1] = x;

    return str;
}

char* Swap(char str[])
{
    int i;
    char Temp;

    for (i = 0; i < charsize; i += 2)
    {
        Temp = str[i];
        str[i] = str[i + 1];
        str[i + 1] = Temp;
    }

    return str;
}

char* Reverse(char str[])
{
    unsigned int lens = 0;
    while (*(str + lens)) lens++;
    char temp;
    size_t len = lens - 1;
    size_t i;
    size_t k = len;

    for (i = 0; i < len; i++)
    {
        temp = str[k];
        str[k] = str[i];
        str[i] = temp;
        k--;
        if (k == (len / 2))
        {
            break;
        }
    }

    return str;
}

char* Enc_Table(char str[])
{

    int i;
    unsigned int lens = 0;
    while (*(str + lens)) lens++;

    char dec[38] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '\0' };
    char enc[38] = { 'S', '5', 'P', 'W', 'A', 'R', '8', 'Q', 'I', 'V', 'B', '0', 'D', 'Z', '3', 'C', '2', 'H', '4', '1', 'K', '7', 'G', 'Y', '6', 'O', 'J', 'E', 'U', 'N', 'F', '9', 'M', 'L', 'T', 'X', '\0' };

    for (i = 0; i < charsize; i++)
    {
        int index = getPos(dec, sizeof(dec), str[i]);
        str[i] = enc[index];
        i++;
    }

    return str;
}

int getPos(const char *array, size_t size, char c)
{
    for (size_t i = 0; i < size; i++)
    {
        if (array[i] == c)
            return (int)i;
    }
    return -1;
}

1 个答案:

答案 0 :(得分:2)

加密表应该像这样工作:

void table(char *str, int forward)
{
    const char dec[36] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    const char enc[36] = "S5PWAR8QIVB0DZ3C2H41K7GY6OJEUNF9MLTX";

    const char *from = forward ? dec : enc;
    const char *to   = forward ? enc : dec;

    for (int i = 0, len = strlen(str); i < len; i++)
    {
        for (int n = 0; n < 36; n++)
        {
            if (str[i] == from[n])
            {
                str[i] = to[n];
                break;
            }
        }
    }
}

int main()
{
    char s[100];
    strcpy(s, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");

    table(s, 1);// encryption
    printf("%s\n", s);
    table(s, 0);// decryption
    printf("%s\n", s);
    return 0;
}

与strcspn相同的功能:

void table(char *str, int forward)
{
    const char *dec = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    const char *enc = "S5PWAR8QIVB0DZ3C2H41K7GY6OJEUNF9MLTX";

    const char *from = forward ? dec : enc;
    const char *to   = forward ? enc : dec;

    char buf[2] = { 0 };
    for (int i = 0, len = strlen(str); i < len; i++)
    {
        buf[0] = str[i];
        int pos = strcspn(from, buf);
        if (pos < 36)
            str[i] = to[pos];
    }
}

我们在这里时,快速旋转功能:

void rotate(char *str, int rotate, int direction /*1=rotate-right, 0=rotate-left*/)
{
    int length = strlen(str);
    if (!rotate || rotate >= length)
        return;

    int offset = direction ? (length - rotate) : rotate;

    char *left  = _strdup(str);
    char *right = _strdup(str + offset);

    //swap left and right to finish rotate 
    strcpy(str, right);
    strncat(str, left, offset);

    free(left);
    free(right);
}