PDA

View Full Version : سوال: رمزنگاری AES در ++C



nafisehsh
یک شنبه 18 آبان 1393, 21:35 عصر
سلام دوستان
من میخواستم با استفاده از دو رشته ی عددی و حرفی، کلیدی بسازم که برای رمز کردن فایل پی دی اف از آن استفاده کنم. حال سوالم این است که آیا رمزنگاری AES دارای یک کلید ثابت است یا میتوان بدان مقدار داد؟ اگر می شود از چه دستوری باید استفاده کنم؟
با تشکر

taghanak12
یک شنبه 18 آبان 1393, 21:38 عصر
باسلام
گرامی، به این آدرس مراجعه کنید!
http://cplusplus.ir/site/index.php?option=com_content&view=category&id=5&Itemid=83

silverfox
یک شنبه 18 آبان 1393, 21:58 عصر
کلیدش که ثابت نیست طبیعتا. از چه libraryی استفاده می کنی؟
مثلا اگر از ++crypto استفاده کنی کدت شبیه این می شه:
http://www.cryptopp.com/wiki/Advanced_Encryption_Standard
#include <iostream>
#include <iomanip>

#include "modes.h"
#include "aes.h"
#include "filters.h"

int main(int argc, char* argv[]) {

//Key and IV setup
//AES encryption uses a secret key of a variable length (128-bit, 196-bit or 256-
//bit). This key is secretly exchanged between two parties before communication
//begins. DEFAULT_KEYLENGTH= 16 bytes
byte key[ CryptoPP::AES::DEFAULT_KEYLENGTH ], iv[ CryptoPP::AES::BLOCKSIZE ];
memset( key, 0x00, CryptoPP::AES::DEFAULT_KEYLENGTH );
memset( iv, 0x00, CryptoPP::AES::BLOCKSIZE );

//
// String and Sink setup
//
std::string plaintext = "Now is the time for all good men to come to the aide...";
std::string ciphertext;
std::string decryptedtext;

//
// Dump Plain Text
//
std::cout << "Plain Text (" << plaintext.size() << " bytes)" << std::endl;
std::cout << plaintext;
std::cout << std::endl << std::endl;

//
// Create Cipher Text
//
CryptoPP::AES::Encryption aesEncryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption( aesEncryption, iv );

CryptoPP::StreamTransformationFilter stfEncryptor(cbcEncryption, new CryptoPP::StringSink( ciphertext ) );
stfEncryptor.Put( reinterpret_cast<const unsigned char*>( plaintext.c_str() ), plaintext.length() + 1 );
stfEncryptor.MessageEnd();

//
// Dump Cipher Text
//
std::cout << "Cipher Text (" << ciphertext.size() << " bytes)" << std::endl;

for( int i = 0; i < ciphertext.size(); i++ ) {

std::cout << "0x" << std::hex << (0xFF & static_cast<byte>(ciphertext[i])) << " ";
}

std::cout << std::endl << std::endl;

//
// Decrypt
//
CryptoPP::AES::Decryption aesDecryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption( aesDecryption, iv );

CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink( decryptedtext ) );
stfDecryptor.Put( reinterpret_cast<const unsigned char*>( ciphertext.c_str() ), ciphertext.size() );
stfDecryptor.MessageEnd();

//
// Dump Decrypted Text
//
std::cout << "Decrypted Text: " << std::endl;
std::cout << decryptedtext;
std::cout << std::endl << std::endl;

return 0;
}

nafisehsh
دوشنبه 19 آبان 1393, 07:59 صبح
باسلام
گرامی، به این آدرس مراجعه کنید!
http://cplusplus.ir/site/index.php?option=com_content&view=category&id=5&Itemid=83

ضمن عرض سلام و تشکر از پاسخگویی شما
متاسفانه در لینک زیر مطلبی راجع به مطلب مذکور یافت نشد.
با تشکر

