Hello!

I have the same problem.

Encoded (with JAVA) public key in a file and no chance to import the
key with crypto++

Please help


On 5 Sep. 2003, 19:53, Scott Barnhart <[EMAIL PROTECTED]>
wrote:
> Hi,
>
> I am trying to use CryptoPP 5.1 to decode files encoded with the Java crypto 
> classes using Diffie-Hellman keys and DESede encryption (CBC mode, PKCS5 
> padding).  The problem I can not figure out at this point is how to load a 
> 1024-bit Diffie-Hellman public or private key from a file, where the public 
> key is in X509 format, and the private key is PKCS#8 format.  The DH::Agree() 
> method wants byte* arguments for the keys, in (I assume) an un-encoded 
> format.  I am getting totally lost in how to use all the different template 
> classes and arguments to template classes to construct what I want.  All the 
> DH sample code shows generating keys, and then using those same keys 
> immediately to do the DH agreement.  But I can't find any code which shows 
> how to load DH keys from a file system.
>
> Can someone please show me some code or some pointers in the right direction 
> on how to do this?  I have used CryptoPP for some other stuff 
> (DefaultEncryptorWithMAC), and I understand Sources, Sinks, and 
> Transformations pretty well, but I can NOT figure this out.  Anyhelpwould be 
> greatly appreciated.
>
> Here is the Java code I use to generate and save the DH keys:
>
>     DHParameterSpec dhSkipParamSpec = new DHParameterSpec(skip1024Modulus, 
> skip1024Base);
>     KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("DH");
>     keyPairGen.initialize(dhSkipParamSpec);
>     KeyPair keyPair = keyPairGen.generateKeyPair();
>
>     writeKey(keyPair.getPublic(),  "Public.bin");
>     writeKey(keyPair.getPrivate(), "Private.bin");
>
>     private void writeKey(Key key, String strName) throws Exception
>     {
>         FileOutputStream fKey = new FileOutputStream(strName);
>         fKey.write(key.getEncoded());   // java getEncoded() returns X509 
> format for public key, PKCS#8 for private
>         fKey.close();
>     }
>
> The java code to encode looks like:
>
>     private void run(String strPriFile, String strPubFile, String 
> strFileToEncode) throws Exception
>     {
>         KeyFactory facDH = KeyFactory.getInstance("DH");
>         KeySpec pubKeySpec = new X509EncodedKeySpec(getRawKey(strPubFile));
>         KeySpec priKeySpec = new PKCS8EncodedKeySpec(getRawKey(strPriFile));
>
>         PublicKey pubKey  = facDH.generatePublic(pubKeySpec);
>         PrivateKey priKey = facDH.generatePrivate(priKeySpec);
>
>         KeyAgreement keyAgree = KeyAgreement.getInstance("DH");
>         keyAgree.init(priKey);
>         keyAgree.doPhase(pubKey, true);
>         SecretKey keySecret = keyAgree.generateSecret("DESede");     // 
> Transforms the DH agreed secret value into a 168-bit 3DES key.
>
>         Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
>         cipher.init(Cipher.ENCRYPT_MODE, keySecret);
>
>         FileInputStream  fIn  = new FileInputStream(strFileToEncode);
>         DataOutputStream fOut = new DataOutputStream(new 
> FileOutputStream(strFileToEncode + ".enc"));
>
>         byte[] iv = cipher.getIV(); // get the initialization vector
>         fOut.write(iv); // write IV
>
>         // Read from the input and write to the encrypting output stream
>         byte[] buffer = new byte[BUFSIZE];
>         int bytesRead;
>         while((bytesRead = fIn.read(buffer)) != -1)
>         {
>             fOut.write(cipher.update(buffer, 0, bytesRead));
>         }
>         fOut.write(cipher.doFinal());
>         fOut.close();
>         fIn.close();
>     }
>
>     private byte[] getRawKey(String strFile) throws Exception
>     {
>         File keyfile = new File(strFile);
>         DataInputStream in = new DataInputStream(new 
> FileInputStream(keyfile));
>         byte[] rawkey = new byte[(int)keyfile.length()];
>         in.readFully(rawkey);
>         in.close();
>         return rawkey;
>     }
>
> Thanks,
> Scott Barnhart
> [EMAIL PROTECTED]


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the "Crypto++ Users" 
Google Group.
To unsubscribe, send an email to [EMAIL PROTECTED]
More information about Crypto++ and this group is available at 
http://www.cryptopp.com.
-~----------~----~----~----~------~----~------~--~---

Reply via email to