engos
(usa openSUSE)
Enviado em 07/05/2008 - 12:25h
Sim, usa a biblioteca Crypto++ ou CryptoPP.
Vou ver se coloco algo como um tutorial ou script, mas segue um exemplo que fiz para facilitar o estudo:
#include "files.h"
#include "hex.h"
#include "osrng.h" // PRNG
StreamTransformation
#include "base32.h" // Base32
#include "modes.h" // CBC_Mode< >
using namespace CryptoPP;
using std::string;
using std::cout;
using std::endl;
// The Features Available...
const unsigned int FEATURE_EVALUATION = 0x01; // 0000 0001
const unsigned int FEATURE_USE_SQUARES = 0x02; // 0000 0010
const unsigned int FEATURE_USE_CIRCLES = 0x04; // 0000 0100
const unsigned int FEATURE_USE_WIDGETS = 0x08; // 0000 1000
// Span Byte Boundary...
const unsigned int FEATURE_USE_ELLIPSES = 0x0100; // 0000 0001 0000 0000
const unsigned int FEATURE_USE_TRIANGLES = 0x0200; // 0000 0010 0000 0000
const unsigned int FEATURE_USE_POLYGONS = 0x0400; // 0000 0100 0000 0000
const unsigned int FEATURE_USE_PENTAGONS = 0x0800; // 0000 1000 0000 0000
// 1010 1010 ... 1010 1010
const unsigned int FEATURE_MAGIC = 0xAAAAAAAA;
void PrintPrologue( std::string algorithm, byte* key, int keylen, byte* iv, int ivlen );
void EncodeFeatures( std::string& FeatureText, unsigned int features );
void PrintFeatures( unsigned int features );
class CryptGeneration {
private:
string _Option;
string _File;
public:
/// Constructor
CryptGeneration( string Option, string File ){
_Option = Option;
_File = File;
}
/// Return Serial
bool Crypt( string& File ) {
File = this->_File + "\n\n";
return true;
}
};
int main( int argc, char* argv[] ) {
string Option;
string File;
string ret;
if( argc != 3 ) {
cout << "Usage: " << argv[0] << " [OPTION] \"File\"";
cout << "\n\t[OPTION]\tCrypt - To crypt the file";
cout << "\n\t \tDescrypt - To descrypt the file\n\n";
return 1;
}
Option = argv[1];
File = argv[2];
if( Option.compare( "Crypt" ) && Option.compare( "Descrypt" ) ) {
cout << "Usage: " << argv[0] << " [OPTION] \"File\"";
cout << "\n\t[OPTION]\tCrypt - To crypt the file";
cout << "\n\t \tDescrypt - To descrypt the file\n\n";
return 2;
}
CryptGeneration cg( Option, File );
cg.Crypt( ret );
cout << ret;
// Key and IV setup
byte key[ CryptoPP::AES::DEFAULT_KEYLENGTH ] =
{ 0x93, 0x33, 0x6B, 0x82, 0xD6, 0x64, 0xB2, 0x46,
0x95, 0xAB, 0x89, 0x91, 0xD3, 0xE5, 0xDC, 0xB0 };
byte iv[ CryptoPP::AES::BLOCKSIZE ] =
{ 0x61, 0x4D, 0xCA, 0x6F, 0xB2, 0x56, 0xF1, 0xDB,
0x0B, 0x24, 0x5D, 0xCF, 0xB4, 0xBD, 0xB6, 0xD3 };
// Pseudo Random Number Generator
CryptoPP::AutoSeededRandomPool rng;
// Encryptor
CryptoPP::CBC_Mode<CryptoPP::AES>::Encryption Encryptor( key, sizeof(key), iv );
// Decryptor
CryptoPP::CBC_Mode<CryptoPP::AES>::Decryption Decryptor( key, sizeof(key), iv );
// Magic
const std::string MagicText( 4, (char)0xAA );
//////////////////////////////////////////
// Output //
//////////////////////////////////////////
PrintPrologue( Encryptor.AlgorithmName(), key, sizeof(key), iv, sizeof(iv) );
///////////////////////////////////////////
// Generation Loop //
///////////////////////////////////////////
unsigned int ITERATIONS = 3;
for( unsigned int i = 0; i < ITERATIONS; i++ ) {
string FeatureText = "Hello World!";
string SaltText = "";
string EncodedText = "";
Encryptor.Resynchronize( iv );
Decryptor.Resynchronize( iv );
// Salt - RandomNumberSource
CryptoPP::RandomNumberSource( rng, 4, true, new CryptoPP::StringSink( SaltText ) );
// Features
// No Longer Random
unsigned int Features = 0;
Features |= FEATURE_USE_ELLIPSES;
Features |= FEATURE_USE_PENTAGONS;
EncodeFeatures( FeatureText, Features );
// Encryption
CryptoPP::StringSource( SaltText + MagicText + FeatureText, true, new CryptoPP::StreamTransformationFilter( Encryptor, new CryptoPP::Base32Encoder( new CryptoPP::StringSink( EncodedText ), true, 4, "-") ) );
// Add Appendage for Pretty Printing
EncodedText += "JW";
//////////////////////////////////////////
// Output //
//////////////////////////////////////////
cout << EncodedText << endl;
//////////////////////////////////////////
// DMZ //
//////////////////////////////////////////
// Recovered Text Sink
std::string RecoveredText = "";
// Remove Appendage for Pretty Printing
EncodedText = EncodedText.substr( 0, EncodedText.length() - 2 );
CryptoPP::StringSource( EncodedText, true, new CryptoPP::Base32Decoder( new CryptoPP::StreamTransformationFilter( Decryptor, new CryptoPP::StringSink( RecoveredText ) ) ) );
// Salt
unsigned int RecoveredSalt = *( (unsigned int*)(RecoveredText.substr( 0, 4 ).data() ) );
// Step Over Salt
assert( RecoveredText.length() >= 4 );
RecoveredText = RecoveredText.substr( 4 );
// Magic
unsigned int RecoveredMagic = *( (unsigned int*)(RecoveredText.substr( 0, 4 ).data() ) );
// Step Over Magic
assert( RecoveredText.length() >= 4 );
RecoveredText = RecoveredText.substr( 4 );
//////////////////////////////////////////
// Key Tampering? //
//////////////////////////////////////////
assert( FEATURE_MAGIC == RecoveredMagic );
// Features
unsigned int RecoveredFeatures =
*( (unsigned int*)(RecoveredText.substr( 0, 4 ).data() ) );
// Step over Features
assert( RecoveredText.length() >= 4 );
RecoveredText = RecoveredText.substr( 4 );
//////////////////////////////////////////
// Output //
//////////////////////////////////////////
PrintFeatures( RecoveredFeatures );
} // for( ITERATIONS )
return 0;
}
void EncodeFeatures( std::string& FeatureText, unsigned int features )
{
assert( 4 == sizeof( unsigned int ) );
char c = '{TTEXTO}';
c = ( features >> 0 ) & 0xFF;
FeatureText += c;
c = ( features >> 8 ) & 0xFF;
FeatureText += c;
c = ( features >> 16 ) & 0xFF;
FeatureText += c;
c = ( features >> 24 ) & 0xFF;
FeatureText += c;
}
void PrintPrologue( std::string algorithm, byte* key,
int keylen, byte* iv, int ivlen )
{
std::string HexKey, HexIV;
CryptoPP::HexEncoder KeyEncoder( NULL, true, 2 );
KeyEncoder.Attach( new CryptoPP::StringSink( HexKey ) );
KeyEncoder.PutMessageEnd( key, keylen );
CryptoPP::HexEncoder IVEncoder( NULL, true, 2 );
IVEncoder.Attach( new CryptoPP::StringSink( HexIV ) );
IVEncoder.PutMessageEnd( iv, ivlen );
cout << algorithm << endl;
cout << "key[] = " << HexKey << endl;
cout << " iv[] = " << HexIV << endl;
cout << endl;
}
void PrintFeatures( unsigned int features )
{
if( FEATURE_EVALUATION == ( features & FEATURE_EVALUATION ) )
{ cout << "Evaluation Edition" << endl; }
if( FEATURE_USE_SQUARES == ( features & FEATURE_USE_SQUARES ) )
{ cout << "Operations are permitted on Squares" << endl; }
if( FEATURE_USE_CIRCLES == ( features & FEATURE_USE_CIRCLES ) )
{ cout << "Operations are permitted on Circles" << endl; }
if( FEATURE_USE_WIDGETS == ( features & FEATURE_USE_WIDGETS ) )
{ cout << "Operations are permitted on Widgets" << endl; }
if( FEATURE_USE_ELLIPSES == ( features & FEATURE_USE_ELLIPSES ) )
{ cout << "Operations are permitted on Ellipses" << endl; }
if( FEATURE_USE_POLYGONS == ( features & FEATURE_USE_POLYGONS ) )
{ cout << "Operations are permitted on Polygons" << endl; }
if( FEATURE_USE_TRIANGLES == ( features & FEATURE_USE_TRIANGLES ) )
{ cout << "Operations are permitted on Triangles" << endl; }
if( FEATURE_USE_PENTAGONS == ( features & FEATURE_USE_PENTAGONS ) )
{ cout << "Operations are permitted on Pentagons" << endl; }
cout << endl;
}