Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Crypto - RC4
Message
De
02/03/2003 06:07:45
 
 
À
01/03/2003 19:13:04
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Titre:
Divers
Thread ID:
00759999
Message ID:
00760095
Vues:
28
>>Anyone knows how to make a key from a string.
>>
>>I have a key ( string ) which I want to use for a RC4 encryption. The problem I have is to set up a key (using CryptImportKey() I guess). Once I have set up the key I can call either CryptEncrypt() or CryptDecrypt().
>>
>>I am generating a small pdf file which I would like to protect from modifying
>>
>>Now I have a little program in vfp that does the RC4 encryption ( rc4(rc4Key, src) ). I would prefer to use the crypto functions instead since they will be much faster.

>
>I'm not sure I understand your problem.
>Does this help?
>
>oCrypto = NEWOBJECT("_cryptapi",HOME() + "ffc\_crypt.vcx")
>cEncryptedStream=SPACE(255)
>oCrypto.EncryptSessionStreamString(cEncryptedString, cPassWord, @cEncryptedStream)
>* now cEncryptedStream has your encrypted string.
>
>
>If what you are looking for is to encrypt a whole pdf file, then you could use something like this:
>
>oCrypto = NEWOBJECT("_cryptapi",HOME() + "ffc\_crypt.vcx")
>oCrypto.encryptsessionstreamfile("temp\test.pdf",cPassWord,"test.pdf.enc")
>* now you have a resulting, encrypted file (original is left untouched)
>* this will reverse it:
>oCrypto.decryptsessionstreamfile("test.pdf.enc",cPassWord,"test.pdf")
>
>
>HTH

Alex,

Thanks for the answer. I am still on vfp6 (though I have vfp7 in my cupboard since a few months, haven't had the time yet), so I have not got ffc\_crypt.vcx.

To explain further. I am not after encrypting a whole pdf file. The pdf I produce and encrypt must be read by AcrobatReader. Hence, I have to adhere to their principles

In a pdf file, the strings and streams may be encrypted to protect from modification, printing, and the like.

This is done with an algorithm described in the pdf reference manual ( algorithm below, manual is +/- 1000 pages http://partners.adobe.com/asn/developer/acrosdk/docs/filefmtspecs/PDFReference.pdf (page 73)

Where I am stuck is point 4.:

Algorithm 3.1 Encryption of data using an encryption key

1. Obtain the object number and generation number from the object identifier of
the string or stream to be encrypted (see Section 3.2.9, “Indirect Objects”). If the
string is a direct object, use the identifier of the indirect object containing it.

2. Treating the object number and generation number as binary integers, extend the
original n-byte encryption key to n + 5 bytes by appending the low-order 3 bytes
of the object number and the low-order 2 bytes of the generation number in that
order, low-order byte first. (n is 5 unless the value of V in the encryption dictionary
is greater than 1, in which case n is the value of Length divided by 8.)

3. Initialize the MD5 hash function and pass the result of step 2 as input to this function.

4. Use the first (n + 5) bytes, up to a maximum of 16, of the output from the MD5
hash as the key for the RC4 encryption function, along with the string or stream
data to be encrypted. The output is the encrypted data to be stored in the PDF file.

I can use the crypto API to generate the Encryption key as a start for point 1. (page 78, algorithm 3.2).
From point 1. I can get to 3. using crypto api

The problem is point 4 where I have to feed a variable length key to the RC4. That key is a part of the key generated in point 3.

I can do that with a program, I just do not know how to get that key into the crypto api, so that the CryptoEncrypt/CryptoDecrypt uses that key.

As far as I can see, CryptDeriveKey() won't work.

Just hoped there might be a possibility. If there isn't, tough, I'll use the program

as an example (all in hex)
IN   3EF00938B4C591A254B911A5D25432F8B73848CB
KEY  CCF0A3B5D1A558E99ED3
Out  4889AC555B6FDA30187DCFAFF0237BA8E3FB65AA
Of course, I may have overlooked something
Gregory
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform