title: Need an example - decrypting string in Java using Microsoft Crypto API
body: <p>First and foremost, I am not a Java programmer.  I'm looking for an example solution to this problem because the Java developer I have does not have much experience working with encryption.  Everything we've found online pertains to encrypting web pages and dealing with the MS keystore.  We just want to work with a single string from PowerBuilder (PB) and be able to decrypt it in Java.  The restriction here is the MS library.  Due to certain limitations, we are stuck with using this method of encrypting so it's up to the Java side to handle what's being thrown at it.</p>
  
  <p>What I have is a PB version 10.2 program that needs to call this Java utility and pass it a username &amp; password.  We are trying to encrypt the password as a command line friendly string as that is how PB will make the call to the Java app.</p>
  
  <p>In PB I'm using the following object:
  <a href="http://www.topwizprogramming.com/freecode_crypto.html" rel="nofollow noreferrer">http://www.topwizprogramming.com/freecode_crypto.html</a></p>
  
  <p>What the code is doing, is wrapping the Microsoft cryptographic API found in advapi32.dll.  The functions it uses are:</p>
  
  <p>CryptAcquireContext
  <a href="http://msdn.microsoft.com/en-us/library/aa379886(VS.85).aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/aa379886(VS.85).aspx</a></p>
  
  <p>CryptCreateHash
  <a href="http://msdn.microsoft.com/en-us/library/aa379908(VS.85).aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/aa379908(VS.85).aspx</a></p>
  
  <p>CryptHashData
  <a href="http://msdn.microsoft.com/en-us/library/aa380202(VS.85).aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/aa380202(VS.85).aspx</a></p>
  
  <p>CryptDeriveKey
  <a href="http://msdn.microsoft.com/en-us/library/aa379916(VS.85).aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/aa379916(VS.85).aspx</a></p>
  
  <p>CryptEncrypt
  <a href="http://msdn.microsoft.com/en-us/library/aa379924(VS.85).aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/aa379924(VS.85).aspx</a></p>
  
  <p>It's using the Microsoft Strong Cryptographic Provider and PROV_RSA_FULL.  The code takes the data to be encrypted, converts it to a BLOB which is then passed to the encryption functions.  There, it acuires a context, creates a hash object from the context, hashes the password, gets a session key from the hash, then calls encrypt/decrypt.  Last thing is it takes the BLOB returned and converts it to a string under the ANSI character set.</p>
  
  <p>There are a number of constants which at a glance I understand where some come from, others not so much:
  Constant String KEY_CONTAINER = "MyKeyContainer"
  Constant ULong PROV_RSA_FULL = 1
  Constant ULong CALG_MD5 = 32771
  Constant ULong CALG_RC4 = 26625
  Constant ULong ENCRYPT_ALGORITHM = CALG_RC4
  Constant ULong CRYPT_NEWKEYSET = 8
  Constant ULong ERROR_MORE_DATA = 234</p>
  
  <p>Whether this is done in 1.5 using something like BouncyCastle or 1.6 with the Sun crypto interface for MS I don't care, we're just dying to see this work and are honestly over our heads.</p>
  
  <hr>
  
  <p>Hey i need to crypt a string and store it a file and then I need to read the file again and decrypt the same string back.
  But i dont want to crypt the whole file. Once I have got the required crypted value stored in the file ,i need to convert that alone to the original string.
  Can you please help me out with the sample code.</p>