2013年11月19日星期二

Help Java DES encryption , how to implement in PHP , find the source

Gurus , thank you, look , seeking PHP source code

mainly hex conversion did not look out . PHP ultimate effect is of equal length encrypted.

Before: C8837B23FF8AAA8A2DDE915473CE0991
After: 0F83B43F06585551177ECDCE352EA4E7

source as follows

import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.Date;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;


public class test {


private static final char [] kHexChars = {'0 ', '1', '2 ', '3', ' 4 ', '5', '6 ', '7', '8 ', '9', 'a', 'b', 'c' , 'd', 'e', ​​'f'};
private static String pingKey = "21218cca77804d2b";
public test (String Key)
{
pingKey = Key;
}
public String DES (String payPass)
{
payPass = getMd5Hex (payPass.getBytes ());

/ / payment password encryption
String payPassEncrypt = desEncrypt (payPass);
return payPassEncrypt;
}
/ **
* @ param args
* @ throws Exception
* /
public static void main (String [] args) throws Exception {
/ / TODO Auto-generated method stub
String payPass = "123321";
payPass = getMd5Hex (payPass.getBytes ());
System.out.println ("Before:" + payPass);
/ / payment password encryption
String payPassEncrypt = desEncrypt (payPass);
System.out.println ("After:" + payPassEncrypt);
}

public static String desEncrypt (String hexData)
{

byte [] encryptBytes = encryptDES (hexToBuffer (hexData), hexToBuffer (pingKey));

return bufferToHex (encryptBytes);
}
public static byte [] encryptDES (byte [] data, byte [] key)
{
byte [] encryptedData = (byte []) null;
try
{
DESKeySpec dks = new DESKeySpec (key);

SecretKeyFactory keyFactory = SecretKeyFactory.getInstance ("DES");
SecretKey sk = keyFactory.generateSecret (dks);

Cipher cipher = Cipher.getInstance ("DES / ECB / NoPadding");

cipher.init (1, sk);

encryptedData = cipher.doFinal (data);
} catch (InvalidKeyException e) {
e.printStackTrace ();
} catch (InvalidKeySpecException e) {
e.printStackTrace ();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace ();
} catch (NoSuchPaddingException e) {
e.printStackTrace ();
} catch (IllegalBlockSizeException e) {
e.printStackTrace ();
} catch (BadPaddingException e) {
e.printStackTrace ();
}

return encryptedData;
}
public static String bufferToHex (byte [] buffer)
{
return bufferToHex (buffer, 0, buffer.length). toUpperCase ();
}

public static String bufferToHex (byte [] buffer, int startOffset, int length)
{
StringBuffer hexString = new StringBuffer (2 * length);
int endOffset = startOffset + length;
for (int i = startOffset; i appendHexPair (buffer [i], hexString);
return hexString.toString ();
}
public static byte [] md5Encrypt (byte [] data)
{
try
{
MessageDigest messageDigest = MessageDigest.getInstance ("MD5");
byte [] plainText = data;
messageDigest.update (plainText);
return messageDigest.digest ();
} catch (NoSuchAlgorithmException e1) {
e1.printStackTrace ();
}
return null;
}

public static String getMd5Hex (byte [] data)
{
String md5 = bufferToHex (md5Encrypt (data));
while (md5.length () <32) {
md5 = "0" + md5;
}
return md5;
}

private static void appendHexPair (byte b, StringBuffer hexString)
{
char highNibble = kHexChars [((b & 0xF0) >> 4)];
char lowNibble = kHexChars [(b & 0xF)];
hexString.append (highNibble);
hexString.append (lowNibble);
}

public static String hexToString (String hexString) throws NumberFormatException
{
byte [] bytes = hexToBuffer (hexString);
return new String (bytes);
}

public static byte [] hexToBuffer (String hexString) throws NumberFormatException
{
int length = hexString.length ();
byte [] buffer = new byte [(length + 1) / 2];
boolean evenByte = true;
byte nextByte = 0;
int bufferOffset = 0;

if (length% 2 == 1)
evenByte = false;
for (int i = 0; i char c = hexString.charAt (i);
int nibble;
if ((c> = '0 ') && (c <= '9' ) ) {
nibble = c - '0 ';
}
else
{

if ((c> = 'A') && (c <= ; 'F')) {
nibble = c - 'A' + 10;
}
else
{

if ((c> = 'a') && (c <= 'f'))
nibble = c - 'a' + 10;
else throw new NumberFormatException ("Invalid hex digit '" + c + "' ." ) ;
}
}

if (evenByte) {
nextByte = (byte) (nibble << 4);
} else {
nextByte = (byte) (nextByte + (byte) nibble);
buffer [(bufferOffset + +)] = nextByte;
}
evenByte =! evenByte;
}
return buffer;
}
}


------ Solution ------------------------------------ --------
halo, this is not the question to ask php java version ?

two functions are as follows:
encryption function : encrypt
function encrypt ($ encrypt, $ key = "") {
$ iv = mcrypt_create_iv (mcrypt_get_iv_size (MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);
$ passcrypt = mcrypt_encrypt (MCRYPT_RIJNDAEL_256, $ key, $ encrypt, MCRYPT_MODE_ECB, $ iv);
$ encode = base64_encode ($ passcrypt);
return $ encode;
}

decryption function : decrypt
function decrypt ($ decrypt, $ key = "") {
$ decoded = base64_decode ($ decrypt);
$ iv = mcrypt_create_iv (mcrypt_get_iv_size (MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);
$ decrypted = mcrypt_decrypt (MCRYPT_RIJNDAEL_256, $ key, $ decoded, MCRYPT_MODE_ECB, $ iv);
return $ decrypted;
}

Well, you are too lazy to search, I can help you google under a lot
------ For reference only ------------------ ---------------------
you this question should be asked to php area . However, under this kind of thing should google "php achieve des encryption " should be everywhere the
------ For reference only ------------------ ---------------------
your answer I would have thought of, to ask PHP , java someone answers say go back to the java ask, people will say to PHP to ask questions.
------ For reference only -------------------------------------- -



Thank you for your reply , you do not understand java, he is a hexadecimal to Bytes, the final output in hexadecimal .
you that is base64_decode, you have to understand is that it is estimated will come to a bintohex. you this way I tried a few days ago .

没有评论:

发表评论