Exemplo n.º 1
1
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;
}
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();
    }
}
Exemplo n.º 3
0
TEST(AESTest, aes_test)
{
  static const struct { 
    int keylen;
    unsigned char key[32], pt[16], ct[16];
  } tests[] = {
    { 16,
      { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 
        0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }, 
      { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
        0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff },
      { 0x69, 0xc4, 0xe0, 0xd8, 0x6a, 0x7b, 0x04, 0x30, 
        0xd8, 0xcd, 0xb7, 0x80, 0x70, 0xb4, 0xc5, 0x5a }
    }, { 
      24,
      { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 
        0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
        0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 },
      { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
        0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff },
      { 0xdd, 0xa9, 0x7c, 0xa4, 0x86, 0x4c, 0xdf, 0xe0, 
        0x6e, 0xaf, 0x70, 0xa0, 0xec, 0x0d, 0x71, 0x91 }
    }, {
      32,
      { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 
        0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
        0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 
        0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f },
      { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
        0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff },
      { 0x8e, 0xa2, 0xb7, 0xca, 0x51, 0x67, 0x45, 0xbf, 
        0xea, 0xfc, 0x49, 0x90, 0x4b, 0x49, 0x60, 0x89 }
    }
  };

  AES *tf;

  unsigned char tmp[2][16];
  size_t i;
  int y;

  for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) {
    tf = new AES(tests[i].key, tests[i].keylen);

    tf->Encrypt(tests[i].pt, tmp[0]);
    tf->Decrypt(tmp[0], tmp[1]);
    if (memcmp(tmp[0], tests[i].ct, 16) != 0 || memcmp(tmp[1], tests[i].pt, 16) != 0) {
      delete tf;
      FAIL() << "Test vector " << i;
    }

    /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */
    for (y = 0; y < 16; y++) tmp[0][y] = 0;
    for (y = 0; y < 1000; y++) tf->Encrypt(tmp[0], tmp[0]);
    for (y = 0; y < 1000; y++) tf->Decrypt(tmp[0], tmp[0]);
    for (y = 0; y < 16; y++) if (tmp[0][y] != 0) {delete tf; FAIL() << "Encrypt/Decrypt zeros failed";}

    delete tf;
  }
  SUCCEED();
}
Exemplo n.º 4
0
int _tmain(int argc, _TCHAR* argv[])
{
	AES* AESCipher = NULL;
	string msg;
	string key;
	char* temp_msg_hex;
	char* temp_key_hex;
	int mode = -1;

	while (mode != 0 && mode != 1) {
		cout << "Please choose the AES mode (0 for CBC, 1 for CTR): ";

		cin >> mode;
		
		switch (mode) {
		case 0:
			AESCipher = new AES128CBC();
			break;
		case 1:
			AESCipher = new AES128CTR();
			break;
		}

		cin.clear();
		cin.ignore(numeric_limits<std::streamsize>::max(), '\n');
	}

	if (NULL == AESCipher) {
		cout << "Error allocating memory for cipher engine, program exiting...\n";
		return 0;
	}

	cout << "please provide the key: ";
	cin >> key;

	cout << "please provide the message text: ";
	cin >> msg;
	
	temp_key_hex = new char[key.length() + 1];
	for (int i = 0; i < key.length(); i++) {
		temp_key_hex[i] = key.c_str()[i];
	}
	temp_key_hex[key.length()] = 0;

	temp_msg_hex = new char[msg.length() + 1];
	for (int i = 0; i < msg.length(); i++) {
		temp_msg_hex[i] = msg.c_str()[i];
	}
	temp_msg_hex[msg.length()] = 0;

	AESCipher->setKey(temp_key_hex);

	mode = -1;
	while (/* mode != 0 && */ mode != 1) {
		cout << "Please choose whether you want to encrypt or decrypt (1 for decrypt, encrypt is not ready): ";
		cin >> mode;

		switch (mode) {
		case 0:
			AESCipher->setPT(temp_msg_hex);
			AESCipher->Encrypt();
			break;
		case 1:
			AESCipher->setCT(temp_msg_hex);
			AESCipher->Decrypt();
			break;
		}

		cin.clear();
		cin.ignore(numeric_limits<std::streamsize>::max(), '\n');
	}


	return 0;
}