Re: Decrypting on different platforms

From: Frank Perry (FrankPerry_at_discussions.microsoft.com)
Date: 05/26/05


Date: Thu, 26 May 2005 03:21:04 -0700

Howdy,

I am using the built in CSP.

The code is lifted from the eMbedded Visual C++ help for encrypting and
decrypting data. Here are the sections that give me trouble.

 // Get the handle to the default provider.
  if (!CryptAcquireContext (&hProv, NULL, NULL, PROV_RSA_FULL, 0))
  {
    wprintf (TEXT("Error %x during CryptAcquireContext!\n"),
             GetLastError ());
    goto exit;
  }

  if (lpszPassword == NULL)
  {
    // Encrypt the file with a random session key.

    // Create a random session key.
    if (!CryptGenKey (hProv, CALG_RC2, CRYPT_EXPORTABLE, &hKey))
    {
      wprintf (TEXT("Error %x during CryptGenKey!\n"),
               GetLastError ());
      goto exit;
    }

    // Get the handle to the key exchange public key.
    if (!CryptGetUserKey (hProv, AT_KEYEXCHANGE, &hXchgKey))
    {
      wprintf (TEXT("Error %x during CryptGetUserKey!\n"),
               GetLastError ());
      goto exit;
    }
This is the path with no password. Here it should be giving me a key that I
can export to a blob and then save someplace. At this point, it always fails
saying there is no key.
  ***
  }
  else
  {
    // Encrypt the file with a session key derived from a password.

    // Create a hash object.
    if (!CryptCreateHash (hProv, CALG_MD5, 0, 0, &hHash))
    {
      wprintf (TEXT("Error %x during CryptCreateHash!\n"),
               GetLastError ());
      goto exit;
    }

    // Hash in the password data.
    if (!CryptHashData (hHash, (PBYTE)lpszPassword,
                        wcslen (lpszPassword), 0))
    {
      wprintf (TEXT("Error %x during CryptHashData!\n"),
               GetLastError ());
      goto exit;
    }

    // Derive a session key from the hash object.
    if (!CryptDeriveKey (hProv, CALG_RC2, hHash, 0, &hKey))
    {
      wprintf (TEXT("Error %x during CryptDeriveKey!\n"),
               GetLastError ());
      goto exit;
    }
  }

If I give it a pass word, this section works fine but the same pass word on
the CE machine does not work on the W2K machine. It does work on a different
CE machine. (I have converted both to bytes so the CE machine is not
encrypting Unicode.)

For the first example, I have tried running it on both the systems and get
the same failure. My current thinking is that the is a security setting at
the OS level that is preventing me from exporting a key. If so, the sample
code should say something about it, but doesn't.

For the second, I have looked at the hash and found the hash was different
between the machines. I took the hash from one machine and force the hash in
the other machine to the same value. But that didn't help. I would force
the key but I can't get an exportable key.

Any ideas? As I said, I'm looking at the security settings to see if there
is something that could block me from making an exportable key.

-- 
Frank Perry
LavaLeaf Software
"Ryan Menezes [MSFT]" wrote:
> Which encryption algorithm are you using and what are the default CSPs on 
> those machines ?
> 
> Would definetely help if you can post some sample code.
> 
> Thanks,
> Ryan Menezes [MSFT]
> 
> -
> This posting is provided "AS IS" with no warranties, and confers no rights. 
> "Use of included script samples are subject to the terms specified at 
> http://www.microsoft.com/info/cpyright.htm"
> 
> 
> 
> "Frank Perry" <FrankPerry@discussions.microsoft.com> wrote in message 
> news:03189DBD-7470-4C7A-97D3-50F68764BCED@microsoft.com...
> > Howdy,
> >
> > I forgot to mention that I have both machines using 40 bit key lengths.
> >
> > -- 
> > Frank Perry
> > LavaLeaf Software
> >
> >
> > "Frank Perry" wrote:
> >
> >> Howdy,
> >>
> >> I have been using the sample code for encrypting and decrypting files.  I
> >> have no trouble using the password version to encrypt a file and decrypt 
> >> it
> >> on the same machine.  But when I move the file to a different machine 
> >> with a
> >> different OS, it fails.  The two OSs are W2K and CE.
> >>
> >> So far, I have found the the hash created from the same password is
> >> different on the two machines.  I have tried forcing the same hash on 
> >> both
> >> machines but that doesn't get me anywhere either.  I haven't seen the CE
> >> side, but the KP_IV of the W2K side is all 0's.
> >>
> >> I would try transfering the key itself, but on a different problem, I 
> >> can't
> >> get CryptGetUserKey to return a key.  I am using the code from the 
> >> example
> >> but nothing comes back.  I get the error that there is no key.
> >>
> >> Are there any ideas about either of these problems?
> >>
> >> Thanks in advance.
> >> -- 
> >> Frank Perry
> >> LavaLeaf Software 
> 
> 
> 


Relevant Pages

  • Re: rsa implementation question
    ... > There is a notion of blocks in many public-key ciphers, ... It's not about decrypting to sign, encrypting to ... as it would mean that you'd have to find hash collisions. ... I generate a signature for a string "some string" with SHA. ...
    (comp.lang.python)
  • Re: Truecrypt and ist real security
    ... hash function. ... The faster machine you install AxCrypt ... using it as a key encrypting key as above. ...
    (sci.crypt)
  • Re: Decrypting on different platforms
    ... > wprintf, ... > // Hash in the password data. ... > encrypting Unicode.) ...
    (microsoft.public.platformsdk.security)
  • Re: Encryption/Decryption
    ... For encrypting / decrypting multiple files at once, ... >>> successfully, on the desktop, without Windows CE involved. ... The next> thing>>> to do is to build the "init user" app for Windows CE, and run it, and> see>>> if it succeeds. ...
    (microsoft.public.windowsce.app.development)
  • Re: ADVERT: Secure communications
    ... Hash: SHA1 ... security analysis on it in existence were by its author. ... I'm assuming this means RSA without hybridizing with a symmetric cipher. ... than encrypting a whole message block-by-block with a 2048-bit ...
    (sci.crypt)