0

我正在尝试将密码扰频器从 复制JavascriptC. 它的作用是获取字母的 ASCII 字符代码,将其放置,划分,然后从给定列表中抓取一个随机字符。

Javascript版本:

function getScrambledPassword(pwd) {
    var cipher = ['k', 's', 'z', 'h', 'x', 'b', 'p', 'j', 'v', 'c', 'g', 'f', 'q', 'n', 't', 'm'];
    var result="";
    if (pwd == null)
        pwd = "";
    pwd = encodeURIComponent(pwd);
    //alert("encoded password: " + pwd);
    for(var i=0;i<pwd.length;i++) {
            var cc = pwd.charCodeAt(i);
        result += cipher[Math.floor(cc/16)] + cipher[cc%16];
    }
    //alert("scrambled password: " + result);
    return result;
}

正在运行的加扰器示例:https ://jsfiddle.net/w5db66va/

到目前为止我所做的:

#include <stdio.h>
#include <math.h>
#include <string.h>

static char *scramblePassword(char *pwd)
{
    char *cipher[] = {
        "k", "s", "z", "h",
        "x", "b", "p", "j",
        "v", "c", "g", "f",
        "q", "n", "t", "m"
    };

    char *result = "";
    for(int i=0; i < strlen(pwd); i++)
    {
        int cc = (int) pwd[i];
        printf("%d", cc);
        result + cipher[floor(cc/16)] + cipher[cc%16];
    }
    return *result;
}

int main(void)
{
    char *test[] = {"test", "testtwo", "testthree"};
    for (int i=0;i < sizeof(test); i++)
    {
        printf("Original: %s", test[i]);
        printf("Scrambled: %s", scramblePassword(test[i]));
    }
}

我遇到的问题是,当我运行c文件(编译后)时,它根本不会输出任何东西。我做错了什么以至于我无法让它像我期望的那样运行?

4

3 回答 3

2

继续评论,您的问题比您最初想象的要深一些。首先,您不想cipher成为一个字符串数组,您只希望它是一个字符数组,例如:

    char cipher[] = "kszhxbpjvcgfqnm";

接下来,您不能返回在函数体内声明的数组。返回时,内存result被销毁scramblePassword。您的选择是 (1) 动态分配resultin scramblePassword(并在 中释放),或 (2) 为inmain声明存储并将其作为参数传递给. 例如:resultmainscramblePassword

#define MAX 32

static char *scramblePassword (char *pwd, char *result)
{
 ...
    return result;
}

int main(void)
{
    char result[MAX] = "";
    ...
        printf ("Scrambled: %s\n", scramblePassword (test[i], result));

最后,如果您的算法打算从中构建一个乱码字符数组,cipher将导致选择超出范围的索引,cipher从而导致未定义的行为。如果意图只是分配一个值,result[x]而不管它是否是有效的可打印 ASCII 值,那么它可能是可以的。但是,如果第一个是您的目标,则算法的结果必须始终产生一个在 的范围内的值cipher,例如:

         result[i] = cipher[((int)floor (cc / 16) + cc % 16) % sizeof cipher];

将所有这些部分放在一起,并回顾这main是类型int并因此返回一个值,您可以执行以下操作:

#include <stdio.h>
#include <math.h>
#include <string.h>

#define MAX 32

static char *scramblePassword (char *pwd, char *result)
{
    char cipher[] = "kszhxbpjvcgfqnm";
    int i;

    for (i = 0; i < (int)strlen (pwd); i++)
    {
        int cc = (int) pwd[i];
        // result[i] = cipher[(int)floor (cc / 16)] + cipher[cc % 16];
        result[i] = cipher[((int)floor (cc / 16) + cc % 16) % sizeof cipher];
    }
    result[i] = 0;  /* you MUST nul-terminate to use as a string */

    return result;
}

int main(void)
{
    char *test[] = {"test", "testtwo", "testthree"};
    char result[MAX] = "";

    for (int i = 0; i < (int)(sizeof test/sizeof *test); i++)
    {
        printf ("\nOriginal : %s\n", test[i]);
        printf ("Scrambled: %s\n", scramblePassword (test[i], result));
    }

    return 0;
}

示例使用/输出

这将导致以下内容的可读输出:

$ ./bin/pwscramble

Original : test
Scrambled: ffgf

Original : testtwo
Scrambled: ffgffmb

Original : testthree
Scrambled: ffgffmcff

我将留给您研究该算法实际上应该做什么。如果您还有其他问题,请告诉我。

于 2017-03-31T15:14:35.417 回答
2

正在发生的事情是你变得非常幸运。您的程序在 C 中调用未定义的行为。

先看这条线

result + cipher[floor(cc/16)] + cipher[cc%16];

首先,它什么也没做。那只是一个被抛弃的表达方式。你真正想要的是:

result += cipher[floor(cc/16)] + cipher[cc%16];

但它仍然行不通,因为 C 并没有真正的字符串概念。字符串实际上只是以 '\0' 结尾的字符序列。result只是指向这样一个序列的指针,并且与任何其他指针一样,当您向其中添加某些内容时,您只需增加指针指向的位置。

此外,返回*result实际上取消引用指针并返回它指向的内容。

该声明

result = "";

在某处分配一点内存,空字节序列\0以单个 nul 字节结尾。在堆栈上(或在寄存器中,取决于实现),结果被分配并给出 nul 字节的地址。

当您返回时,*result您返回 nul 字节,但调用者认为您正在返回一个指针,因此它将将该 nul 字节解释为一个指针(我很惊讶您的代码在实际编译时没有给出错误)并且该指针可能是一个空指针。

在 C 中连接字符串是一项棘手的操作。您必须使用strcat或其更安全的衍生物之一。您必须确保为结果分配足够的空间,并且必须使用 malloc 动态地执行此操作,因为当您从分配的函数中返回时,本地分配的字符串会消失。

编辑

还有一件事......

C 有不止一种数字数据类型。当您将一个整数除以另一个时,您会得到一个整数结果。如果 cc 不能被 16 整除,则结果已经是floor(cc/16)

于 2017-03-31T14:50:58.783 回答
0

C 不是 JS。

有许多微妙的问题,您的程序甚至无法编译。

你可能想要这个:

#include <stdio.h>
#include <math.h>
#include <string.h>

static char *scramblePassword(char *pwd, char *result)
{
  char cipher[] = {    // you need an array of chars here, not an
                       // array of pointers to char
    'k', 's', 'z', 'h',
    'x', 'b', 'p', 'j',
    'v', 'c', 'g', 'f',
    'q', 'n', 't', 'm'
  };

  size_t i;
  for (i = 0; i < strlen(pwd); i++)
  {
    int cc = (int)pwd[i];
    //printf("%d", cc);
    result[i] = cipher[/*(int)floor*/(cc / 16)] + cipher[cc % 16];
                           // ^ actually you can drop the floor function,
                           // there is no floating point here, so integer
                           // division will do the job
  }

  result[i] = 0;  // this will NUL terminate the string
  return result;
}

int main(void)
{
  char *test[] = { "test", "testtwo", "testthree" };
  for (int i = 0; i < sizeof(test) / sizeof(test[0]); i++)
                    // ^ the number of elements is not sizeof(test)
                    // (that's the number of bytes the array takes in memory
                    // but sizeof(test) / sizeof(test[0])
  {
    char result[100];  // you cannot return arrays in C, you need to provide
                       // the array and pass the pointer to your function
    printf("Original: %s\n", test[i]);
    printf("Scrambled: %s\n", scramblePassword(test[i], result));
  }
}
于 2017-03-31T14:53:53.300 回答