Results 1 to 2 of 2
  1. #1
    alexalex is offline Member
    Join Date
    Oct 2012
    Rep Power

    Default decryption equal to c#

    I have a snopped that works in c#

    Java Code:
     string cipherText="text to decrypt$%TRTEGRTEhgrtgewfewfewg6tyrjhryth";
    		string pw="12345678";
                    IBuffer pwBuffer = CryptographicBuffer.ConvertStringToBinary(pw, BinaryStringEncoding.Utf8);
                    IBuffer saltBuffer = CryptographicBuffer.ConvertStringToBinary(pw, BinaryStringEncoding.Utf8);
                    IBuffer cipherBuffer = CryptographicBuffer.DecodeFromBase64String(cipherText);
                    // Derive key material for password size 32 bytes for AES256 algorithm
                    KeyDerivationAlgorithmProvider keyDerivationProvider = Windows.Security.Cryptography.Core.KeyDerivationAlgorithmProvider.OpenAlgorithm("PBKDF2_SHA1");
                    // using salt and 1000 iterations
                    KeyDerivationParameters pbkdf2Parms = KeyDerivationParameters.BuildForPbkdf2(saltBuffer, 1000);
                    // create a key based on original key and derivation parmaters
                    CryptographicKey keyOriginal = keyDerivationProvider.CreateKey(pwBuffer);
                    IBuffer keyMaterial = CryptographicEngine.DeriveKeyMaterial(keyOriginal, pbkdf2Parms, 32);
                    CryptographicKey derivedPwKey = keyDerivationProvider.CreateKey(pwBuffer);
                    // derive buffer to be used for encryption salt from derived password key 
                    IBuffer saltMaterial = CryptographicEngine.DeriveKeyMaterial(derivedPwKey, pbkdf2Parms, 16);
                    // display the keys - because KeyDerivationProvider always gets cleared after each use, they are very similar unforunately
                    string keyMaterialString = CryptographicBuffer.EncodeToBase64String(keyMaterial);
                    string saltMaterialString = CryptographicBuffer.EncodeToBase64String(saltMaterial);
                    SymmetricKeyAlgorithmProvider symProvider = SymmetricKeyAlgorithmProvider.OpenAlgorithm("AES_CBC_PKCS7");
                    // create symmetric key from derived password material
                    CryptographicKey symmKey = symProvider.CreateSymmetricKey(keyMaterial);
                    // encrypt data buffer using symmetric key and derived salt material
                    IBuffer resultBuffer = CryptographicEngine.Decrypt(symmKey, cipherBuffer, saltMaterial);
                    byte[] asd;
                    CryptographicBuffer.CopyToByteArray(resultBuffer, out asd);
                    string result = CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, resultBuffer);
                    return result;
    But it hard to my to convert code to java. Here are my Java tries:
    Java Code:
    String textTo="text to decrypt$%TRTEGRTEhgrtgewfewfewg6tyrjhryth";
    	final String kkey="0000000000000000";
    	            final javax.crypto.spec.SecretKeySpec keySpec = new SecretKeySpec(kkey.getBytes(), "AES");
    	        	 final javax.crypto.Cipher cipher = Cipher.getInstance("AES");
    				cipher.init(Cipher.ENCRYPT_MODE, keySpec);
    				byte [] encryptedValue = cipher.doFinal(textTo.getBytes());
    		        String aa=new String(encryptedValue); 
    		    System.out.println(new String(aa));
    	        return aa;
    Seems that's impossible to find same algorythm at Java as at C#, or?

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Washington, DC
    Rep Power

    Default Re: decryption equal to c#

    Snopped? Word of the day.

    You're going to have to break the algorithm down into its individual pieces, then figure out how to do each step in Java. What does each line do?
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

Similar Threads

  1. Decryption problem...
    By muneeb in forum New To Java
    Replies: 0
    Last Post: 09-13-2011, 10:19 AM
  2. Decryption problem...
    By muneeb in forum Java Software
    Replies: 0
    Last Post: 09-13-2011, 10:19 AM
  3. Encryption and Decryption
    By jatinkansagara in forum Advanced Java
    Replies: 7
    Last Post: 06-27-2011, 03:08 PM
  4. problem about AES Decryption(help me please)
    By smart_mody in forum New To Java
    Replies: 2
    Last Post: 09-29-2009, 11:32 PM
  5. PGP Encryption&Decryption
    By Deepa in forum New To Java
    Replies: 2
    Last Post: 07-07-2009, 06:22 AM

Tags for this Thread

Posting Permissions

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