nafisehsh
دوشنبه 19 آبان 1393, 08:04 صبح
کلیدش که ثابت نیست طبیعتا. از چه libraryی استفاده می کنی؟
مثلا اگر از ++crypto استفاده کنی کدت شبیه این می شه:
http://www.cryptopp.com/wiki/Advanced_Encryption_Standard
#include <iostream>
#include <iomanip>

#include "modes.h"
#include "aes.h"
#include "filters.h"

int main(int argc, char* argv[]) {

//Key and IV setup
//AES encryption uses a secret key of a variable length (128-bit, 196-bit or 256-
//bit). This key is secretly exchanged between two parties before communication
//begins. DEFAULT_KEYLENGTH= 16 bytes
byte key[ CryptoPP::AES::DEFAULT_KEYLENGTH ], iv[ CryptoPP::AES::BLOCKSIZE ];
memset( key, 0x00, CryptoPP::AES::DEFAULT_KEYLENGTH );
memset( iv, 0x00, CryptoPP::AES::BLOCKSIZE );

//
// String and Sink setup
//
std::string plaintext = "Now is the time for all good men to come to the aide...";
std::string ciphertext;
std::string decryptedtext;

//
// Dump Plain Text
//
std::cout << "Plain Text (" << plaintext.size() << " bytes)" << std::endl;
std::cout << plaintext;
std::cout << std::endl << std::endl;

//
// Create Cipher Text
//
CryptoPP::AES::Encryption aesEncryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption( aesEncryption, iv );

CryptoPP::StreamTransformationFilter stfEncryptor(cbcEncryption, new CryptoPP::StringSink( ciphertext ) );
stfEncryptor.Put( reinterpret_cast<const unsigned char*>( plaintext.c_str() ), plaintext.length() + 1 );
stfEncryptor.MessageEnd();

//
// Dump Cipher Text
//
std::cout << "Cipher Text (" << ciphertext.size() << " bytes)" << std::endl;

for( int i = 0; i < ciphertext.size(); i++ ) {

std::cout << "0x" << std::hex << (0xFF & static_cast<byte>(ciphertext[i])) << " ";
}

std::cout << std::endl << std::endl;

//
// Decrypt
//
CryptoPP::AES::Decryption aesDecryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption( aesDecryption, iv );

CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink( decryptedtext ) );
stfDecryptor.Put( reinterpret_cast<const unsigned char*>( ciphertext.c_str() ), ciphertext.size() );
stfDecryptor.MessageEnd();

//
// Dump Decrypted Text
//
std::cout << "Decrypted Text: " << std::endl;
std::cout << decryptedtext;
std::cout << std::endl << std::endl;

return 0;
}




ضمن عرض سلام و تشکر از پاسخگویی شما
من هم قصد داشتم از همین library استفاده کنم، منتهی در اینجا نمیتوان برای تولید کلید به آن مقدار داد. من فکر میکنم این ثابت نبودن کلید را خودش به صورت default مقدار میدهد.ممنون میشوم اگر اطلاعات بیشتری در اختیارم بگذارید.
با تشکر

مسعود اقدسی فام
دوشنبه 19 آبان 1393, 09:12 صبح
ضمن عرض سلام و تشکر از پاسخگویی شما
من هم قصد داشتم از همین library استفاده کنم، منتهی در اینجا نمیتوان برای تولید کلید به آن مقدار داد. من فکر میکنم این ثابت نبودن کلید را خودش به صورت default مقدار میدهد.ممنون میشوم اگر اطلاعات بیشتری در اختیارم بگذارید.
با تشکر

بعد از خط چهارده که متغیرهای key و iv تعریف شده، مثل دو خط بعدی می‌شه بهشون مقدار داد. اینجا هر دو رو با 0 پر کرده. شما می‌تونید هر مقدار دیگه‌ای که دوست دارید با هر روشی (از فایل، از ورودی و ...) مقداردهی کنید.