示例#1
0
UT_Error ODc_Crypto::decrypt(GsfInput* pStream, const ODc_CryptoInfo& cryptInfo,
    const std::string& password, GsfInput** pDecryptedInput)
{
	UT_return_val_if_fail(pStream, UT_ERROR);
	UT_return_val_if_fail(pDecryptedInput, UT_ERROR);
	
	// check if we support the requested decryption method
	UT_return_val_if_fail(g_ascii_strcasecmp(cryptInfo.m_algorithm.c_str(), "Blowfish CFB") == 0, UT_ERROR);
	UT_return_val_if_fail(g_ascii_strcasecmp(cryptInfo.m_keyType.c_str(), "PBKDF2") == 0, UT_ERROR);
	
	// base64 decode the salt
	gsize salt_length;
	unsigned char* salt = g_base64_decode(cryptInfo.m_salt.c_str(), &salt_length);

	// base64 decode the initialization vector
	gsize ivec_length;
	unsigned char* ivec = g_base64_decode(cryptInfo.m_initVector.c_str(), &ivec_length);

	// decrypt the content
	UT_Error result = performDecrypt(pStream, salt, salt_length, cryptInfo.m_iterCount,  
			ivec, password, cryptInfo.m_decryptedSize, pDecryptedInput);

	// cleanup
	FREEP(salt);
	FREEP(ivec);

	return result;
}
示例#2
0
文件: hw6.c 项目: ashtonmendes/AES
int main(int argc, char *argv[])
{
	//skip argv[0]
	argc--;
	argv++;

	if(argc <= 0) //No arguments provided
	{
		fprintf(stderr, "Please enter command line arguments.\n");
		printUsage();
		return EXIT_FAILURE;
	}

	//tablecheck
	if(strcmp(*argv, "tablecheck") == 0)
	{
		if(argc-1 < 1 || argc-1 > 1)
		{
			fprintf(stderr, "Malformed command.\n");
			printUsage();
			return EXIT_FAILURE;
		}

		char* tablefile = malloc(100*sizeof(char));

		for(argc--; argc>0; argc--)
		{
			argv++;

			if(strlen(*argv) < 4)
			{
				fprintf(stderr, "Malformed command.\n");
				printUsage();
				return EXIT_FAILURE;
			}

			//tablefile
			if(argv[0][0]=='-' && argv[0][1]=='t' && argv[0][2]=='=')
			{
				//Get tablefile from argv
				int i=0;
				for(i=3; i<strlen(*argv); i++)
				{
					tablefile[i-3] = argv[0][i];
				}
				tablefile[i-3] = '\0';
			}

			else
			{
				fprintf(stderr, "Malformed command.\n");
				printUsage();
				return EXIT_FAILURE;
			}
		}

		checkTable(tablefile);
	}

	//modprod
	else if(strcmp(*argv, "modprod") == 0)
	{
		//Get hold of arguments
		char* poly1 = malloc(100*sizeof(char));
		char* poly2 = malloc(100*sizeof(char));

		//No, less or extra parameters for modprod
		if(argc-1 < 2 || argc-1 > 2)
		{
			fprintf(stderr, "Malformed command.\n");
			printUsage();
			return EXIT_FAILURE;
		}

		for(argc--; argc>0; argc--)
		{
			argv++;

			if(strlen(*argv) < 5)
			{
				fprintf(stderr, "Malformed command.\n");
				printUsage();
				return EXIT_FAILURE;
			}

			//poly1
			if(argv[0][0]=='-' && argv[0][1]=='p' && argv[0][2]=='1' && argv[0][3]=='=')
			{
				//Get poly1 from argv
				int i=0;
				for(i=4; i<strlen(*argv); i++)
				{
					poly1[i-4] = argv[0][i];
				}
				poly1[i-4] = '\0';
			}

			//poly2
			else if(argv[0][0]=='-' && argv[0][1]=='p' && argv[0][2]=='2' && argv[0][3]=='=')
			{
				//Get poly2 from argv
				int i=0;
				for(i=4; i<strlen(*argv); i++)
				{
					poly2[i-4] = argv[0][i];
				}
				poly2[i-4] = '\0';
			}

			else
			{
				fprintf(stderr, "Malformed command.\n");
				printUsage();
				return EXIT_FAILURE;
			}
		}

		if(strlen(poly1) == 0 || strlen(poly2) == 0)
		{
			fprintf(stderr, "Malformed command.\n");
			printUsage();
			return EXIT_FAILURE;
		}

		computeModprod(poly1, poly2);

		free(poly1);
		free(poly2);
	}

	//keyexpand
	else if(strcmp(*argv, "keyexpand") == 0)
	{
		//Get hold of arguments
		char* key = malloc(100*sizeof(char));
		char* tablefileName = malloc(100*sizeof(char));

		//No, less or extra parameters for keyexpand
		if(argc-1 < 2 || argc-1 > 2)
		{
			fprintf(stderr, "Malformed command.\n");
			printUsage();
			return EXIT_FAILURE;
		}

		for(argc--; argc>0; argc--)
		{
			argv++;

			if(strlen(*argv) < 4)
			{
				fprintf(stderr, "Malformed command.\n");
				printUsage();
				return EXIT_FAILURE;
			}

			//key
			if(argv[0][0]=='-' && argv[0][1]=='k' && argv[0][2]=='=')
			{
				//Get key from argv
				int i=0;
				for(i=3; i<strlen(*argv); i++)
				{
					key[i-3] = argv[0][i];
				}
				key[i-3] = '\0';
			}

			//tablefile
			else if(argv[0][0]=='-' && argv[0][1]=='t' && argv[0][2]=='=')
			{
				//Get tablefile from argv
				int i=0;
				for(i=3; i<strlen(*argv); i++)
				{
					tablefileName[i-3] = argv[0][i];
				}
				tablefileName[i-3] = '\0';
			}

			else
			{
				fprintf(stderr, "Malformed command.\n");
				printUsage();
				return EXIT_FAILURE;
			}
		}

		if(strlen(key) == 0 || strlen(tablefileName) == 0)
		{
			fprintf(stderr, "Malformed command.\n");
			printUsage();
			return EXIT_FAILURE;
		}

		expandKey(key, tablefileName);

		free(key);
		free(tablefileName);
	}

	//Encrypt - Decrypt
	else if(strcmp(*argv, "encrypt") == 0 || strcmp(*argv, "decrypt") == 0)
	{
		char command[10]="";
		if(strcmp(*argv, "encrypt") == 0)strcpy(command, "encrypt");
		if(strcmp(*argv, "decrypt") == 0)strcpy(command, "decrypt");

		//Get hold of arguments
		char* key = malloc(100*sizeof(char));
		char* tableFileName = malloc(100*sizeof(char));
		char* file = malloc(100*sizeof(char));
		*file='\0';

		//No, less or extra parameters
		if(argc-1 < 2 || argc-1 > 3)
		{
			fprintf(stderr, "Malformed command.\n");
			printUsage();
			return EXIT_FAILURE;
		}

		for(argc--; argc>0; argc--)
		{
			argv++;

			//Commanldine option encountered
			if(argv[0][0] == '-')
			{
				if(strlen(*argv) < 4)
				{
					fprintf(stderr, "Malformed command.\n");
					printUsage();
					return EXIT_FAILURE;
				}

				//key
				if(argv[0][0]=='-' && argv[0][1]=='k' && argv[0][2]=='=')
				{
					//Get key from argv
					int i=0;
					for(i=3; i<strlen(*argv); i++)
					{
						key[i-3] = argv[0][i];
					}
					key[i-3] = '\0';
				}
				else if(argv[0][0]=='-' && argv[0][1]=='t' && argv[0][2]=='=')
				{
					//Get tableFileName from argv
					int i=0;
					for(i=3; i<strlen(*argv); i++)
					{
						tableFileName[i-3] = argv[0][i];
					}
					tableFileName[i-3] = '\0';
				}
				else
				{
					fprintf(stderr, "Malformed command.\n");
					printUsage();
					return EXIT_FAILURE;
				}
			}

			//file
			else if(strlen(*argv) > 0)
			{
				//Get file from argv
				int i=0;
				for(i=0; i<strlen(*argv); i++)
				{
					file[i] = argv[0][i];
				}
				file[i] = '\0';
			}
		}

		//Check for errors
		if(strlen(key)==0 || strlen(tableFileName)==0)
		{
			fprintf(stderr, "Malformed command.\n");
			printUsage();
			return EXIT_FAILURE;
		}
		else
		{
			if(strcmp(command, "encrypt")==0 || strcmp(command, "decrypt")==0)
			{
				//CHECK FORMAT OF KEY

				/*if(strlen(key)!=16)
				{
					fprintf(stderr, "Key should be 8-bytes long, in hexstring format.\n");
					printUsage();
					return EXIT_FAILURE;
				}
				else if(strlen(key)==16)
				{
					int k=0;
					for(k=0; k<16; k++)
					{
						if(key[k] < 48 || (key[k] > 57 && key[k] < 97) || key[k] > 102)
						{
							fprintf(stderr, "Key is not in hexstring format.\n");
							printUsage();
							return EXIT_FAILURE;
						}
					}
				}*/
			}
		}

		if(strlen(key) == 0 || strlen(tableFileName) == 0)
		{
			fprintf(stderr, "Malformed command.\n");
			printUsage();
			return EXIT_FAILURE;
		}

		if(strcmp(command, "encrypt") == 0) performEncrypt(key, tableFileName, file);
		if(strcmp(command, "decrypt") == 0) performDecrypt(key, tableFileName, file);

		free(key);
		free(tableFileName);
		free(file);
	}

	//inverse
	else if(strcmp(*argv, "inverse") == 0)
	{
		if(argc-1 < 1 || argc-1 > 1)
		{
			fprintf(stderr, "Malformed command.\n");
			printUsage();
			return EXIT_FAILURE;
		}

		char* poly = malloc(100*sizeof(char));

		for(argc--; argc>0; argc--)
		{
			argv++;

			if(strlen(*argv) < 4)
			{
				fprintf(stderr, "Malformed command.\n");
				printUsage();
				return EXIT_FAILURE;
			}

			//poly
			if(argv[0][0]=='-' && argv[0][1]=='p' && argv[0][2]=='=')
			{
				//Get tablefile from argv
				int i=0;
				for(i=3; i<strlen(*argv); i++)
				{
					poly[i-3] = argv[0][i];
				}
				poly[i-3] = '\0';
			}
		}

		if(strlen(poly) == 0)
		{
			fprintf(stderr, "Malformed command.\n");
			printUsage();
			return EXIT_FAILURE;
		}

		computeInverse(poly);
	}

	else
	{
		fprintf(stderr, "Malformed command.\n");
		printUsage();
		return EXIT_FAILURE;
	}

	return EXIT_SUCCESS;
}