Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Help With Decrypt A value from a key

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help With Decrypt A value from a key

    Hi, I got a the key: "HK8N99LQ" and the
    value: "THBiUURtaHdCSDlFRWYva1VSRm5QUCtWN2NnditldVE2RnhEa zdLNHlwZEZZQnlxU0xDQ3Zlc1ZrV1RNNHgyamcyNXgvUFUxQk1 UQVpPLzJTVDZnR3ZIUTZLbjRoemo4aU9ORVdKZ21kdktjOEZqZ UM0bkwvd29hbWM3bXQ2cXcvbjZRaUdtZDU5ZWtUSlFMZm1DUHZ aL284OGdqd1JJTGNvQzczb1dISTNScFhSc3VDemRRVTM2bmNic lM4b2FuTWM5RUE2UnUvL1RoT0tSOGFScDltSHZaY2R2MHNQS2x COGZOOUkzazcvVmtCZ2FpTnNwZ3p4aGVBWkJtUWJzN1M2YnAyM DJvMXFUMHdCYk9MUG5vNUNOaEtrVTZCZ2RJWTh3dzNqMDR6VVk vMTNNUW0reGtrUT090" from http://api.cashcow.co.il/

    to decrypt the code in c# the website provide this code:

    public static string Decrypt(string app_token, string signed_request)
            {
                byte[] keyArray;
                //get the byte code of the string
     
                byte[] toEncryptArray = Convert.FromBase64String(signed_request);
     
                // app token
                string key = app_token;
     
                //if hashing was used get the hash code with regards to your key
                MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
                //release any resource held by the MD5CryptoServiceProvider
     
                hashmd5.Clear();
     
                TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
                //set the secret key for the tripleDES algorithm
                tdes.Key = keyArray;
                //mode of operation. there are other 4 modes. 
                //We choose ECB(Electronic code Book)
     
                tdes.Mode = CipherMode.ECB;
                //padding mode(if any extra byte added)
                tdes.Padding = PaddingMode.PKCS7;
     
                ICryptoTransform cTransform = tdes.CreateDecryptor();
                byte[] resultArray = cTransform.TransformFinalBlock(
                                     toEncryptArray, 0, toEncryptArray.Length);
                //Release resources held by TripleDes Encryptor                
                tdes.Clear();
                //return the Clear decrypted TEXT
                return UTF8Encoding.UTF8.GetString(resultArray);
            }

    but how I decrypt it in Java, I tried to use that code but I got error message:
    public String Decrypt(String signed_request)
        {		
    		String decryptedString = null;
    		try
    		{    
    		    // Generate the secret key specs.
    		    byte[] key = "HK8N99LQ".getBytes();
    		    MessageDigest sha = MessageDigest.getInstance("MD5");
    		    key = sha.digest(key);	   	    
    		    key = Arrays.copyOf(key, 16); // use only first 128 bit
     
    		    SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
     
    		    // Instantiate the cipher
    		    Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
     
    		    //DECRYPT_MODE
    		    cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
    		    byte[] original = cipher.doFinal(signed_request.getBytes());
    		    decryptedString = new String(original);
    		}
    		catch(Exception e)
    		{
    			String message = e.getMessage();
    			System.out.print(message);
    		}
     
            return decryptedString;
        }


    Error Message:
    Input length must be multiple of 16 when decrypting with padded cipher.
    Last edited by Matan; August 30th, 2014 at 05:52 AM. Reason: The Code was not correctly using highkight tags


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help With Decrypt A value from a key

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly using code or highlight tags per the above link.

    Post error messages you want help with.

Similar Threads

  1. Replies: 1
    Last Post: April 30th, 2014, 05:30 AM
  2. Replies: 1
    Last Post: April 3rd, 2014, 02:11 PM
  3. [SOLVED] Encrypt Decrypt
    By dianac in forum What's Wrong With My Code?
    Replies: 15
    Last Post: April 14th, 2013, 11:01 AM
  4. Need Help for encrypt/decrypt.
    By superdhebz in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 17th, 2010, 12:17 PM