user-0 提问: 求助c语言关于Expression:*file!=_T('\0')

回复:
user: user-0
content: 引用 4楼wang0635 的回复:参考这种模式 fp = fopen("abc.txt", "r"); if (fp != NULL) { 	i = 0; 	while (!feof(fp)) 		fscanf(fp, "%c", &inString[i++]); 	inString[i] = '\0'; }谢谢

user: user-1
content: 参考这种模式 fp = fopen("abc.txt", "r"); if (fp != NULL) { 	i = 0; 	while (!feof(fp)) 		fscanf(fp, "%c", &inString[i++]); 	inString[i] = '\0'; }

user: user-0
content: 引用 1 楼 wang0635 的回复:[quote=引用 楼主 zcyang911 的回复:] 本人想实现AES对文件内容的加密与解密。输入文件名，输出加密的文件；输入加密的文件，输出解密的文件。但运行后出现如标题所描述的问题  #include <memory.h> #include <stdio.h> #include <string.h> #include <stdlib.h>  #include "aes.h"  #pragma comment(lib,"libeay32.lib")     char enString[1024]; 	char deString[1024];  void testAes(char inString[], int inLen, char passwd[], int pwdLen) { 	int i,j, len, nLoop, nRes;   	unsigned char buf[16]; 	unsigned char buf2[16]; 	unsigned char aes_keybuf[32]; 	AES_KEY aeskey;  	// 准备32字节(256位)的AES密码字节 	memset(aes_keybuf,0x90,32); 	if(pwdLen<32){ len=pwdLen; } else { len=32;} 	for(i=0;i<len;i++) aes_keybuf[i]=passwd[i]; 	// 输入字节串分组成16字节的块	 	nLoop=inLen/16; nRes = inLen%16; 	// 加密输入的字节串 	AES_set_encrypt_key(aes_keybuf,256,&aeskey); 	for(i=0;i<nLoop;i++){ 		memset(buf,0,16); 		for(j=0;j<16;j++) buf[j]=inString[i*16+j]; 		AES_encrypt(buf,buf2,&aeskey); 		for(j=0;j<16;j++) enString[i*16+j]=buf2[j]; 	} 	if(nRes>0){ 		memset(buf,0,16); 		for(j=0;j<nRes;j++) buf[j]=inString[i*16+j]; 		AES_encrypt(buf,buf2,&aeskey); 		for(j=0;j<16;j++) enString[i*16+j]=buf2[j]; 		//puts("encrypt"); 	} 	enString[i*16+j]=0; 	// 密文串的解密	 	AES_set_decrypt_key(aes_keybuf,256,&aeskey); 	for(i=0;i<nLoop;i++){ 		memset(buf,0,16); 		for(j=0;j<16;j++) buf[j]=enString[i*16+j]; 		AES_decrypt(buf,buf2,&aeskey); 		for(j=0;j<16;j++) deString[i*16+j]=buf2[j]; 	} 	if(nRes>0){ 		memset(buf,0,16); 		for(j=0;j<16;j++) buf[j]=enString[i*16+j]; 		AES_decrypt(buf,buf2,&aeskey); 		for(j=0;j<16;j++) deString[i*16+j]=buf2[j]; 		//puts("decrypt"); 	} 	deString[i*16+nRes]=0; 	//比较解密后的串是否与输入的原始串相同 	 }  int main(int argc, char* argv[]) {    	FILE *fp1,*fp2; 	int i=0; 	int j=0; 	char inString[1024]={0};     char enfilename[256]; 	char defilename[256]; 	char passwd[32]={0}; 	int x; 	while(1) 	{ 	printf("------选择模式------\n"); 	printf("1.enc\n"); 	printf("2.dec\n"); 	printf("3.exit\n"); 	scanf("%d",&x); 	printf("\n");      	if(x==3)       break; 	switch(x) 	{ 	case 1:printf("input enfile name:");            gets(enfilename); 		   fp1=fopen(enfilename,"r"); 		   if(fp1==NULL) 		   { 		   printf("error\n"); 		   getchar(); 		   exit(0); 		   } 		   fclose(fp1); 		   while(inString[i]!='\0') 		   {fscanf(fp1,"%c",inString[i]);  刚刚fclose了，fp1已经无效！后面还有类似情况 		   i++; 		   }  		   printf("input key:\n"); 		   scanf("%c",&passwd); 		   testAes(inString, strlen(inString), passwd, strlen(passwd)); 		   		   fp2=fopen("cipher.txt","w"); 		   if(fp2==NULL) 		   { 			 printf("error\n"); 			 getchar(); 			 exit(0); 		   }             while(enString[j]!='\0') 		   {fscanf(fp2,"%c",enString[j]); 		   j++; 		   } 		   fclose(fp2);  		   break;   	case 2:printf("input defile name:");            gets(defilename); 		   fp1=fopen(defilename,"r"); 		   if(fp1==NULL) 		   { 		   printf("error\n"); 		   getchar(); 		   exit(0); 		   } 		   fclose(fp1); 		   while(inString[i]!='\0') 		   {fscanf(fp1,"%c",inString[i]); 		   i++; 		   } 		   printf("input key:\n"); 		   scanf("%c",&passwd); 		   testAes(inString, strlen(inString), passwd, strlen(passwd)); 		   		   if(fp2==NULL) 		   { 			 printf("error\n"); 			 getchar(); 			 exit(0); 		   } 		  fp2=fopen("decipher.txt","w"); 		     while(deString[j]!='\0') 		   {fscanf(fp2,"%c",deString[j]); 		   j++; 		   } 			   fclose(fp2); 		   break; 	       default:printf("error\n"); 		break;     } 	     } 	return 0; } 至少fclose之后再用文件指针有问题[/quote] 我想把文件里的内容赋值到数组里，请问怎么改

