473,320 Members | 1,881 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Porting .NET Encryption to Win32 or ATL

Hi,
I have a piece of code in .NET that encrypts a string.
The .NET code is quite simple and through the last couple of days I've
been trying to build it's equivalent in ATL playing arround with CCrypt
classes and I just haven't been able to get it right.
So if anyone can help me porting this to ATL or Win32 I would be
grateful.
..NET Code:
String* Encrypt(String* PlainText)
{
System::Byte IV[] = { 1, 9, 1, 7, 1, 4, 6, 4 };
System::Byte key[] = Convert::FromBase64String("password");
TripleDES *algorithm = TripleDESCryptoServiceProvider::Create();
MemoryStream *memStream = new MemoryStream();
CryptoStream *cryptoStream = new CryptoStream( memStream,
algorithm->CreateEncryptor( key, IV ),CryptoStreamMode::Write );
StreamWriter* myWriter = new StreamWriter(cryptoStream);
myWriter->Write(PlainText);
myWriter->Flush();
cryptoStream->FlushFinalBlock();
memStream->Flush();
String *strResult =
Convert::ToBase64String(memStream->GetBuffer(),0,(int)memStream->get_Length*());

return strResult;
}

Nov 17 '05 #1
3 1949
"Sygnosys" <cl*****************@gmail.com> wrote

Hi!
I have a piece of code in .NET that encrypts a string.
The .NET code is quite simple and through the last couple of days I've
been trying to build it's equivalent in ATL playing arround with CCrypt
classes and I just haven't been able to get it right.
So if anyone can help me porting this to ATL or Win32 I would be
grateful.
It's not acutally too hard. It's just that the documentation isn't
exactly great. Always look in the docs for the raw Win32 APIs.

First you need to initialize the Crypto API and select
the appropriate CSP.

CCryptProv prov;
CheckHresult( prov.Initialize() );
String* Encrypt(String* PlainText)
{
System::Byte IV[] = { 1, 9, 1, 7, 1, 4, 6, 4 };
System::Byte key[] = Convert::FromBase64String("password");
TripleDES *algorithm = TripleDESCryptoServiceProvider::Create();
MemoryStream *memStream = new MemoryStream();
CryptoStream *cryptoStream = new CryptoStream( memStream,
algorithm->CreateEncryptor( key, IV ),CryptoStreamMode::Write );


I'm not sure that is really what you want. Typically, you would derive
the key from a hash of the password not from the raw password.

This would probably look like
System::Byte key[] = new PasswordDeriveBytes(
Convert::FromBase64String("password"), salt )
->CryptDeriveKey("TripleDES", "SHA1", 192, IV );

Not exactly certain (looks like the documentation is pretty poor here)
I guess - quite contrary to the docs --, salt isn't used at all here and IV
receives the initialization vector. Haven't tried it, though.

If you want it that way, you should use CCryptDerivedKey. Otherwise
(the key is a raw password) you need to use CCryptImportKey.

To construct the CCryptDerivedKey you need a hash of the
key. You get that from the corresponding CCryptXXXHash class.

E.g.:
CCryptSHA1Hash hash;
CheckHresult( hash.Initialize(prov), password );
CCryptDerivedKey key;
CheckHresult( key.Initialize(prov, hash, CALG_3DES ) );

key.Encrypt( ... );

Base64 encoding is also supported by the ATL Server libs.
Look for Base64(En/De)code.

-hg
Nov 17 '05 #2
Thanks for your reply Holger.

I must agree that the .NET algorithm is somewhat "strange" to be kind,
but I do have to suport it in my app. So I want to create a compatible
version in ATL or Win32.
So thanks for your sugestion I will try it and post back my findings
here!

Thanks
Cláudio Albuquerque

Nov 17 '05 #3

Hi,
In the hope that this will help someone in the future.
There goes the details

The ATL classes CCryptImportKey do not work.

So the anwser relies in the microsoft knowlege base article "How to
export and import plain text session keys by using CryptoAPI".

Kind Regards
Cláudio Albuquerque

Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: gaurav khanna | last post by:
Hi I need to store the credit card information in my database. I have been looking for some third party tools which could provide encryption for credit card numbers. The help I need is: a)...
2
by: rawCoder | last post by:
Hi All, I have a *.cer file, a public key of some one and I want to encrypt some thing using this public key. Can someone point me to a sample code for Encrypting some file using...
27
by: MLH | last post by:
Silly me. I thought that if I clicked Tools, Security, Encrypt database MyDB.mdb to Ncrypt.mdb I would not be able to read the module code if opening Ncrypt.mdb inside A97 later. I've found that...
113
by: Bonj | last post by:
I was in need of an encryption algorithm to the following requirements: 1) Must be capable of encrypting strings to a byte array, and decyrpting back again to the same string 2) Must have the same...
5
by: BK-Chicago | last post by:
I am in the midst of porting a massive MFC application from VS6.0 to VS8.0. While i have fixed most of the compile time errors, i do have quite a linker error that i have not been able to resolve....
34
by: subramanian100in | last post by:
Is there any difference between porting and migrating. Kindly explain
133
by: Jean-Pierre Mestre | last post by:
Good evening, I have a C software for Windows that I need to port to Redhat Unix. At the moment it works completely fine with the Windows FLOSS compiler lccwin32. I try gcc but now it doesn't...
47
by: =?Utf-8?B?ZW1hdmlzdQ==?= | last post by:
Dear guys, I'm in trouble having to port my project from C++Builder6 to VisualC++. Has anyone of you idea if there are any tools to help my doing this job? My current project is widely using VCL...
1
by: viren.chaudhary2008 | last post by:
I have just few questions. We are thinking of porting our 32 bit application to 64 bit as we need to use more memory usage and want to take advantage of 64 bit processor. Currently my applications...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.