我一直在尝试用C创建一个密码生成器。
除了实际的加密循环计数器之外,它们都完全正常工作。它总是停在39左右。强制它进一步只会导致出现一些随机的ASCII符号,而不是A-Z。 (对不起,我不能特别好地描述它。)
#include <stdio.h>
#include <stdlib.h> /* For exit() function*/
#include <time.h>
#define SIZE 26
int main(){
char plainAlphabet[27] = {'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',' '};
char cipherAlphabet[27];
char plainText[] = "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG";
int x = 0;
int c = 0;
int numbers[SIZE];
int i, n, tmp;
srand(time(NULL));
// Initialize the array
for(i = 0;i < SIZE;++i)
numbers[i] = i;
// Shuffle the array
for(i = 0;i < SIZE;++i)
{
n = rand() % SIZE;
tmp = numbers[n];
numbers[n] = numbers[i];
numbers[i] = tmp;
}
// Iterate through the array. Your numbers are already random
for(i = 0;i < SIZE;++i){
cipherAlphabet[i] = plainAlphabet[numbers[i]];
};
int a = 0;
x = 0;
i = 0;
int z = strlen(plainText);
printf("%d\n",z);
for(plainText[x] != "^";c < z + 100; c++){
if(plainAlphabet[c] == plainText["%d",x]){
printf("%c",cipherAlphabet[c]);
a = a + 1;
x = x + 1;
c = 0;
};
};
printf("%d",c);
printf("%d",z);
scanf("%d", tmp);
return 0;
}
(对不起,如果它看起来很明显和/或重复。)
答案 0 :(得分:0)
终于来了!它应该工作!您的主要/最大错误是在上一个c
循环中将0
设置为for
!
因为在您将其设置为0
后,它会增加1
,但在您的plainAlphabet 'A'
中会index 0
!因此,如果您的文字包含A's
,则该计划会停止,因为c
它永远不会得到0
而A
会在您的plainAlphabet中找到神经! (解决方案:在我的示例c
中将c设置为-1 |为arrayCount
)
(你的代码中也有一些奇怪的错误!)
#include <stdio.h>
#include <stdlib.h> /* For exit() function*/
#include <time.h>
#include <string.h>
#define SIZE 26
int main() {
char plainAlphabet[27] = {'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', ' '};
char cipherAlphabet[50];
char plainText[255] = "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG";
int arrayCount, randomNumber, swap, charCount = 0, strLength = strlen(plainText);
int numbers[SIZE];
srand(time(NULL));
// Initialize the array
for(arrayCount = 0; arrayCount < SIZE; arrayCount++)
numbers[arrayCount] = arrayCount;
// Shuffle the array
for(arrayCount = 0; arrayCount < SIZE; arrayCount++) {
randomNumber = rand() % SIZE;
swap = numbers[randomNumber];
numbers[randomNumber] = numbers[arrayCount];
numbers[arrayCount] = swap;
}
// Iterate through the array. Your numbers are already random
for(arrayCount = 0; arrayCount < SIZE; arrayCount++)
cipherAlphabet[arrayCount] = plainAlphabet[numbers[arrayCount]];
printf("String Length: %d\n\n", strLength);
printf("Encrypted Text: \n");
charCount = 0;
for(arrayCount = 0; arrayCount < strLength; arrayCount++) {
if(plainAlphabet[arrayCount] == plainText[charCount] && plainText[arrayCount] != '^') {
printf("%c", cipherAlphabet[arrayCount]);
charCount += 1;
arrayCount = -1;
}
if(plainText[charCount] == '\0')
break;
}
int a = 0; //If this variable is commented out, the program gives other results
printf("\n\n");
system("pause");
return 0;
}