user: user-2
content: 仅供参考：#pragma comment(lib, "crypt32.lib") #pragma comment(lib, "advapi32.lib") #define _WIN32_WINNT 0x0400 #include <stdio.h> #include <windows.h> #include <wincrypt.h> #define KEYLENGTH  0x00800000 void HandleError(char *s); //-------------------------------------------------------------------- //  These additional #define statements are required. #define ENCRYPT_ALGORITHM CALG_RC4 #define ENCRYPT_BLOCK_SIZE 8 //   Declare the function EncryptFile. The function definition //   follows main. BOOL EncryptFile(     PCHAR szSource,     PCHAR szDestination,     PCHAR szPassword); //-------------------------------------------------------------------- //   Begin main. void main(void) {     CHAR szSource[100];     CHAR szDestination[100];     CHAR szPassword[100];     printf("Encrypt a file. \n\n");     printf("Enter the name of the file to be encrypted: ");     scanf("%s",szSource);     printf("Enter the name of the output file: ");     scanf("%s",szDestination);     printf("Enter the password:");     scanf("%s",szPassword);     //--------------------------------------------------------------------     // Call EncryptFile to do the actual encryption.     if(EncryptFile(szSource, szDestination, szPassword)) {         printf("Encryption of the file %s was a success. \n", szSource);         printf("The encrypted data is in file %s.\n",szDestination);     } else {         HandleError("Error encrypting file!");     } } // End of main //-------------------------------------------------------------------- //   Code for the function EncryptFile called by main. static BOOL EncryptFile(     PCHAR szSource,     PCHAR szDestination,     PCHAR szPassword) //-------------------------------------------------------------------- //   Parameters passed are: //     szSource, the name of the input, a plaintext file. //     szDestination, the name of the output, an encrypted file to be //         created. //     szPassword, the password. {     //--------------------------------------------------------------------     //   Declare and initialize local variables.     FILE *hSource;     FILE *hDestination;     HCRYPTPROV hCryptProv;     HCRYPTKEY hKey;     HCRYPTHASH hHash;     PBYTE pbBuffer;     DWORD dwBlockLen;     DWORD dwBufferLen;     DWORD dwCount;     //--------------------------------------------------------------------     // Open source file.     if(hSource = fopen(szSource,"rb")) {         printf("The source plaintext file, %s, is open. \n", szSource);     } else {         HandleError("Error opening source plaintext file!");     }     //--------------------------------------------------------------------     // Open destination file.     if(hDestination = fopen(szDestination,"wb")) {         printf("Destination file %s is open. \n", szDestination);     } else {         HandleError("Error opening destination ciphertext file!");     }     //以下获得一个CSP句柄     if(CryptAcquireContext(                 &hCryptProv,                 NULL,               //NULL表示使用默认密钥容器，默认密钥容器名                 //为用户登陆名                 NULL,                 PROV_RSA_FULL,                 0)) {         printf("A cryptographic provider has been acquired. \n");     } else {         if(CryptAcquireContext(                     &hCryptProv,                     NULL,                     NULL,                     PROV_RSA_FULL,                     CRYPT_NEWKEYSET))//创建密钥容器         {             //创建密钥容器成功，并得到CSP句柄             printf("A new key container has been created.\n");         } else {             HandleError("Could not create a new key container.\n");         }     }     //--------------------------------------------------------------------     // 创建一个会话密钥（session key）     // 会话密钥也叫对称密钥，用于对称加密算法。     // （注: 一个Session是指从调用函数CryptAcquireContext到调用函数     //   CryptReleaseContext 期间的阶段。会话密钥只能存在于一个会话过程）     //--------------------------------------------------------------------     // Create a hash object.     if(CryptCreateHash(                 hCryptProv,                 CALG_MD5,                 0,                 0,                 &hHash)) {         printf("A hash object has been created. \n");     } else {         HandleError("Error during CryptCreateHash!\n");     }     //--------------------------------------------------------------------     // 用输入的密码产生一个散列     if(CryptHashData(                 hHash,                 (BYTE *)szPassword,                 strlen(szPassword),                 0)) {         printf("The password has been added to the hash. \n");     } else {         HandleError("Error during CryptHashData. \n");     }     //--------------------------------------------------------------------     // 通过散列生成会话密钥     if(CryptDeriveKey(                 hCryptProv,                 ENCRYPT_ALGORITHM,                 hHash,                 KEYLENGTH,                 &hKey)) {         printf("An encryption key is derived from the password hash. \n");     } else {         HandleError("Error during CryptDeriveKey!\n");     }     //--------------------------------------------------------------------     // Destroy the hash object.     CryptDestroyHash(hHash);     hHash = NULL;     //--------------------------------------------------------------------     //  The session key is now ready.     //--------------------------------------------------------------------     // 因为加密算法是按ENCRYPT_BLOCK_SIZE 大小的块加密的，所以被加密的     // 数据长度必须是ENCRYPT_BLOCK_SIZE 的整数倍。下面计算一次加密的     // 数据长度。     dwBlockLen = 1000 - 1000 % ENCRYPT_BLOCK_SIZE;     //--------------------------------------------------------------------     // Determine the block size. If a block cipher is used,     // it must have room for an extra block.     if(ENCRYPT_BLOCK_SIZE > 1)         dwBufferLen = dwBlockLen + ENCRYPT_BLOCK_SIZE;     else         dwBufferLen = dwBlockLen;     //--------------------------------------------------------------------     // Allocate memory.     if(pbBuffer = (BYTE *)malloc(dwBufferLen)) {         printf("Memory has been allocated for the buffer. \n");     } else {         HandleError("Out of memory. \n");     }     //--------------------------------------------------------------------     // In a do loop, encrypt the source file and write to the destination file.     do {         //--------------------------------------------------------------------         // Read up to dwBlockLen bytes from the source file.         dwCount = fread(pbBuffer, 1, dwBlockLen, hSource);         if(ferror(hSource)) {             HandleError("Error reading plaintext!\n");         }         //--------------------------------------------------------------------         // 加密数据         if(!CryptEncrypt(                     hKey,           //密钥                     0,              //如果数据同时进行散列和加密，这里传入一个                     //散列对象                     feof(hSource),  //如果是最后一个被加密的块，输入TRUE.如果不是输.                     //入FALSE这里通过判断是否到文件尾来决定是否为                     //最后一块。                     0,              //保留                     pbBuffer,       //输入被加密数据，输出加密后的数据                     &dwCount,       //输入被加密数据实际长度，输出加密后数据长度                     dwBufferLen))   //pbBuffer的大小。         {             HandleError("Error during CryptEncrypt. \n");         }         //--------------------------------------------------------------------         // Write data to the destination file.         fwrite(pbBuffer, 1, dwCount, hDestination);         if(ferror(hDestination)) {             HandleError("Error writing ciphertext.");         }     } while(!feof(hSource));     //--------------------------------------------------------------------     //  End the do loop when the last block of the source file has been     //  read, encrypted, and written to the destination file.     //--------------------------------------------------------------------     // Close files.     if(hSource)         fclose(hSource);     if(hDestination)         fclose(hDestination);     //--------------------------------------------------------------------     // Free memory.     if(pbBuffer)         free(pbBuffer);     //--------------------------------------------------------------------     // Destroy session key.     if(hKey)         CryptDestroyKey(hKey);     //--------------------------------------------------------------------     // Destroy hash object.     if(hHash)         CryptDestroyHash(hHash);     //--------------------------------------------------------------------     // Release provider handle.     if(hCryptProv)         CryptReleaseContext(hCryptProv, 0);     return(TRUE); } // End of Encryptfile //-------------------------------------------------------------------- //  This example uses the function HandleError, a simple error //  handling function, to print an error message to the standard error //  (stderr) file and exit the program. //  For most applications, replace this function with one //  that does more extensive error reporting. void HandleError(char *s) {     fprintf(stderr,"An error occurred in running the program. \n");     fprintf(stderr,"%s\n",s);     fprintf(stderr, "Error number %x.\n", GetLastError());     fprintf(stderr, "Program terminating. \n");     exit(1); } // End of HandleError

