[問題] AES 實作 in c

看板C_and_CPP (C/C++)作者 (song)時間16年前 (2009/09/02 22:13), 編輯推噓1(103)
留言4則, 2人參與, 最新討論串1/1
下面的程式是這個網頁→http://www.efgh.com/software/rijndael.htm 的部分AES實作 我用gcc rijndael.c encrypt.c -o encrypt 產生執行檔 encrypt 後 執行 encrypt 1111111111111111 aaa.txt ( encrypt password cryptofile) 卻發現一直卡在 while (!feof(stdin)) 這個loop裡 (可以一直輸入明文並按enter,但還是跑不出來) 試了很久還是沒辦法解決 麻煩大家幫我看一下 謝謝 附上AES source code (code 要自己分開): http://www.efgh.com/software/rijndael.txt =============================== ENCRYPT.C =============================== #include <stdio.h> #include "rijndael.h" #define KEYBITS 256 int main(int argc, char **argv) { unsigned long rk[RKLENGTH(KEYBITS)]; unsigned char key[KEYLENGTH(KEYBITS)]; int i; int nrounds; char *password; FILE *output; if (argc < 3) { fputs("Missing argument\n", stderr); return 1; } password = argv[1]; for (i = 0; i < sizeof(key); i++) key[i] = *password != 0 ? *password++ : 0; output = fopen(argv[2], "wb"); if (output == NULL) { fputs("File write error", stderr); return 1; } nrounds = rijndaelSetupEncrypt(rk, key, 256); /*=============卡在這個while loop裡 > <" =========================*/ while (!feof(stdin)) { unsigned char plaintext[16]; unsigned char ciphertext[16]; int j; for (j = 0; j < sizeof(plaintext); j++) { int c = getchar(); if (c == EOF) break; plaintext[j] = c; } if (j == 0) break; for (; j < sizeof(plaintext); j++) plaintext[j] = ' '; rijndaelEncrypt(rk, nrounds, plaintext, ciphertext); if (fwrite(ciphertext, sizeof(ciphertext), 1, output) != 1) { fclose(output); fputs("File write error", stderr); return 1; } } fclose(output); } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 220.133.3.165 ※ 編輯: zelda312 來自: 220.133.3.165 (09/02 22:14)

09/02 22:18, , 1F
補充一下:while裡會一直getchar(抓明文來加密)
09/02 22:18, 1F

09/02 22:20, , 2F
而 rijndaelEncrypt 這個function則用來加密明文
09/02 22:20, 2F

09/02 22:57, , 3F
ctrl-z in win, ctrl-d in unix
09/02 22:57, 3F

09/02 23:44, , 4F
感謝樓上大大的解答 可以加解密了 感溫^^
09/02 23:44, 4F
文章代碼(AID): #1AddsHhx (C_and_CPP)
文章代碼(AID): #1AddsHhx (C_and_CPP)