Beispiel #1
0
int main(void)
{
	
	//256
    uint8_t key[] = { 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
                      0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4 };
    uint8_t in[]  = { 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a};
    uint8_t out[16];
	
	for(int i = 0; i < 16; i++)
	{
		out[i] = in[i];
	}
	phex(out,16);
	
    struct AES_ctx ctx;
    AES_init_ctx(&ctx, key);
    AES_ECB_encrypt(&ctx, in);
	phex(in,16);
	AES_init_ctx(&ctx, key);
	AES_ECB_decrypt(&ctx, in);
	phex(out,16);


    return 0;
}
Beispiel #2
0
QByteArray QTinyAes::encryptRaw(const QByteArray &plain) const
{
	Q_ASSERT_X(!d->key.isEmpty(), Q_FUNC_INFO, "The key must not be empty to encrypt data");
	Q_ASSERT_X(plain.size() % BlockSize == 0, Q_FUNC_INFO, "plain must be a multiple of QTinyAes::BlockSize");
	auto buffer = plain;

	AES_ctx ctx;
	if(d->iv.isNull())
		AES_init_ctx(&ctx, (uint8_t*)d->key.constData());
	else
		AES_init_ctx_iv(&ctx, (uint8_t*)d->key.constData(), (uint8_t*)d->iv.constData());

	switch(d->mode) {
	case CTR:
		AES_CTR_xcrypt_buffer(&ctx, (uint8_t*)buffer.data(), (uint32_t)buffer.size());
		break;
	case CBC:
		AES_CBC_encrypt_buffer(&ctx, (uint8_t*)buffer.data(),  (uint32_t)buffer.size());
		break;
	case ECB:
		for(auto i = 0; i < buffer.size(); i += BlockSize) {
			auto ctxCopy = ctx;
			AES_ECB_encrypt(&ctxCopy, (uint8_t*)(buffer.data() + i));
		}
		break;
	default:
		Q_UNREACHABLE();
		break;
	}

	memset(&ctx, 0, sizeof(AES_ctx));

	return buffer;

}