user: user-1
content: 引用 楼主 zcyang911 的回复:本人想实现AES对文件内容的加密与解密。输入文件名，输出加密的文件；输入加密的文件，输出解密的文件。但运行后出现如标题所描述的问题  #include <memory.h> #include <stdio.h> #include <string.h> #include <stdlib.h>  #include "aes.h"  #pragma comment(lib,"libeay32.lib")     char enString[1024]; 	char deString[1024];  void testAes(char inString[], int inLen, char passwd[], int pwdLen) { 	int i,j, len, nLoop, nRes;   	unsigned char buf[16]; 	unsigned char buf2[16]; 	unsigned char aes_keybuf[32]; 	AES_KEY aeskey;  	// 准备32字节(256位)的AES密码字节 	memset(aes_keybuf,0x90,32); 	if(pwdLen<32){ len=pwdLen; } else { len=32;} 	for(i=0;i<len;i++) aes_keybuf[i]=passwd[i]; 	// 输入字节串分组成16字节的块	 	nLoop=inLen/16; nRes = inLen%16; 	// 加密输入的字节串 	AES_set_encrypt_key(aes_keybuf,256,&aeskey); 	for(i=0;i<nLoop;i++){ 		memset(buf,0,16); 		for(j=0;j<16;j++) buf[j]=inString[i*16+j]; 		AES_encrypt(buf,buf2,&aeskey); 		for(j=0;j<16;j++) enString[i*16+j]=buf2[j]; 	} 	if(nRes>0){ 		memset(buf,0,16); 		for(j=0;j<nRes;j++) buf[j]=inString[i*16+j]; 		AES_encrypt(buf,buf2,&aeskey); 		for(j=0;j<16;j++) enString[i*16+j]=buf2[j]; 		//puts("encrypt"); 	} 	enString[i*16+j]=0; 	// 密文串的解密	 	AES_set_decrypt_key(aes_keybuf,256,&aeskey); 	for(i=0;i<nLoop;i++){ 		memset(buf,0,16); 		for(j=0;j<16;j++) buf[j]=enString[i*16+j]; 		AES_decrypt(buf,buf2,&aeskey); 		for(j=0;j<16;j++) deString[i*16+j]=buf2[j]; 	} 	if(nRes>0){ 		memset(buf,0,16); 		for(j=0;j<16;j++) buf[j]=enString[i*16+j]; 		AES_decrypt(buf,buf2,&aeskey); 		for(j=0;j<16;j++) deString[i*16+j]=buf2[j]; 		//puts("decrypt"); 	} 	deString[i*16+nRes]=0; 	//比较解密后的串是否与输入的原始串相同 	 }  int main(int argc, char* argv[]) {    	FILE *fp1,*fp2; 	int i=0; 	int j=0; 	char inString[1024]={0};     char enfilename[256]; 	char defilename[256]; 	char passwd[32]={0}; 	int x; 	while(1) 	{ 	printf("------选择模式------\n"); 	printf("1.enc\n"); 	printf("2.dec\n"); 	printf("3.exit\n"); 	scanf("%d",&x); 	printf("\n");      	if(x==3)       break; 	switch(x) 	{ 	case 1:printf("input enfile name:");            gets(enfilename); 		   fp1=fopen(enfilename,"r"); 		   if(fp1==NULL) 		   { 		   printf("error\n"); 		   getchar(); 		   exit(0); 		   } 		   fclose(fp1); 		   while(inString[i]!='\0') 		   {fscanf(fp1,"%c",inString[i]);  刚刚fclose了，fp1已经无效！后面还有类似情况 		   i++; 		   }  		   printf("input key:\n"); 		   scanf("%c",&passwd); 		   testAes(inString, strlen(inString), passwd, strlen(passwd)); 		   		   fp2=fopen("cipher.txt","w"); 		   if(fp2==NULL) 		   { 			 printf("error\n"); 			 getchar(); 			 exit(0); 		   }             while(enString[j]!='\0') 		   {fscanf(fp2,"%c",enString[j]); 		   j++; 		   } 		   fclose(fp2);  		   break;   	case 2:printf("input defile name:");            gets(defilename); 		   fp1=fopen(defilename,"r"); 		   if(fp1==NULL) 		   { 		   printf("error\n"); 		   getchar(); 		   exit(0); 		   } 		   fclose(fp1); 		   while(inString[i]!='\0') 		   {fscanf(fp1,"%c",inString[i]); 		   i++; 		   } 		   printf("input key:\n"); 		   scanf("%c",&passwd); 		   testAes(inString, strlen(inString), passwd, strlen(passwd)); 		   		   if(fp2==NULL) 		   { 			 printf("error\n"); 			 getchar(); 			 exit(0); 		   } 		  fp2=fopen("decipher.txt","w"); 		     while(deString[j]!='\0') 		   {fscanf(fp2,"%c",deString[j]); 		   j++; 		   } 			   fclose(fp2); 		   break; 	       default:printf("error\n"); 		break;     } 	     } 	return 0; } 至少fclose之后再用文件指针有问题