java的 DES 加密解密方法 求对应php的加密解密方法!!!!急切 5
publicclassAESHelper{privatestaticStringdesKey="12345678";privatestaticStringAlgorith...
public class AESHelper {
private static String desKey = "12345678";
private static String Algorithm = "DES"; // 定义加密算法,可用DES,DESede,Blowfish
static {
Security.addProvider(new com.sun.crypto.provider.SunJCE());
}
public static String encodeByKey(String keyStr, String str)
throws Exception {
byte[] key = keyStr.getBytes();
return toHex(encode(str.getBytes(), key));
}
public static String encodeStr(String str) throws Exception {
return encodeByKey(getDesKey(), str);
}
private static String decodeByKey(String keyStr, String str)
throws Exception {
byte[] key = keyStr.getBytes();
return new String(decode(hexToBytes(str), key));
}
public static String decodeStr(String str) throws Exception {
return decodeByKey(getDesKey(), str);
}
private static byte[] encode(byte[] input, byte[] key) throws Exception {
SecretKey deskey = new javax.crypto.spec.SecretKeySpec(key, Algorithm);
Cipher c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.ENCRYPT_MODE, deskey);
byte[] cipherByte = c1.doFinal(input);
return cipherByte;
}
这是java的 des 方法 求对应php的 展开
private static String desKey = "12345678";
private static String Algorithm = "DES"; // 定义加密算法,可用DES,DESede,Blowfish
static {
Security.addProvider(new com.sun.crypto.provider.SunJCE());
}
public static String encodeByKey(String keyStr, String str)
throws Exception {
byte[] key = keyStr.getBytes();
return toHex(encode(str.getBytes(), key));
}
public static String encodeStr(String str) throws Exception {
return encodeByKey(getDesKey(), str);
}
private static String decodeByKey(String keyStr, String str)
throws Exception {
byte[] key = keyStr.getBytes();
return new String(decode(hexToBytes(str), key));
}
public static String decodeStr(String str) throws Exception {
return decodeByKey(getDesKey(), str);
}
private static byte[] encode(byte[] input, byte[] key) throws Exception {
SecretKey deskey = new javax.crypto.spec.SecretKeySpec(key, Algorithm);
Cipher c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.ENCRYPT_MODE, deskey);
byte[] cipherByte = c1.doFinal(input);
return cipherByte;
}
这是java的 des 方法 求对应php的 展开
1个回答
展开全部
DES是一种标准的数据加密算法,关于这个算法的详细介绍可以参考wiki和百度百科:
php中有一个扩展可以支持DES的加密算法,是:extension=php_mcrypt.dll
在配置文件中将这个扩展打开还不能够在windows环境下使用
需要将PHP文件夹下的 libmcrypt.dll 拷贝到系统的 system32 目录下,这是通过phpinfo可以查看到mcrypt表示这个模块可以正常试用了。
下面是PHP中使用DES加密解密的一个例子:
//$input - stuff to decrypt
//$key - the secret key to use
function do_mencrypt($input, $key)
{
$input = str_replace(""n", "", $input);
$input = str_replace(""t", "", $input);
$input = str_replace(""r", "", $input);
$key = substr(md5($key), 0, 24);
$td = mcrypt_module_open('tripledes', '', 'ecb', '');
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
mcrypt_generic_init($td, $key, $iv);
$encrypted_data = mcrypt_generic($td, $input);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return trim(chop(base64_encode($encrypted_data)));
}
//$input - stuff to decrypt
//$key - the secret key to use
function do_mdecrypt($input, $key)
{
$input = str_replace(""n", "", $input);
$input = str_replace(""t", "", $input);
$input = str_replace(""r", "", $input);
$input = trim(chop(base64_decode($input)));
$td = mcrypt_module_open('tripledes', '', 'ecb', '');
$key = substr(md5($key), 0, 24);
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
mcrypt_generic_init($td, $key, $iv);
$decrypted_data = mdecrypt_generic($td, $input);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return trim(chop($decrypted_data));
}
参考自:http://www.cnblogs.com/cocowool/archive/2009/01/07/1371309.html
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询