如何解决不执行下一个任务自动调用退出函数
参考我的 previous question,我只需要更改大部分代码并使用函数调用。现在文件可以按预期读取和打印,但我注意到当它转到预期的 fileReader 并打印文件后,它通过调用 choiceReader
(case: else ) 函数自动退出这不是要调用的预期函数。我的期望是 algorithm()
将是下一个调用。请问我又在这里错过了什么?
新代码是:
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int success = 0;
char counter[1000];//used to monitor the read file length
int c;
size_t cipher_len; // size_t is sizeof(type)
unsigned char* ciphertext;
unsigned char plainpassword[] = "00000";
unsigned char* password = &plainpassword[0];
unsigned char salt[8];
unsigned char key[16];
unsigned char iv[16];
FILE *fp;
FILE *fptr;
char* plaintext = "To be or not to be is the question.";
char dict[] = "0123456789ABCDEFGHIJKLMnopQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
int decryptedtext_len,ciphertext_len,dict_len;
char* ciphertext_base64;
unsigned char *plaintexts;
int len;
int plaintext_len;
void handleOpenSSLErrors(void)
{
ERR_print_errors_fp(stderr);
abort();
}
unsigned char* decrypt(unsigned char *ciphertext,int ciphertext_len,unsigned char *key,unsigned char *iv ){
EVP_CIPHER_CTX *ctx;
unsigned char* plaintext = malloc(ciphertext_len);
bzero(plaintext,ciphertext_len);
/* Create and initialise the context */
if(!(ctx = EVP_CIPHER_CTX_new())) handleOpenSSLErrors();
/* Initialise the decryption operation. IMPORTANT - ensure you use a key
* and IV size appropriate for your cipher
* IV size for *most* modes is the same as the block size. For AES this
* is 128 bits */
if(1 != EVP_Decryptinit_ex(ctx,EVP_aes_128_cbc(),NULL,key,iv))
handleOpenSSLErrors();
EVP_CIPHER_CTX_set_key_length(ctx,EVP_MAX_KEY_LENGTH);
/* Provide the message to be decrypted,and obtain the plaintext output.
* EVP_DecryptUpdate can be called multiple times if necessary
*/
if(1 != EVP_DecryptUpdate(ctx,plaintext,&len,ciphertext,ciphertext_len))
handleOpenSSLErrors();
plaintext_len = len;
/* Finalise the decryption. Further plaintext bytes may be written at
* this stage.
*/
// return 1 if decryption successful,otherwise 0
if(1 == EVP_DecryptFinal_ex(ctx,plaintext + len,&len))
success = 1;
plaintext_len += len;
/* Add the null terminator */
plaintext[plaintext_len] = 0;
/* Clean up */
EVP_CIPHER_CTX_free(ctx);
//delete [] plaintext;
return plaintext;
}
size_t calcDecodeLength(char* b64input) {
size_t len = strlen(b64input),padding = 0;
if (b64input[len-1] == '=' && b64input[len-2] == '=') //last two chars are =
padding = 2;
else if (b64input[len-1] == '=') //last char is =
padding = 1;
return (len*3)/4 - padding;
}
void Base64Decode( char* b64message,unsigned char** buffer,size_t* length) {
BIO *bio,*b64; // A BIO is an I/O strean abstraction
int decodeLen = calcDecodeLength(b64message);
*buffer = (unsigned char*)malloc(decodeLen + 1);
(*buffer)[decodeLen] = '\0';
bio = BIO_new_mem_buf(b64message,-1);
b64 = BIO_new(BIO_f_base64());
bio = BIO_push(b64,bio);
//BIO_set_flags(bio,BIO_FLAGS_BASE64_NO_NL); //Do not use newlines to flush buffer
*length = BIO_read(bio,*buffer,strlen(b64message));
BIO_free_all(bio);
}
void initAES(const unsigned char *pass,unsigned char* salt,unsigned char* key,unsigned char* iv )
{
//initialisatio of key and iv with 0
bzero(key,sizeof(key));
bzero(iv,sizeof(iv));
EVP_BytesToKey(EVP_aes_128_cbc(),EVP_sha1(),salt,pass,strlen(pass),1,iv);
}
int checkPlaintext(char* plaintext,char* result){
int length = 10; // we just check the first then characters
return strncmp(plaintext,result,length);
}
//this tries to crack the password.
int algorithm(){
time_t begin = time(NULL);
for(int i=0; i<dict_len; i++)
for(int j=0; j<dict_len; j++)
for(int k=0; k<dict_len; k++)
for(int l=0; l<dict_len; L++)
for(int m=0; m<dict_len; m++){
*password = dict[i];
*(password+1) = dict[j];
*(password+2) = dict[k];
*(password+3) = dict[l];
*(password+4) = dict[m];
initAES(password,iv);
unsigned char* result = decrypt(ciphertext,cipher_len,iv);
if (success == 1){
if(checkPlaintext(plaintext,result)==0){
printf("Password is %s\n",password);
time_t end = time(NULL);
printf("Time elpased is %ld seconds",(end - begin));
return 0;
}
}
free(result);
}
}
// this given cipher text
int firstReader(){
if ((fp = fopen("givenfile.txt","r")) == NULL) {
printf("Error! File cannot be opened.");
exit(1);
}
fscanf(fp,"%[^\n]",counter);
printf("Cipher text is:\n%s",counter);
printf("\nCracking cipher text one in progress ....\n");
algorithm();//calls the algorithm function
}
int secondReader(){
if ((fptr = fopen("cipher1.txt","r")) == NULL) {
printf("Error! File cannot be opened.");
// Program exits if the file pointer returns NULL.
exit(1);
}
fscanf(fptr,counter);
printf("\nCracking cipher text tow in progress ....\n");
algorithm(); // calls the algorithm function
}
// reads user's choice and performs the next operation accordingly
void choiceReader(){
printf("User's application guide.\nIst cipher decryption = 1\nSecond cipher decryption = 2\nQuit = Any other input\n");
scanf("%d",&c);
if(c==1){
firstReader();
}
if(c==2){
secondReader();
}
else{
printf("Application Exited ...");
exit(1);
}
}
int main (void)
{
//the core function that controls the user's action.
choiceReader();
dict_len = strlen(dict);
if (c==1){
ciphertext_base64 = (char*) fp;
}
if (c==2){
ciphertext_base64 = (char*) fptr;
}
printf(ciphertext_base64);
ERR_load_crypto_strings();
Base64Decode(ciphertext_base64,&ciphertext,&cipher_len);
// retrive the slater from ciphertext (binary)
if (strncmp((const char*)ciphertext,"Salted__",8) == 0) { // find the keyword "Salted__"
memcpy(salt,&ciphertext[8],8);
ciphertext += 16;
cipher_len -= 16;
}
// Clean up
fclose(fptr);
EVP_cleanup();
ERR_free_strings();
return 0;
}
输出截图如下:
解决方法
你写道:
if(c==1){
firstReader();
}
if(c==2){
secondReader();
}
else{
printf("Application Exited ...");
exit(1);
}
意思是:
- 如果 c 为 1,则调用 firstReader。
- 如果 c 为 2,则调用 secondReader,否则退出。
如果 c 是 1,那么它调用 firstReader,然后退出,因为 c 不是 2。计算机不够聪明,无法弄清楚你的实际意思。您可能打算编写 else if(c==2)
将两个 if
链接在一起,以便最后的 else
仅在两者都不为真时运行。
现在一切都按预期完美运行,顺序错误只是由于对 ciphertext_base64
的错误逻辑调用
感谢大家的时间和指导。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。