所以我試圖做一個文件用C加密程序(由我是那種方式新C)所以我寫了這個簡單的XOR文件加密代碼:我的C加密程序有什麼問題?
#include <stdio.h>
#include <string.h>
int main()
{
char fileName1[35] = {'\0'}; //Name of original file
char fileName2[35] = {'\0'}; //Name of encrypted file
char keyString[20] = {'\0'}; //Key determines result encryption
FILE* originalFile; //File to be encrypted
FILE* cryptedFile; //The encrypted file
int c; //byte read from original file
printf("Enter file location followed by name of the file you want to encrypt: ");
scanf("%s", fileName1);
printf("Enter file location followed by name of the encrypted file: ");
scanf("%s", fileName2);
printf("Enter your key (Encryption changes based on Key): ");
scanf("%s", keyString);
originalFile = fopen(fileName1, "rb"); //rb to read file bytes
cryptedFile = fopen(fileName2, "wb"); //wb to write bytes to file
if(originalFile != NULL && cryptedFile != NULL){
while((c = getc(originalFile)) != EOF){
int x;
for(x=0; x<strlen(keyString); x++){
c ^= keyString[x];
putc(c, cryptedFile);
}
}
fclose(originalFile);
fclose(cryptedFile);
}
return 0;
}
所以要測試這個程序我創建了一個名爲file1.txt的文件,並運行加密程序,將第二個文件作爲file2.txt,並輸入密碼爲,祕密爲。然後我再次運行該程序,但是這次是在加密的file2.txt文件中創建了一個file3.txt,其密鑰爲,密鑰爲。由於它是相同的密鑰,file3.txt應該與file1.txt相同,但是file3.txt中包含隨機內容。那麼我做錯了什麼?
謝謝,我不知道我是如何錯過這樣的問題 – user1546022 2012-08-03 11:56:02