How to convert char * to Byte[] in TripleDES class  
Author Message
BearPalm





PostPosted: Visual C++ Express Edition, How to convert char * to Byte[] in TripleDES class Top

Hello, everybody.

I am developing a crypt class for existing MFC application

in .NET framework. Parameters of encryption and decryption

functions must be "char *" type. The tripleDES method is used

to encode and decode. However, I failed to convert char * to Byte[]

during cryption. Could anybody help me how to solve this issue

Thank you in advance.

Jeff

/**************RUNTIME ERROR****************/

Unhandled Exception:

System.Security.Cryptography.CryptographicException:

Length of the data to decrypt is invalid.

at System.Security.Cryptography.CryptoAPITransform.TransformFinalBlock

(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount)

at System.Security.Cryptography.CryptoStram.FlushFinalBlock()

at CCrypto.CryptDecode3Des(SByte *enc, SByte *dec)

/*************Source Code************************/

#include <tchar.h>

#using <mscorlib.dll>

using namespace System;

using namespace System::IO;

using namespace System::Text;

using namespace System::Collections;

using namespace Security::Cryptography;

using namespace System::Runtime::InteropServices;

/* Triple DES */

/* txt: [IN] Plain Text

* enc: [OUT] encrpted string

*/

int CCrypto::CryptEncode3Des(char * txt, char * enc)

{

Byte key[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24};

Byte iv[] = {65, 110, 68, 26, 69, 178, 200, 219};

int cnt, encnt;

//Byte encodedBytes[];

String *str = new String(txt);

UTF8Encoding *utf8 = new UTF8Encoding();

//Transform the plainText into a Byte array.

Byte encodedBytes[] = utf8->GetBytes(str);

//Create a new TripleDES service provider

TripleDESCryptoServiceProvider *tdes = new TripleDESCryptoServiceProvider();

//along with encryption key and init vector

ICryptoTransform *transformer = tdes->Crea****cryptor(key, iv);

// Provide a memory stream to encrypt information into stream

MemoryStream *encryptedStream = new MemoryStream();

CryptoStream *cryptStream = new CryptoStream(encryptedStream, transformer, CryptoStreamMode::Write);

//Write the encrypted information to the stream

cryptStream->Write(encodedBytes, 0, encodedBytes.Length);

cryptStream->FlushFinalBlock();

encryptedStream->Position = 0;

//Read the stream back into a Byte array

Byte result[] = new Byte[encryptedStream->Length];

encryptedStream->Read(result, 0, encryptedStream->Length);

UTF8Encoding *enutf8 = new UTF8Encoding();

sprintf(enc, "%s", enutf8->GetString(result));

cryptStream->Close();

return 0;

}

// decode an encrypted text

// enc:[IN] encrypted string

// dec:[OUT] decrypted string

// return 0: successful, others: error code

int CCrypto::CryptDecode3Des(char * enc, char * dec)

{

Byte key[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24};

Byte iv[] = {65, 110, 68, 26, 69, 178, 200, 219};

String *str = new String(enc);

int cnt;

UTF8Encoding *utf8 = new UTF8Encoding();

Byte decodedBytes[] = utf8->GetBytes(str);//here is wrong

//Create a new TripleDES service provider

TripleDESCryptoServiceProvider *tdes = new TripleDESCryptoServiceProvider();

//along with encryption key and init vector

ICryptoTransform *transformer = tdes->CreateDecryptor(key, iv);

// Provide a memory stream to encrypt information into stream

MemoryStream *decryptedStream = new MemoryStream();

decryptedStream->set_Capacity(arr_len);

CryptoStream *cryptStream = new CryptoStream(decryptedStream, transformer, CryptoStreamMode::Write);

//Write the decrypted information to the stream

cryptStream->Write(decodedBytes, 0, decodedBytes.Length);

cryptStream->FlushFinalBlock();

decryptedStream->Position = 0;

//Read the stream back into a Byte array

Byte result[] = new Byte[(int)decryptedStream->Length];

decryptedStream->Read(result, 0, decryptedStream->Length);

//

UTF8Encoding *deutf8 = new UTF8Encoding();

sprintf(dec, "%s", deutf8->GetString(result));

cryptStream->Close();

return 0;

}




Visual Studio Express Editions11  
 
 
nobugz





PostPosted: Visual C++ Express Edition, How to convert char * to Byte[] in TripleDES class Top

You are passing cryptStream->Write() an array of bytes that has 0 elements. Don't initialize decodedBytes with utf8->GetBytes() but with new Byte[ENOUGH] where ENOUGH is large enough to handle any decoding.

What kind of code is this BTW It doesn't look like C++/CLI (new instead of gcnew, * instead of ^)...