Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. How to encrypt and decrypt a const char* in winrt

How to encrypt and decrypt a const char* in winrt

Scheduled Pinned Locked Moved ATL / WTL / STL
graphicsalgorithmsdata-structuresregexhelp
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    yourchandrashekhar gmail com
    wrote on last edited by
    #1

    Hi all, I have been trying to write encrypt and decrypt functions whose signatures require the input and the output strings to be in 'void*' type only. The code works fine if the inputs can be specified as IBuffer^ but in the other case the source string and the encrypted->decrypted string do not match. I've googled a lot but to no good. Please help...it's urgent.

    IBuffer^ byteArrayToIBufferPtr(byte *source, int size)
    {
    Platform::ArrayReference blobArray(source, size);
    IBuffer ^buffer = CryptographicBuffer::CreateFromByteArray(blobArray);
    return buffer;
    }

    byte* IBufferPtrToByteArray(IBuffer ^buffer)
    {
    Array ^platArray = ref new Array(256);
    CryptographicBuffer::CopyToByteArray(buffer,&platArray);

    byte \*dest = platArray->Data;
    return dest;
    

    }

    int DataEncryption::encryptData(EncryptionAlgorithm algo, int keySize, void* srcData, const unsigned int srcSize,
    void*& encData, unsigned int& encSize)
    {

    LOG\_D(TAG, "encryptData()");
    
    if(srcData == nullptr)
    {
    	LOG\_E(TAG,"");
    	return DataEncryption::RESULT\_EMPTY\_DATA\_ERROR;
    }
    if(srcSize == 0)
    {
    	LOG\_E(TAG,"");
    	return DataEncryption::RESULT\_SIZE\_ZERO\_ERROR;
    }
    
    IBuffer^ encrypted;
    IBuffer^ buffer;
    IBuffer^ iv = nullptr;
    String^ algName;
    bool cbc = false;
    
    switch (algo)
    {
    case DataEncryption::ENC\_DEFAULT:
    	algName = "AES\_CBC";
    	cbc = true;
    	break;
    default:
    	break;
    }
    
    // Open the algorithm provider for the algorithm specified on input.
    SymmetricKeyAlgorithmProvider^ Algorithm = SymmetricKeyAlgorithmProvider::OpenAlgorithm(algName);
    
    // Generate a symmetric key.
    IBuffer^ keymaterial = CryptographicBuffer::GenerateRandom((keySize + 7) / 8);
    CryptographicKey^ key;
    
    try
    {
        key = Algorithm->CreateSymmetricKey(keymaterial);
    }
    catch(InvalidArgumentException^ e)
    {
    	LOG\_E(TAG,"encryptData(): Could not create key.");
    	return DataEncryption::RESULT\_ERROR;
    }
    
    // CBC mode needs Initialization vector, here just random data.
    // IV property will be set on "Encrypted".
    if (cbc)
        iv = CryptographicBuffer::GenerateRandom(Algorithm->BlockLength);
    
    // Set the data to encrypt. 
    IBuffer ^srcDataBuffer = byteArrayToIBufferPtr(static\_cast(srcData),256);
    
    // Encrypt and create an authenticated tag.
    encrypted = CryptographicEngine::Encrypt(key, srcDataBuffer, iv);
    
    //encData = encrypted;
    byte \*bb = IBufferPtrToByteArray(en
    
    L 1 Reply Last reply
    0
    • Y yourchandrashekhar gmail com

      Hi all, I have been trying to write encrypt and decrypt functions whose signatures require the input and the output strings to be in 'void*' type only. The code works fine if the inputs can be specified as IBuffer^ but in the other case the source string and the encrypted->decrypted string do not match. I've googled a lot but to no good. Please help...it's urgent.

      IBuffer^ byteArrayToIBufferPtr(byte *source, int size)
      {
      Platform::ArrayReference blobArray(source, size);
      IBuffer ^buffer = CryptographicBuffer::CreateFromByteArray(blobArray);
      return buffer;
      }

      byte* IBufferPtrToByteArray(IBuffer ^buffer)
      {
      Array ^platArray = ref new Array(256);
      CryptographicBuffer::CopyToByteArray(buffer,&platArray);

      byte \*dest = platArray->Data;
      return dest;
      

      }

      int DataEncryption::encryptData(EncryptionAlgorithm algo, int keySize, void* srcData, const unsigned int srcSize,
      void*& encData, unsigned int& encSize)
      {

      LOG\_D(TAG, "encryptData()");
      
      if(srcData == nullptr)
      {
      	LOG\_E(TAG,"");
      	return DataEncryption::RESULT\_EMPTY\_DATA\_ERROR;
      }
      if(srcSize == 0)
      {
      	LOG\_E(TAG,"");
      	return DataEncryption::RESULT\_SIZE\_ZERO\_ERROR;
      }
      
      IBuffer^ encrypted;
      IBuffer^ buffer;
      IBuffer^ iv = nullptr;
      String^ algName;
      bool cbc = false;
      
      switch (algo)
      {
      case DataEncryption::ENC\_DEFAULT:
      	algName = "AES\_CBC";
      	cbc = true;
      	break;
      default:
      	break;
      }
      
      // Open the algorithm provider for the algorithm specified on input.
      SymmetricKeyAlgorithmProvider^ Algorithm = SymmetricKeyAlgorithmProvider::OpenAlgorithm(algName);
      
      // Generate a symmetric key.
      IBuffer^ keymaterial = CryptographicBuffer::GenerateRandom((keySize + 7) / 8);
      CryptographicKey^ key;
      
      try
      {
          key = Algorithm->CreateSymmetricKey(keymaterial);
      }
      catch(InvalidArgumentException^ e)
      {
      	LOG\_E(TAG,"encryptData(): Could not create key.");
      	return DataEncryption::RESULT\_ERROR;
      }
      
      // CBC mode needs Initialization vector, here just random data.
      // IV property will be set on "Encrypted".
      if (cbc)
          iv = CryptographicBuffer::GenerateRandom(Algorithm->BlockLength);
      
      // Set the data to encrypt. 
      IBuffer ^srcDataBuffer = byteArrayToIBufferPtr(static\_cast(srcData),256);
      
      // Encrypt and create an authenticated tag.
      encrypted = CryptographicEngine::Encrypt(key, srcDataBuffer, iv);
      
      //encData = encrypted;
      byte \*bb = IBufferPtrToByteArray(en
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You need to provide more information about the problem, what does not work, and which lines of code are giving the problem.

      Use the best guess

      Y 1 Reply Last reply
      0
      • L Lost User

        You need to provide more information about the problem, what does not work, and which lines of code are giving the problem.

        Use the best guess

        Y Offline
        Y Offline
        yourchandrashekhar gmail com
        wrote on last edited by
        #3

        Hi Richards, Thanks for your response. I found the problem. First let me describe the issue: Suppose my source string(say 'pData') is "SampleTextSample" and I encrypt it using encryptData() which stores the encrypted string in 'encryptedData'(suppose). Then I call decryptData() with 'encryptedData' as the input and in turn decryptData() stores the decrypted string in 'decryptedData'(suppose). Now logically, pData and decryptedData should be equal. But this is not happenning in my code. Error: Using CryptographicBuffer::GenerateRandom() for creating key and initialazation vector in both encryptData() and decryptData(). Thus I am using a different key and initialization vector in encryptData() and another key and initialization vector in decryptData(). Hence the results don't match. Solution: Arranged to use the same key in both encryptData() and decryptData().

        L 1 Reply Last reply
        0
        • Y yourchandrashekhar gmail com

          Hi Richards, Thanks for your response. I found the problem. First let me describe the issue: Suppose my source string(say 'pData') is "SampleTextSample" and I encrypt it using encryptData() which stores the encrypted string in 'encryptedData'(suppose). Then I call decryptData() with 'encryptedData' as the input and in turn decryptData() stores the decrypted string in 'decryptedData'(suppose). Now logically, pData and decryptedData should be equal. But this is not happenning in my code. Error: Using CryptographicBuffer::GenerateRandom() for creating key and initialazation vector in both encryptData() and decryptData(). Thus I am using a different key and initialization vector in encryptData() and another key and initialization vector in decryptData(). Hence the results don't match. Solution: Arranged to use the same key in both encryptData() and decryptData().

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          yourchandrashekhar@gmail.com wrote:

          and another key and initialization vector in decryptData().

          Why would you expect it to work with different keys?

          Use the best guess

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups