0

I need to get two functions. I want to transfer data from my website to my server in xml format. Now on my server, I want to make a function that encrypts the data and place it in an xml, and another function in java to decrypt it.

Please tell me if there is any predefined function or can you just spare 5 minutes?

Jonas
  • 121,568
  • 97
  • 310
  • 388
DJ'
  • 1,760
  • 1
  • 13
  • 26
  • Encrypting/decrypting != encoding/decoding. What's the functional requirement? – BalusC Aug 28 '10 at 03:08
  • Balus, thank you for posting a comment. The requirement is right what I have stated, I needed encryption and decryption. – DJ' Aug 29 '10 at 08:01

3 Answers3

3

Well, you can use any encrypting mcrypt function in PHP. One example for encrypting in AES 128:

  $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
  $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
  $key = "Put your secret key here";
  $text = "<xml>This is your XML text</xml>";

  //encrypting now with RIJNDAEL 128 encryption.
  $crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $text, MCRYPT_MODE_ECB, $iv);

  //Display encrypted content
  echo $crypttext;

And for decrypting, use this code (I'm not a Java pro, so there may be some bugs):

package org.kamal.crypto;

import java.security.*;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.Cipher; 
import javax.crypto.spec.SecretKeySpec;
import sun.misc.*;

public class SimpleProtector
{
    private static final String ALGORITHM = "AES";
    private static final byte[] keyValue = 
        new byte[] { 'P', 'u', 't', ' ', 'Y', 'o', 'u', 'r', ' ', 'S', 'e', 'c', 'r', 'e', 't', ' ', 'K', 'e', 'y', '', 'H', 'e', 'r', 'e'};

    public static String decrypt(String encryptedValue) throws Exception {
        Key key = generateKey();
        Cipher c = Cipher.getInstance(ALGORITHM);
        c.init(Cipher.DECRYPT_MODE, key);
        byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedValue);
        byte[] decValue = c.doFinal(decordedValue);
        String decryptedValue = new String(decValue);
        return decryptedValue;
    }

    private static Key generateKey() throws Exception {
        Key key = new SecretKeySpec(keyValue, ALGORITHM);
        // SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
        // key = keyFactory.generateSecret(new DESKeySpec(keyValue));
        return key;
    }
}
shamittomar
  • 46,210
  • 12
  • 74
  • 78
  • shamittomar, Thank you so much for your response, this is what I was looking for. You made my day! :D God Bless us All! – DJ' Aug 29 '10 at 08:02
  • I have not yet checked the functions, but will do soon to secure my data. – DJ' Aug 29 '10 at 08:03
  • You're welcome. Just check the Java code. I am not a pro in it, there may be some bugs. – shamittomar Aug 29 '10 at 08:04
1

No point reinventing the wheel here. Use SSL, which is what an HTTPS request would involve. You can do those through CURL.

curl is built into PHP and there is also a java version http://php.net/manual/en/book.curl.php

cURL equivalent in JAVA

Hope that helps.

Community
  • 1
  • 1
hookenz
  • 36,432
  • 45
  • 177
  • 286
-1

Have you looked into JSON?

It's not encrypted, but it's an easy way to pass data back and forth between different programs and languages.

cbednarski
  • 11,718
  • 4
  • 26
  • 33
  • -1 - JSON is an encoding scheme, not an encryption scheme. Encryption (even something as simple as ROT13) is about obscuring meaning, and JSON doesn't attempt to do that. – Stephen C Aug 28 '10 at 06:28
  • @Stephen C I'm well aware of that, and I believe my answer makes that clear as well. It's not really clear whether the OP is equivocating or actually asking about security. You'll notice I did not present JSON as a security solution, merely an encoding / decoding solution to pass information between PHP and Java, per the question's tags. – cbednarski Aug 28 '10 at 06:33
  • If you said "this is encoding rather than encrypting" rather than "this is not encrypting from a security standpoint", I'd have no problems with your answer. Clearly, what you've written implies that your solution *is* encrypting "from a non-security standpoint" which is nonsensical: hence my downvote. – Stephen C Aug 28 '10 at 06:49
  • To illustrate, ROT13 is (genuinely) not encryption from a security standpoint ... but it is still (genuinely) encryption, and serves a genuine purpose. – Stephen C Aug 28 '10 at 06:54
  • @Stephen C If my "implied solution" is nonsensical, why did you just give me an example of it? Really, I'm trying to read between the OP's lines and provide some resources that may help solve the problem they're trying to overcome, but if you want to have a hairsplitting contest, I'm not going to participate. – cbednarski Aug 28 '10 at 07:12