Results 1 to 2 of 2
  1. #1
    heinkoi459 is offline Member
    Join Date
    Jun 2013
    Posts
    1
    Rep Power
    0

    Default Translate Decrypting string in Java using Microsoft Crypto API

    Hello everyone, I have tried to translate this code from C++ to java and nothing works. Any can help me please?

    C++

    Java Code:
    std::string &sCryptKeySeed;    // My encryption/decryption password
    
    HCRYPTPROV hCryptProv = NULL;
    HCRYPTHASH hHash      = NULL;
    HCRYPTKEY  hCryptKey  = NULL;
    DWORD iLen            = iSize;
    char *pData           = NULL;
    
    if(CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_MACHINE_KEYSET))
    {
        if(CryptCreateHash(hCryptProv, CALG_SHA1, 0, 0, &hHash))
        {
            if(CryptHashData(hHash, (BYTE *)sCryptKeySeed.c_str(), sCryptKeySeed.length(), 0))
            {
                if(CryptDeriveKey(hCryptProv, CALG_RC2, hHash, 0, &hCryptKey))
                {
                    if(CryptDecrypt(hCryptKey, NULL, TRUE, 0, (BYTE *)pData, &iLen))
                    {
                        sValue.assign(pData);
                    }
                }
            }
            CryptDestroyHash(hHash);
            hHash = NULL;
        }
        CryptReleaseContext(hCryptProv, 0);
        hCryptProv = NULL;
    }
    What I have tried and nothing works...

    Java

    Java Code:
    byte[] password = sCryptKeySeed.getBytes("UTF-8");
    byte iv[] = {1,2,3,4,5,6,7,8};
    MessageDigest digest = MessageDigest.getInstance("SHA-1");
    byte[] hash = digest.digest(password);
      
    Cipher rc2 = Cipher.getInstance("RC2");
    rc2.init(Cipher.DECRYPT_MODE, new SecretKeySpec(hash, "RC2"), new RC2ParameterSpec(128,iv));
    String decryptedValue = new String(rc2.doFinal(value.getBytes()), "UTF-8");
    Thank's very much!!!


    Solution :

    If it interest somebody, the correct form to translate this C++ code is :

    Java Code:
        MessageDigest digest = MessageDigest.getInstance("SHA-1");
        byte[] hash = digest.digest(sCryptKeySeed.getBytes());
    						
        hash = Arrays.copyOfRange(hash, 0, 16);
    
        SecretKeySpec rc2keySPec = new SecretKeySpec(hash,"RC2");
        RC2ParameterSpec param = new RC2ParameterSpec(128, new byte[8]);
    
        Cipher rc2 = Cipher.getInstance("RC2/CBC/PKCS5Padding"); 
        rc2.init(Cipher.DECRYPT_MODE, rc2keySPec, param);
    
        String decryptedValue = new String(rc2.doFinal(value.getBytes()));
    Last question :

    If I include "Dash" or Underscore" in the sCryptKeySeed as this :

    Java Code:
    sCryptKeySeed = "SDFSD2-DF45-ASD5-BLA_BLA"
    The decryption made a lot of strange characters as :

    Java Code:
    4D7B2B4F7F14C544AD28C802tÅv¾)¨¸*E557912•062013
    Supposed output :

    Java Code:
    4D7B2B4F7F14C544AD28C80227849D54E5579127062013
    Anybody have an idea why it does this?
    Last edited by heinkoi459; 06-27-2013 at 08:59 PM.

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,648
    Rep Power
    5

    Default Re: Translate Decrypting string in Java using Microsoft Crypto API

    I suggest you provide more specifics:

    1. Original password,
    2. Encrypted password
    3. Prose description of desired procedure.
    4. Results you are getting.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Replies: 7
    Last Post: 10-04-2012, 03:42 PM
  2. Replies: 2
    Last Post: 04-18-2012, 03:51 PM
  3. Replies: 5
    Last Post: 09-11-2011, 05:13 AM
  4. Replies: 3
    Last Post: 08-15-2011, 10:16 AM
  5. Decrypting Java Malware
    By javadan in forum Advanced Java
    Replies: 1
    Last Post: 01-27-2011, 12:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •