Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / November 2005

Tip: Looking for answers? Try searching our database.

Porting .NET Encryption to Win32 or ATL

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Sygnosys - 28 Oct 2005 10:03 GMT
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;
}
Holger Grund - 30 Oct 2005 19:39 GMT
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)
> {
[quoted text clipped - 5 lines]
> 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
sygnosys@gmail.com - 31 Oct 2005 21:03 GMT
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
Sygnosys - 03 Nov 2005 23:33 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.