Menu

Decrypt openssl-encrypted data?

Help
2013-11-28
2013-12-02
  • Philippe Lang

    Philippe Lang - 2013-11-28

    Hi,

    I'm trying to decode with pidCrypt some data which has been encrypted with openssl, and I have a few problems. Here is what I'm doing server-side:

    # Generate encrypted private key.
    openssl genrsa -aes256 -out private.key
    
    # Extract public key from private key.
    openssl rsa -in private.key -pubout > public.key
    
    # Encrypt with public key, and encode in Base64
    echo 'Hello Trac!' | openssl rsautl -encrypt -pubin -inkey public.key | base64 > encrypted_with_public_key
    

    My goal is to decrypt the content of file "encrypted_with_public_key" with pidCrypt, the private key, and the password chosen to encrypt the private key.

    Here is the javascript code I'm using:

    :::javascript
    // Get input data
    private_key_encoded = $("div.password > .private_key_encoded").html();
    password_encrypted_base64 = $("div.password > .encrypted_password").html();
    
    // Parse the private key
    params = certParser(private_key_encoded);
    
    // Aes-cbc decoding
    aes = new pidCrypt.AES.CBC();
    salt = params.salt;
    k_and_iv = aes.createKeyAndIv({password:data.password, salt:salt, bits:params.bits});
    aes.initByValues(params.b64, k_and_iv.key, params.iv.toLowerCase(), {UTF8:false, A0_PAD:false, nBits:params.bits});
    rsapem_decrypted = aes.decrypt();
    
    // ASN1 Parsing
    decryptedBytes = pidCryptUtil.toByteArray(rsapem_decrypted);
    asn = pidCrypt.ASN1.decode(decryptedBytes);
    asnTree = asn.toHexTree();
    
    // RSA decoding
    rsa = new pidCrypt.RSA();
    // Set the private key based on the result of the ASN1 parsing
    rsa.setPrivateKeyFromASN(asnTree);
    // Prepare text to decode
    ciphertext = pidCryptUtil.decodeBase64(pidCryptUtil.stripLineFeeds(password_encrypted_base64));
    // Decode
    plain = rsa.decrypt(pidCryptUtil.convertToHex(ciphertext));
    
    alert('Data: ' + plain);
    

    Plaintext return is always empty. But key decoding seems to work. Do you see any reason why this shouldn't work?

    Thanks!

     
  • Philippe Lang

    Philippe Lang - 2013-11-29

    I was able to decode my rsa encrypted string by using a hack I still don't understand, after noticing a bunch of "f" at the end of the cipher, just before decryption.

    :::javascript
    ciphertext = ciphertext.substring(0, ciphertext.length - 9);
    

    I might have unneeded trailing characters in my encoded string.

     
  • Jonah (pidder)

    Jonah (pidder) - 2013-11-29

    It might be a padding issue. Please try using PKCS#1 v1.5 (see https://www.openssl.org/docs/apps/rsautl.html).

    We demonstrate a working example in this thread. Please let us know if this does not solve your issue.

    Best,
    Jonah

     
  • Philippe Lang

    Philippe Lang - 2013-12-02

    Hi,

    I tried all combinations of openssl paddings, but none seems to work. In particular, here are the openssl options I have tried:

    -pkcs: (the default) works with my hack, doesn't work without.
    -oaep: doesn't work at all, with or without hack.
    -ssl: works with my hack, doesn't work without.
    -raw: doesn't work at all, with or without hack.
    

    Using OpenSSL 1.0.1e 11 Feb 2013, on Debian SID.

     
  • Philippe Lang

    Philippe Lang - 2013-12-02

    Sorry, this was my fault from the beginning: I'm reading the encoded password through jquery, and I didn't take care of a few empty characters in the HTML file, which used to look like:

    :::html
                <div class="hide encrypted_password">
    VcKQBg6PNAfBtrBTmeE4m5xAiX/s42aCdj+vCh+oNaD5LqCa9v6kqz+cCp3Wg6U5LwSsIRT0SgAs
    DF4NU7dgv2GSSkL6L9bpjOF7UP3CQlm96sMfhkGZ0P/Ka1EoGuxFe6A1WMOhL9BEw+twQm70OdpN
    eP4/vS3PF5rSXuTruWY=
                </div>
    

    Works fine like so:

    :::html
                <div class="hide encrypted_password">
    VcKQBg6PNAfBtrBTmeE4m5xAiX/s42aCdj+vCh+oNaD5LqCa9v6kqz+cCp3Wg6U5LwSsIRT0SgAs
    DF4NU7dgv2GSSkL6L9bpjOF7UP3CQlm96sMfhkGZ0P/Ka1EoGuxFe6A1WMOhL9BEw+twQm70OdpN
    eP4/vS3PF5rSXuTruWY=</div>
    

    Sorry for that! :)

     
  • Jonah (pidder)

    Jonah (pidder) - 2013-12-02

    No problem and we're glad you have figured it out!

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.