Exemplo n.º 1
0
/*
 * encrypt(keySize,password,original_message,encrypted_message, mode, padding) 
 * - encrypts a message using AES Algorithm
 * 
 * Parameters: 
 *    keySize : Size of key to use in AES Algorithm
 *    password: Key to encrypt plaintext message.
 *    original_message: Plaintext message before calculating AES Algorithm
 *    encrypted_message: Ciphertext message after calculating AES Algorithm
 *    mode: cipher mode, two ways: ECB 
 *    padding: padding mode to fill blocks, tree ways PKCS5, ZEROS, X923
 * 
 * Examples:
 *  AES.encrypt(128,"libelium","Libelium",encrypted_message,ECB,PKCS5)
 * 
 */
uint8_t WaspAES::encrypt(uint16_t KeySize,char* Password,char original_message[],uint8_t* encrypted_message,uint8_t mode,uint8_t padding){
  
  // Varibales Declaration  
  uint16_t size;
  size = sizeOfBlocks(original_message);
  uint8_t original_data[size];   
  

  if (init(Password,KeySize)){
    // convert to uint8_t
    for (uint16_t i = 0; i < strlen(original_message); i++){
      original_data[i] = original_message[i];
    }

    // Padding the block??
    if (strlen(original_message) < size ) { 
      paddingEncrypt(original_data,size,strlen(original_message),padding);
    }

    if (mode == ECB){
      ECBEncrypt(original_data,size,KeySize);
    }

    // convert to char
    for (uint16_t i=0; i < size+1;i++){
      encrypted_message[i] = original_data[i];
    }

    return 1;
  
  }else{
    return 0;
  }
}
Exemplo n.º 2
0
/*
 * encrypt(keySize,password,original_message,encrypted_message, mode, padding)
 * - encrypts a message using AES Algorithm
 *
 * Parameters:
 *    keySize : Size of key to use in AES Algorithm
 *    password: Key to encrypt plaintext message.
 *    original_message: Plaintext message before calculating AES Algorithm
 *    encrypted_message: Ciphertext message after calculating AES Algorithm
 *    mode: cipher mode, two ways: ECB
 *    padding: padding mode to fill blocks, tree ways PKCS5, ZEROS, X923
 *
 * Examples:
 *  AES.encrypt(128,"libelium","Libelium",encrypted_message,ECB,PKCS5)
 *
 */
uint8_t WaspAES::encrypt(	uint16_t keySize,
							char* password,
							uint8_t* original_message,
							uint16_t length,
							uint8_t* encrypted_message,
							uint8_t mode,
							uint8_t padding)
{
	// Calculate length in Bytes of the encrypted message 
	// depending on the size of the original message
	uint16_t size;
	size = sizeOfBlocks(length);
	
	uint8_t original_data[size];

	// init the password and create the round keys which are 
	// stored in the proper context attribute
	if (init(password,keySize))
	{
		// convert to uint8_t
		for (uint16_t i = 0; i < length; i++)
		{
			original_data[i] = original_message[i];
		}

		// Padding the block if the length is less than the proper size 
		if ( length < size ) 
		{
			paddingEncrypt(	original_data,
							size,
							length,
							padding);
		}		
		
		if (mode == ECB)
		{
			ECBEncrypt( original_data, size, keySize);
		}
				
		// copy result
		for (uint16_t i=0; i < size;i++)
		{
			encrypted_message[i] = original_data[i];
		}

		return 1;

	}
	else
	{
		return 0;
	}
}