Question

I have a java code

import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import java.io.PrintStream;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class SecureCardData
{
  public static final String retailerid = "61220121";

  public String encryptData(String sData)
    throws Exception
  {
    byte[] bPrivateKey = "61220121".getBytes();
    SecretKeySpec spec = new SecretKeySpec(bPrivateKey, "DES");
    Cipher cipher = Cipher.getInstance("DES");
    cipher.init(1, spec);
    byte[] bEncryptedData = cipher.doFinal(sData.getBytes());
    return Base64.encode(bEncryptedData);
  }

  public String decryptData(String sData)
    throws Exception
  {
    byte[] bPrivateKey = "61220121".getBytes();
    SecretKeySpec spec = new SecretKeySpec(bPrivateKey, "DES");
    Cipher cipher = Cipher.getInstance("DES");
    cipher.init(2, spec);
    byte[] bencryptedData = Base64.decode(sData);
    byte[] bDecryptedData = cipher.doFinal(bencryptedData);
    return new String(bDecryptedData);
  }

  public static void main(String[] args)
    throws Exception
  {
    String s = "1800585544448888|445|0611";
    SecureCardData sd = new SecureCardData();
    String ss = sd.encryptData(s);
    System.out.println(ss);

    ss = sd.decryptData(ss);
    System.out.println(ss);
  }
}

Im not into java,can anyone help me make the code equivalent for this in php? I haven't touched java ever.Finding it really hard to figure out.Is there someone who could help me with this code.?

Was it helpful?

Solution

new SecretKeySpec(bPrivateKey, "DES");

This creates a secret key using the DES specification.

http://en.wikipedia.org/wiki/Data_Encryption_Standard

Note that DES is considered obsolete/easy to break so has not been recommended for use in quite some time.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top