views:

67

answers:

3

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?

+3  A: 

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
shamittomar,Thank you so much for your response, this is what I was looking for. You made my day! :DGod Bless us All!
Dee Jay'
I have not yet checked the functions, but will do soon to secure my data.
Dee Jay'
You're welcome. Just check the Java code. I am not a pro in it, there may be some bugs.
shamittomar
A: 

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.

banzaimonkey
-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
@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.
banzaimonkey
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
To illustrate, ROT13 is (genuinely) not encryption from a security standpoint ... but it is still (genuinely) encryption, and serves a genuine purpose.
Stephen C
@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.
banzaimonkey
+1  A: 

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

http://stackoverflow.com/questions/116650/curl-equivalent-in-java

Hope that helps.

Matt H