encrypt a string using DEC in PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pandey24
Forum Newbie
Posts: 4
Joined: Thu Dec 07, 2006 4:26 am

encrypt a string using DEC in PHP

Post by pandey24 »

Need to convert code used in JAVA Given below in PHP

Code: Select all

import java.util.*;
import java.security.*;
import java.security.spec.*;
import java.security.cert.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import sun.security.x509.*;
import java.io.*;
import sun.misc.BASE64Encoder;

public class KIOSKCrypto
{
    public KIOSKCrypto()
    {
    }
    static
    {
        java.security.Security.insertProviderAt(new com.sun.crypto.provider.SunJCE(),1);
        try{
        	System.out.println("to be posted  "+encryptPassword("<PAYGATE><MSG_CODE>AUTH</MSG_CODE><MER_URL>". "http://65.182.161.101/kioskweb/gres.php</MER_URL><MSG_HASH></MSG_HASH><TXN_DATE>". "06-12-05</TXN_DATE><MER_CODE>ME0002</MER_CODE><ORDER_NO>kiosk78061205</ORDER_NO>". "<TXN_AMT>15</TXN_AMT><TXN_CUR>INA</TXN_CUR></PAYGATE>",null));
        														// <PAYGATE><MSG_CODE>AUTH</MSG_CODE> <MER_URL>http://65.182.161.101/kioskweb/gres.php</MER_URL><MSG_HASH> </MSG_HASH><TXN_DATE>06-12-05</TXN_DATE><MER_CODE>ME0002</MER_CODE> <ORDER_NO>kiosk73061205</ORDER_NO><TXN_AMT>15</TXN_AMT> <TXN_CUR>INA</TXN_CUR></PAYGATE>
		}catch(Exception e){
			e.printStackTrace();
		}
    }

 public static String encryptPassword(String mesg,String merchantCode) throws Exception
 {
 byte[] sha1_digest=null;
 Cipher cipher=null;Cipher cipherWithPadding=null;
 SecretKeyFactory skf=null;SecureRandom sr=null;
 SecretKeySpec desKey=null;
 byte[] fpxPasswdDESKeyInBytes=null;

 try
 {
        cipher=Cipher.getInstance("DES");
        System.out.println("mer code="+merchantCode);
        MessageDigest sha1 = MessageDigest.getInstance("SHA1");
        sha1.update(mesg.getBytes());
        sha1_digest=sha1.digest();
        System.out.println("SHA VALUE="+ByteArrayToHexString(sha1_digest));
        String fpxPasswdDESKey="ABCDABCDABCDABCD";//getKeyFromJKS(merchantCode);//Give Your Static Key
        fpxPasswdDESKeyInBytes=HexStringToByteArray(fpxPasswdDESKey);
        desKey=new SecretKeySpec(fpxPasswdDESKeyInBytes,"DES");
        cipher.init(Cipher.ENCRYPT_MODE,desKey);
        byte[] outL= cipher.doFinal(sha1_digest);

        System.out.println("des value in String "+new String(outL));

        return ByteArrayToHexString(outL);
 }
   catch(Exception e)
   {
    throw new Exception ("[FPXCryptoUtils][encryptPassword] Error ..",e);
   }
   finally
   {
    sha1_digest=null;
   }
  }

public static String byteArrayToString(byte[] byteArray)
{
	//byte[] byteArray=...............already initialised

	String s="";
	for(int i=0; i<byteArray.length; i++){
	s+=( char )byteArray[ i ]+",";
	}
	return(s);
}

 public static byte[] HexStringToByteArray(String strHex)
    {
        byte bytKey[] = new byte[(strHex.length() / 2)];
        int y= 0;
        String strbyte;
        for (int x=0; x < bytKey.length; x++)
        {
            strbyte = strHex.substring(y, (y + 2));
            if (strbyte.equals("FF"))
            {
                bytKey[x] = (byte) 0xFF;
            }
            else
            {
                bytKey[x] = (byte) Integer.parseInt(strbyte, 16);
            }
            y = y + 2;
        }
        return bytKey;
    }

    public static String ByteArrayToHexString(byte byteArray[])
    {
        StringBuffer strArray=new StringBuffer();
        strArray.append("");
        for (int x=0; x < byteArray.length; x++)
        {
            int b = ((int)byteArray[x] & 0x000000ff);
            if (b < 16)
            {
                strArray.append("0");
                strArray.append(Integer.toHexString (b).toUpperCase());
            }
            else
            {
                strArray.append(Integer.toHexString(b).toUpperCase());
            }
        }
        return strArray.toString();
    }
}
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

Please highlight your code using the syntex highlight tag.
pandey24
Forum Newbie
Posts: 4
Joined: Thu Dec 07, 2006 4:26 am

encryption using des in php

Post by pandey24 »

$hash_key = "FD30A380B84214497BB15B6266B55190C8DC1E5F";
need to encrpyt this hash value so that the encrypted should be
9E901E502E92CF0CB431BDAFBB709078F8FF2C5C4A2F1817


i have tried various methods in php but couldn't succeed
plz help
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Re: encryption using des in php

Post by volka »

pandey24 wrote:$hash_key = "FD30A380B84214497BB15B6266B55190C8DC1E5F";
need to encrpyt this hash value so that the encrypted should be
9E901E502E92CF0CB431BDAFBB709078F8FF2C5C4A2F1817


i have tried various methods in php but couldn't succeed
plz help
In what way is this related to viewtopic.php?t=60348 ?
pandey24
Forum Newbie
Posts: 4
Joined: Thu Dec 07, 2006 4:26 am

encrypt a string using DES in PHP

Post by pandey24 »

plz find below the method i am using to calculate the encrypted value


/****Message to be encrypted********/

<?php
require_once('class_ende.php');

$ende = new EnDe('ABCDABCDABCDABCD');

$merReq = "<PAYGATE><MSG_CODE>AUTH</MSG_CODE><MER_URL>http://65.182.161.101/kioskweb/gres.php</MER_URL> <MSG_HASH></MSG_HASH><TXN_DATE>06-12-05</TXN_DATE><MER_CODE>ME0002</MER_CODE> <ORDER_NO>kiosk78061205</ORDER_NO><TXN_AMT>15</TXN_AMT><TXN_CUR>INA</TXN_CUR></PAYGATE>";

/*********calculating it has value**********/

$hash_key = strtoupper(sha1($merReq));

/************encryption**********/

$crypt_key1 = $ende->encrypt($hash_key);

?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

I'd rather know first how this is related to viewtopic.php?t=60348
Just to avoid crossover and chaos.
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

pandey24: Highlight the code you post!!!
Posting Guidelines 9 wrote: Posting Guidelines 9:
Use the bbcode tags for code blocks - for PHP code use the [ p h p ][ / p h p ] tags to enclose it,
for all other code use the [ c o d e ][ / c o d e ] tags - this will make it much easier for people to read your code (especially if you've taken the time to indent it properly).
EDIT:
I suggest you to use [ syntax="java" ][ /syntax ].
pandey24
Forum Newbie
Posts: 4
Joined: Thu Dec 07, 2006 4:26 am

des encryption

Post by pandey24 »

its the same !
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: des encryption

Post by Chris Corbyn »

pandey24 wrote:its the same !
Funny that. Are you the same person or just at the same institution? You're both from the same place at least ;)
Post Reply