I'm trying to write a function that will take a buffer of standard type, 
en/decrypt it and return a buffer with the result.  I'd rather avoid having 
to memcpy the result.  This toy code is only supposed to test getting a 
buffer with encrypted data back.  This is linked against code from the git 
as of 20171118.

But what I get from the code is:
$ ./threefish256tryArrayCall2 |hexdump -C |head
00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  
|................|
*
00001000  00 00 fa 00 00 00 00 00  00 00 00 00 00 00 00 00  
|................|
00001010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  
|................|
*
00002000

which shows the probem is not with output to stdout or returning the buffer 
from the function call.

Code:

// threefish256tryArrayCall2.cpp

// g++ -fpermissive -std=c++11 -m64 -g3 -O2 -Wall -Wextra  
threefish256tryArrayCall2.cpp -o threefish256tryArrayCall2 
-I/usr/local/include/cryptopp -I./ -L/usr/local/lib -lcryptopp


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <iostream>
#include <ostream>
#include <string>
#include <fstream>
#include <array>
#include <vector>

using namespace std;

// Crypto++ Includes
#include "cryptlib.h"
#include "modes.h"      // xxx_Mode< >
#include "filters.h"    // StringSource and
#include "threefish.h"
#include <cryptopp/files.h>

#define CIPHER_MODE CTR_Mode
#define CIPHER Threefish256

#define BLOCKSIZE 32
#define KEY_LENGTH 32
#define IV_LENGTH 32

using namespace CryptoPP;

extern FILE *stderr;
extern FILE *stdin;
extern FILE *stdout;


const int arraylen    = 4096;
   
array<byte, arraylen> procBuf ( array<byte, arraylen> PlainTextB, 
         array<byte, arraylen> CipherTextB, 
         int rbytes,
                 CryptoPP::CTR_Mode<CryptoPP::CIPHER >::Encryption 
Encryptor ) 
{
  
  ArraySink vsink( &CipherTextB[0], CipherTextB.size() );

  CryptoPP::StreamTransformationFilter* enc 
       = new CryptoPP::StreamTransformationFilter( Encryptor, new 
Redirector(vsink) );

  CryptoPP::ArraySource(&PlainTextB[0], rbytes, enc);
  
//   fwrite ( &CipherTextB[0], 1, rbytes, stdout );
//   fwrite ( CipherTextB.data(), 1, rbytes, stdout );

  return CipherTextB;

}

int main(int argc, char* argv[]) {
    
//     array<byte, arraylen> PlainTextB, CipherTextB;
    array<unsigned char, arraylen> PlainTextB, CipherTextB;

    
    CryptoPP::byte key[ KEY_LENGTH ], 
                    iv[ IV_LENGTH ];

    ::memset( key, 0xf1, KEY_LENGTH );
    ::memset(  iv, 0x01, IV_LENGTH );
    
    
    // Encryptor
    CryptoPP::CTR_Mode<CryptoPP::CIPHER >::Encryption
        Encryptor; //( key, CryptoPP::CIPHER::MAX_KEYLENGTH, iv, IV_LENGTH 
);

    Encryptor.SetKeyWithIV(key, KEY_LENGTH, iv, IV_LENGTH);

int i=0;
int rbytes=4096;
// while ( i < 4 ) {
  
    int buflen    = arraylen;
    
    ::memset(&CipherTextB[0] , 0x00, rbytes );
    ::memset(&PlainTextB[0] , 0x00, rbytes );
    
    //something distinct:
    PlainTextB[2]=0xfa;
  
    CipherTextB = procBuf ( PlainTextB, CipherTextB, rbytes, Encryptor );
    
fwrite ( &CipherTextB[0], 1, buflen, stdout );
fwrite ( &(PlainTextB.data())[0], 1, buflen, stdout );

       i++;
// }

return 0;

}


-- 
-- 
You received this message because you are subscribed to the "Crypto++ Users" 
Google Group.
To unsubscribe, send an email to [email protected].
More information about Crypto++ and this group is available at 
http://www.cryptopp.com.
--- 
You received this message because you are subscribed to the Google Groups 
"Crypto++ Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to