本文整理汇总了C++中AES类的典型用法代码示例。如果您正苦于以下问题:C++ AES类的具体用法?C++ AES怎么用?C++ AES使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了AES类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main(int argc, char **argv) {
if(argc < 3) {
printf("USAGE: aes KEY PLAINTEXT\n");
return 1;
}
byte *key;
uint *ct, *pt;
uint keySize = stringToByteArray(argv[1], &key);
uint ptSize = stringToByteArray(argv[2], &pt);
if(keySize != 16 && keySize != 24 && keySize != 32) {
printf("Invalid AES key size.\n");
return 1;
}
if(ptSize % 4 != 0) {
printf("Plaintext size must be a multiple of AES block size.\n");
return 1;
}
ct = (uint *)malloc(ptSize*sizeof(uint));
AES *aes = new AES();
aes->makeKey(key, keySize << 3, DIR_ENCRYPT);
for(uint i = 0; i < ptSize; i += 4) {
aes->encrypt(pt + i, ct + i);
}
printHexArray(ct, ptSize);
return 0;
}
示例2: switch
Mikey_Payloads* Mikey_Payload_KEMAC::decode_payloads( int firstPayloadType, byte_t * encrKey, int encrKeyLength, byte_t * iv )
{
byte_t * decrData = new byte_t[ encr_data_length_value ];
AES * aes;
switch( encr_alg_value)
{
case MIKEY_PAYLOAD_KEMAC_ENCR_AES_CM_128:
aes = new AES( encrKey, encrKeyLength );
aes->ctr_encrypt( encr_data_ptr, encr_data_length_value, decrData, iv );
delete aes;
break;
case MIKEY_PAYLOAD_KEMAC_ENCR_NULL:
memcpy( decrData, encr_data_ptr, encr_data_length_value );
break;
case MIKEY_PAYLOAD_KEMAC_ENCR_AES_KW_128:
//TODO
default:
delete [] decrData;
throw Mikey_Exception(
"Unknown encryption algorithm" );
break;
}
Mikey_Payloads *output =
new Mikey_Payloads( firstPayloadType, decrData, encr_data_length_value );
// decrData is owned and deleted by MikeyPayloads
return output;
}
示例3: main
int main(char* args[]) {
// instantiate classes
InOut* io = new InOut();
AES* aes;
// delete output files
if (OUTPUTKEYEXP) { remove(FOUT_KEYEXP); }
if (OUTPUTENCRYPT) { remove(FOUT_ENCRYPT); }
if (OUTPUTDECRYPT) { remove(FOUT_DECRYPT); }
// run for all three key sizes
for(int keySize = 128; keySize <= 256; keySize = keySize + 64) {
// title
cout<<"AES (Rijndael) - w/ "<<dec<<keySize<<" Bit Key";
// get key and create cryptographic class
byte* key = io->getKey(keySize);
aes = new AES(keySize, key);
// get message
byte* plaintext = io->get16BytesOfMessage();
// print original message
cout<<"\n\nOriginal Message:\n";
for(int i=0; i<16; i++) {
cout<<hex<<setfill('0')<<setw(2);
cout<<int(plaintext[i]);
}
// run encryption
byte* ciphertext = new byte[16];
ciphertext = aes->encrypt(plaintext);
// print ciphertext
cout<<"\n\nEncrypted Message:\n";
for(int i=0; i<16; i++) {
cout<<hex<<setfill('0')<<setw(2);
cout<<int(ciphertext[i]);
}
// run decryption
byte* decrypttext = aes->decrypt(ciphertext);
// recovered plaintext
cout<<"\n\nDecrypted Message:\n";
for(int i=0; i<16; i++) {
cout<<hex<<setfill('0')<<setw(2);
cout<<int(decrypttext[i]);
}
cout<<"\n\n";
}
return 0;
}
示例4: GenWzKey
void GenWzKey(const uint8_t* IV, uint8_t* key) {
uint8_t BigIV[16];
for (int i = 0; i < 16; i += 4) {
memcpy(BigIV+i, IV, 4);
}
AESGen.SetParameters(256, 128);
AESGen.StartEncryption(AESKey2);
AESGen.EncryptBlock(BigIV, key);
for (int i = 16; i < 0x10000; i += 16) {
AESGen.EncryptBlock(key+i-16, key+i);
}
}
示例5: encrypt
/**
* Encrypts the actual text using the password
* @param password Password
* @return Encrypted text
*/
QByteArray MainWindow::encrypt(QString password)
{
AES crypto;
QString hashedPassword = QString( QCryptographicHash::hash(( password.toUtf8() ),QCryptographicHash::Md5).toHex() );
QByteArray key = crypto.hexStringToByte(hashedPassword);
QByteArray data = ui->textEdit_mainWindow_surface->toHtml().toUtf8();
QByteArray encrypted = crypto.encrypt(data, key);
return encrypted;
}
示例6: decrypt
/**
* Decrypts the actual text using the password
* @param password Password
* @return Decrypted text
*/
QByteArray MainWindow::decrypt(QString password)
{
AES crypto;
QString hashedPassword = QString( QCryptographicHash::hash(( password.toUtf8() ),QCryptographicHash::Md5).toHex() );
QByteArray key = crypto.hexStringToByte(hashedPassword);
QByteArray data = QByteArray::fromBase64( ui->textEdit_mainWindow_surface->toPlainText().toUtf8() );
QByteArray decrypted = crypto.decrypt(data, key);
return decrypted;
}
示例7: transportInit
bool transportInit() {
#if defined(MY_RF24_ENABLE_ENCRYPTION)
hwReadConfigBlock((void*)_psk, (void*)EEPROM_RF_ENCRYPTION_AES_KEY_ADDRESS, 32);
//set up AES-key
_aes.set_key(_psk, 32);
// Make sure it is purged from memory when set
memset(_psk, 0, 32);
//set up AES IV
hwReadConfigBlock((void*)IVCl,(void*)EEPROM_RF_ENCRYPTION_AES_IV_ADDRESS,8);
_aes.set_IV(IVCl);
#endif
return RF24_initialize();
}
示例8: transportInit
bool transportInit() {
// Start up the radio library
_rf24.begin();
if (!_rf24.isPVariant()) {
return false;
}
_rf24.setAutoAck(1);
_rf24.setAutoAck(BROADCAST_PIPE,false); // Turn off auto ack for broadcast
_rf24.enableAckPayload();
_rf24.setChannel(MY_RF24_CHANNEL);
_rf24.setPALevel(_paLevel);
if (!_rf24.setDataRate(MY_RF24_DATARATE)) {
return false;
}
_rf24.setRetries(5,15);
_rf24.setCRCLength(RF24_CRC_16);
_rf24.enableDynamicPayloads();
// All nodes listen to broadcast pipe (for FIND_PARENT_RESPONSE messages)
_rf24.openReadingPipe(BROADCAST_PIPE, TO_ADDR(BROADCAST_ADDRESS));
#if defined(MY_RF24_ENABLE_ENCRYPTION)
_aes.set_key(_psk, 16); //set up AES-key
#endif
//_rf24.printDetails();
return true;
}
示例9: main
int main(int argc, char **argv) {
if(argc < 2) {
printf("USAGE: benchmark FILE\n");
return 1;
}
byte key[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
uint keySize = 16;
uint *ct, *pt;
FILE *f = fopen(argv[1], "rb");
if(f == NULL) {
printf("File not found.\n");
return 1;
}
fseek(f, 0, SEEK_END);
uint f_size = ftell(f);
rewind(f);
if(f_size % 4*sizeof(uint) != 0) {
printf("Plaintext size must be a multiple of AES block size.\n");
return 1;
}
uint ptSize = f_size / sizeof(uint);
pt = (uint*)malloc(f_size);
fread(pt, sizeof(uint), ptSize, f);
fclose(f);
ct = (uint *)malloc(ptSize*sizeof(uint));
AES *aes = new AES();
aes->makeKey(key, keySize << 3, DIR_ENCRYPT);
clock_t start = clock();
for(uint i = 0; i < ptSize; i += 4) {
aes->encrypt(pt + i, ct + i);
}
clock_t end = clock();
printf("%d blocks encrypted in %d/%d seconds.\n", ptSize >> 2, end-start, CLOCKS_PER_SEC);
return 0;
}
示例10: RunAESDemo
int RunAESDemo()
{
AES aes;
vector<unsigned char> key;
aes.GenerateKey( 256, key );
printf( "key.size(): %d\n", (int)key.size() );
// unsigned char buffer[50000]; // 50KB
size_t read_bytes = 0;
vector<unsigned char> plaintext;
plaintext.resize(50000,0);
// string plaintext_file = "input_data/plaintext/plaintext_example.txt";
// string plaintext_file = "input_data/plaintext/0s.txt";
string plaintext_file = "input_data/plaintext/more0s.txt";
FILE *fp = fopen( plaintext_file.c_str(), "rb" );
if(fp)
{
// read_bytes = fread(buffer,sizeof(buffer),1,fp);
read_bytes = fread(&plaintext[0], sizeof(unsigned char), plaintext.size(), fp);
printf( "read_bytes: %d\n", (int)read_bytes );
fclose(fp);
}
vector<unsigned char> ciphertext;
aes.Encrypt( plaintext, ciphertext );
printf( "ciphertext.size(): %d\n", (int)ciphertext.size() );
if( 0 < ciphertext.size() )
{
fp = fopen( string(plaintext_file + ".encrypted").c_str(), "wb" );
if(fp)
{
size_t written_bytes = fwrite(&ciphertext[0], sizeof(unsigned char), ciphertext.size(), fp);
printf( "written_bytes: %d\n", (int)written_bytes );
fclose(fp);
}
}
return 0;
}
示例11: OnOK
void CDialogOption::OnOK() {
USES_CONVERSION;
if (!UpdateData(TRUE)) {
return;
}
AES aes;
CString ak;
CString sk;
if (m_strAK.Trim().IsEmpty()) {
MessageBox(_T("ÇëÊäÈë'AK'¡£"));
return;
}
if (m_strSK.Trim().IsEmpty()) {
MessageBox(_T("ÇëÊäÈë'SK'¡£"));
return;
}
if (m_strHost.Trim().IsEmpty()) {
MessageBox(_T("ÇëÊäÈë'·þÎñÆ÷µØÖ·'¡£"));
return;
}
lc_bce_access_key_t key = { 0 };
strncpy_s(key.access_key_id, T2A(m_strAK), -1);
strncpy_s(key.secret_access_key, T2A(m_strSK), -1);
if (m_pParent->DoAuth(&key, m_strHost, FALSE)) {
aes.Encrypt(m_strAK, ak);
aes.Encrypt(m_strSK, sk);
ConfigMgr::Instance().SetAk(ak);
ConfigMgr::Instance().SetSk(sk);
ConfigMgr::Instance().SetHost(m_strHost);
m_pParent->UpdateLiveCaptureData();
EndDialog(IDOK);
DestroyWindow();
}
}
示例12: OnInitDialog
BOOL CDialogOption::OnInitDialog() {
CString ak;
CString sk;
CString host;
ConfigMgr::Instance().GetAk(ak);
ConfigMgr::Instance().GetSk(sk);
ConfigMgr::Instance().GetHost(host);
AES aes;
aes.Decrypt(ak, ak);
aes.Decrypt(sk, sk);
m_strAK = m_strAKbak = ak;
m_strSK = m_strSKbak = sk;
m_strHost = m_strHostbak = host;
CDialog::OnInitDialog();
return TRUE;
}
示例13: transportSend
bool transportSend(uint8_t recipient, const void* data, uint8_t len) {
#if defined(MY_RF24_ENABLE_ENCRYPTION)
// copy input data because it is read-only
memcpy(_dataenc,data,len);
len = len > 16 ? 32 : 16;
#if defined(MY_GATEWAY_FEATURE)
SdReadNodeData(RF24_getNodeID(), nd);
_aes.set_key(nd.AES_256,32);
_aes.set_IV(nd.IV);
#else
#endif
//encrypt data
_aes.set_IV(IVCl);
_aes.cbc_encrypt(_dataenc, _dataenc, len/16);
bool status = RF24_sendMessage( recipient, _dataenc, len );
#else
bool status = RF24_sendMessage( recipient, data, len );
#endif
return status;
}
示例14: transportReceive
uint8_t transportReceive(void *data)
{
uint8_t len = 0;
#if defined(MY_RX_MESSAGE_BUFFER_FEATURE)
transportQueuedMessage* msg = transportRxQueue.getBack();
if (msg) {
len = msg->m_len;
(void)memcpy(data, msg->m_data, len);
(void)transportRxQueue.popBack();
}
#else
len = RF24_readMessage(data);
#endif
#if defined(MY_RF24_ENABLE_ENCRYPTION)
// has to be adjusted, WIP!
RF24_aes.set_IV(0);
// decrypt data
if (RF24_aes.cbc_decrypt((uint8_t *)data, (uint8_t *)data, len > 16 ? 2 : 1) != AES_SUCCESS) {
len = 0;
}
#endif
return len;
}
示例15: memcpy
void Crypto::TransformData(uint8_t* IV, uint8_t* data, uint32_t len) {
uint8_t BigIV[16];
for (int i = 0; i < 16; i += 4) {
memcpy(BigIV+i, IV, 4);
}
uint32_t pos = 0;
uint8_t first = 1;
int32_t tpos = 0;
while (len > pos) {
AESGen.SetParameters(256, 128);
AESGen.StartEncryption(AESKey2);
tpos = 1460 - first * 4;
if (len > pos + tpos) {
AESGen.TransformOFB(data + pos, BigIV, tpos);
}
else {
AESGen.TransformOFB(data + pos, BigIV, len - pos);
}
pos += tpos;
if (first) first = 0;
}
}