Beispiel #1
0
	void Cipher::SetKey (const ConstBufferPtr &key)
	{
		if (key.Size() != GetKeySize ())
			throw ParameterIncorrect (SRC_POS);

		if (!Initialized)
			ScheduledKey.Allocate (GetScheduledKeySize ());

		SetCipherKey (key);
		Key.CopyFrom (key);
		Initialized = true;
	}
Beispiel #2
0
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
%   G e n e r a t e S e c r e t K e y                                         %
%                                                                             %
%                                                                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  GenerateSecretKey() returns WizardTrue if a key is generated and successfully
%  added to the secret key ring.
%
%  The format of the GenerateSecretKey method is:
%
%      WizardBooleanType GenerateSecretKey(SecretInfo *secret_info,
%        ExceptionInfo *exception)
%
%  A description of each parameter follows:
%
%    o secret_info: The secret info.
%
%    o exception: Return any errors or warnings in this structure.
%
*/
WizardExport WizardBooleanType GenerateSecretKey(SecretInfo *secret_info,
  ExceptionInfo *exception)
{
  ExceptionInfo
    *sans;

  WizardBooleanType
    status;

  StringInfo
    *key,
    *phrase;

  (void) LogWizardEvent(TraceEvent,GetWizardModule(),"...");
  WizardAssert(AuthenticateDomain,secret_info != (SecretInfo *) NULL);
  WizardAssert(AuthenticateDomain,secret_info->signature == WizardSignature);
  WizardAssert(AuthenticateDomain,exception != (ExceptionInfo *) NULL);
  if (secret_info->passphrase == (char *) NULL)
    phrase=GetPassphrase(exception);
  else
    phrase=FileToStringInfo(secret_info->passphrase,~0,exception);
  if (phrase == (StringInfo *) NULL)
    return(WizardFalse);
  sans=AcquireExceptionInfo();
  do
  {
    if (secret_info->key != (StringInfo *) NULL)
      secret_info->key=DestroyStringInfo(secret_info->key);
    secret_info->key=GetRandomKey(secret_info->random_info,
      secret_info->key_length/8);
    ConstructHMAC(secret_info->hmac_info,phrase,secret_info->key);
    if (secret_info->id != (StringInfo *) NULL)
      secret_info->id=DestroyStringInfo(secret_info->id);
    secret_info->id=CloneStringInfo(GetHMACDigest(secret_info->hmac_info));
    SetKeyringId(secret_info->keyring_info,secret_info->id);
    status=ExportKeyringKey(secret_info->keyring_info,sans);
  } while (status != WizardFalse);
  sans=DestroyExceptionInfo(sans);
  SetKeyringKey(secret_info->keyring_info,secret_info->key);
  SetKeyringNonce(secret_info->keyring_info,secret_info->nonce);
  SetCipherKey(secret_info->cipher_info,phrase);
  SetCipherNonce(secret_info->cipher_info,GetKeyringNonce(
    secret_info->keyring_info));
  key=CloneStringInfo(GetKeyringKey(secret_info->keyring_info));
  (void) EncipherCipher(secret_info->cipher_info,key);
  SetKeyringKey(secret_info->keyring_info,key);
  status=ImportKeyringKey(secret_info->keyring_info,exception);
  phrase=DestroyStringInfo(phrase);
  key=DestroyStringInfo(key);
  return(WizardTrue);
}
Beispiel #3
0
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
%   A u t h e n t i c a t e S e c r e t K e y                                 %
%                                                                             %
%                                                                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  AuthenticateSecretKey() returns WizardTrue if the caller is authenticated.
%
%  The format of the AuthenticateSecretKey method is:
%
%      void AuthenticateSecretKey(SecretInfo *secret_info,
%        ExceptionInfo *exception)
%
%  A description of each parameter follows:
%
%    o secret_info: The secret info.
%
%    o exception: Return any errors or warnings in this structure.
%
*/
WizardExport WizardBooleanType AuthenticateSecretKey(SecretInfo *secret_info,
  ExceptionInfo *exception)
{
  WizardBooleanType
    status;

  StringInfo
    *digest,
    *phrase;

  (void) LogWizardEvent(TraceEvent,GetWizardModule(),"...");
  WizardAssert(AuthenticateDomain,secret_info != (SecretInfo *) NULL);
  WizardAssert(AuthenticateDomain,secret_info->signature == WizardSignature);
  SetKeyringId(secret_info->keyring_info,secret_info->id);
  status=ExportKeyringKey(secret_info->keyring_info,exception);
  if (status == WizardFalse)
    return(WizardFalse);
  if (secret_info->passphrase == (char *) NULL)
    phrase=GetPassphrase(exception);
  else
    phrase=FileToStringInfo(secret_info->passphrase,~0,exception);
  if (phrase == (StringInfo *) NULL)
    return(WizardFalse);
  SetCipherKey(secret_info->cipher_info,phrase);
  SetCipherNonce(secret_info->cipher_info,GetKeyringNonce(
    secret_info->keyring_info));
  if (secret_info->key != (StringInfo *) NULL)
    secret_info->key=DestroyStringInfo(secret_info->key);
  secret_info->key=CloneStringInfo(GetKeyringKey(secret_info->keyring_info));
  (void) DecipherCipher(secret_info->cipher_info,secret_info->key);
  ConstructHMAC(secret_info->hmac_info,phrase,secret_info->key);
  phrase=DestroyStringInfo(phrase);
  digest=CloneStringInfo(GetHMACDigest(secret_info->hmac_info));
  if (CompareStringInfo(secret_info->id,digest) != 0)
    {
      digest=DestroyStringInfo(digest);
      return(WizardFalse);
    }
  digest=DestroyStringInfo(digest);
  return(WizardTrue);
}
// ----------------------------------------------------------------------------
// MAINLINE LOGIC
// ----------------------------------------------------------------------------
int main() {
	
	// declarations
	int  action = UNSET;
	char rawPT[BUFFER_SIZE]    = "";
	char cleanPT[BUFFER_SIZE]  = "";
	int  a = A, b = B, inverse = INVERSE, n = ALPHABET_SIZE;
	char nameInputFile[30]     = "input.txt" ;
	char nameOutputFile[30]    = "output.txt";

	// seed the random number generator
	srand((unsigned int)time(NULL));

	// get ready
	action = fGetClnStr(action, rawPT, cleanPT);
	
	// display title page
	DispHeader(nameInputFile, nameOutputFile, a, b, inverse);
	puts("Welcome to Derrida - the command line Affine Cipher!");
	Pause();

	// detail loop
	while(action != QUIT) {
		ClearScreen();

		DispHeader(nameInputFile, nameOutputFile, a, b, inverse);
		
		// select an action
		action = SelectAction(action);
		
		DispHeader(nameInputFile, nameOutputFile, a, b, inverse);

		// execute action
		switch(action) {
		case SET_I_FILE: // change the input file
			action = fGetClnStr(action, rawPT, cleanPT, nameInputFile);
			break;
		case SET_O_FILE: // change the output file
			action = SetOutputFileName(nameOutputFile);
			break;
		case DIR_LIST: // display directory listing
			list_dir();
			break;
		case SET_KEY: // change cipher key
			action = SetCipherKey(a, b, n, inverse);
			break;
		case ENCRYPT: // encrypt the PT codes

			// display the string before encryption
			printf("Before encryption, the string contains:\n");
			puts(rawPT);
			printf("\n");
			printf("Sanatized, the string contains:\n");
			puts(cleanPT);
			printf("\n");
			// if encryption succeeds, inform the user
			if(EncryptString(cleanPT, a, b, n)) {
				FileOutput(nameOutputFile, cleanPT);
				printf("After encryption, the string contains:\n");
				puts(cleanPT);
				printf("\n");
			}

			Pause();
			break;

		case DECRYPT:
			// display the clean cipher code before decryption
			puts("Before decryption, the string contains:");
			puts(cleanPT);
			puts("\n");
			// if decryption succeeds, inform the user
			if(DecryptString(cleanPT, a, b, n)) {
				FileOutput(nameOutputFile, cleanPT);
				puts("After decryption, the string contains:\n");
				puts(cleanPT);
				puts("\n");
			}

			Pause();
			break;	
		}		
	}

	puts("The program will now exit.");
	Pause();

	// exit program
	return 0;
}