PHP des加密输入怎么才能和JAVA的des输出一至呢 本帖最后由 xxingmen 于 2012-03-21 19:33:27 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 网上不是蛮多这方面的你看看是否有帮助?http://www.cnblogs.com/ghj1976/archive/2011/05/26/2058841.htmlhttp://blog.csdn.net/raining_peidx/article/details/4490741 <?php function encrypt($str, $key) { $block = mcrypt_get_block_size('des', 'ecb'); $pad = $block - (strlen($str) % $block); $str .= str_repeat(chr($pad), $pad); return mcrypt_encrypt(MCRYPT_DES, $key, $str, MCRYPT_MODE_ECB); } function decrypt($str, $key) { $str = mcrypt_decrypt(MCRYPT_DES, $key, $str, MCRYPT_MODE_ECB); $block = mcrypt_get_block_size('des', 'ecb'); $pad = ord($str[($len = strlen($str)) - 1]); return substr($str, 0, strlen($str) - $pad); } ?> 以前也遇到过类似的加解密问题,不知道是两种语言对数据的处理方式不一样还是其它什么原因,后来实在没辙了,用javabridge解决的 没有看到 php 中对应 keyData = df.format(new Date(timestamp)) + "aop" + keyData;的部分感觉两者的算法不一致 最好把java代码贴全咯。copy过来就能运行那种,咱是phper 这是完整的JAVA代码import java.util.Date;import java.util.TimeZone;import javax.crypto.Cipher;import javax.crypto.KeyGenerator;import javax.crypto.SecretKey;import javax.crypto.SecretKeyFactory;import javax.crypto.spec.DESKeySpec;import javax.crypto.spec.IvParameterSpec;import com.allinpay.api.Constants;/** * 支持 DES对称加密的工具类. * * 支持Hex与Base64两种编码方式. * * @author wangwch */public class CryptoUtils { private static final String DES = "DES"; private static final String DEFAULT_ENCODING = "UTF-8"; /** 加密算法 */ private final static String ALGORITHM = "DES/CBC/PKCS5Padding"; /** * 传输的关键数据加密 * @param keyData 需加密值 * @param timestamp 时间戳 * @param dataSecret 密钥 * @return String 加密数据 */ public static String encryptKeyData(String keyData, Long timestamp, String dataSecret) { DateFormat df = new SimpleDateFormat(Constants.DATE_TIME_FORMAT); df.setTimeZone(TimeZone.getTimeZone(Constants.DATE_TIMEZONE)); keyData = df.format(new Date(timestamp)) + "aop" + keyData; return desEncryptToBase64(keyData, dataSecret.getBytes()); } public static void main(String args[]){ Long timestamp = new Long("20120321161413"); String dataSecret = "abcdefgh"; String keyDate = "11111"; System.out.println("------------------"+encryptKeyData(keyDate, timestamp, dataSecret)); } //-- DES function --// /** * 使用DES加密原始字符串, 返回Hex编码的结果. * * @param input 原始输入字符串 * @param keyBytes 符合DES要求的密钥 */ public static String desEncryptToHex(String input, byte[] keyBytes) { byte[] encryptResult = null; try { encryptResult = des(input.getBytes(DEFAULT_ENCODING), keyBytes, Cipher.ENCRYPT_MODE); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return EncodeUtils.hexEncode(encryptResult); } /** * 使用DES加密原始字符串, 返回Base64编码的结果. * * @param input 原始输入字符串 * @param keyBytes 符合DES要求的密钥 */ public static String desEncryptToBase64(String input, byte[] keyBytes) { byte[] encryptResult = null; try { encryptResult = des(input.getBytes(DEFAULT_ENCODING), keyBytes, Cipher.ENCRYPT_MODE); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return EncodeUtils.base64Encode(encryptResult); } /** * 使用DES解密Hex编码的加密字符串, 返回原始字符串. * * @param input Hex编码的加密字符串 * @param keyBytes 符合DES要求的密钥 */ public static String desDecryptFromHex(String input, byte[] keyBytes) { byte[] decryptResult = des(EncodeUtils.hexDecode(input), keyBytes, Cipher.DECRYPT_MODE); String decryptString = null; try { decryptString = new String(decryptResult, DEFAULT_ENCODING); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return decryptString; } /** * 使用DES解密Base64编码的加密字符串, 返回原始字符串. * * @param input Base64编码的加密字符串 * @param keyBytes 符合DES要求的密钥 */ public static String desDecryptFromBase64(String input, byte[] keyBytes) { byte[] decryptResult = des(EncodeUtils.base64Decode(input), keyBytes, Cipher.DECRYPT_MODE); String decryptString = null; try { decryptString = new String(decryptResult, DEFAULT_ENCODING); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return decryptString; } /** * 使用DES加密或解密无编码的原始字节数组, 返回无编码的字节数组结果. * 通用的java .net php * @param inputBytes 原始字节数组 * @param keyBytes 符合DES要求的密钥 * @param mode Cipher.ENCRYPT_MODE 或 Cipher.DECRYPT_MODE */ private static byte[] des(byte[] inputBytes, byte[] keyBytes, int mode) { try { DESKeySpec desKeySpec = new DESKeySpec(keyBytes); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES); //密钥 SecretKey secretKey = keyFactory.generateSecret(desKeySpec); // 偏移量 IvParameterSpec iv = new IvParameterSpec(keyBytes); Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(mode, secretKey, iv); return cipher.doFinal(inputBytes); } catch (GeneralSecurityException e) { throw convertRuntimeException(e); } } /** * 生成符合DES要求的密钥, 长度为64位(8字节). */ public static byte[] generateDesKey() { try { KeyGenerator keyGenerator = KeyGenerator.getInstance(DES); SecretKey secretKey = keyGenerator.generateKey(); return secretKey.getEncoded(); } catch (GeneralSecurityException e) { throw convertRuntimeException(e); } } /** * 生成符合DES要求的Hex编码密钥, 长度为16字符. */ public static String generateDesHexKey() { return EncodeUtils.hexEncode(generateDesKey()); } private static IllegalStateException convertRuntimeException(GeneralSecurityException e) { return new IllegalStateException("Security exception", e); }} 怎么实现页面中部分刷新 非技术问题:很想知道下,有多少人和我是一样不喜欢用那些框架的 用php导出excel数据 能不能phpinfo的结果赋给一个变量? for循环郁闷了!!! 排行榜实现的思路到底是怎样的? 对于高手的你来说一定很简单,请教了! PHP用GD画图的怪问题。 怎样获取页面提交过来的输入框的值 php,win2000,iis的问题? 使用IIS配置PHP不能上传文件 PHP怎样从一段文字中提取url?
http://www.cnblogs.com/ghj1976/archive/2011/05/26/2058841.html
http://blog.csdn.net/raining_peidx/article/details/4490741
<?php
function encrypt($str, $key)
{
$block = mcrypt_get_block_size('des', 'ecb');
$pad = $block - (strlen($str) % $block);
$str .= str_repeat(chr($pad), $pad);
return mcrypt_encrypt(MCRYPT_DES, $key, $str, MCRYPT_MODE_ECB);
}
function decrypt($str, $key)
{
$str = mcrypt_decrypt(MCRYPT_DES, $key, $str, MCRYPT_MODE_ECB);
$block = mcrypt_get_block_size('des', 'ecb');
$pad = ord($str[($len = strlen($str)) - 1]);
return substr($str, 0, strlen($str) - $pad);
}
?>
keyData = df.format(new Date(timestamp)) + "aop" + keyData;
的部分感觉两者的算法不一致
import java.util.Date;
import java.util.TimeZone;import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;import com.allinpay.api.Constants;/**
* 支持 DES对称加密的工具类.
*
* 支持Hex与Base64两种编码方式.
*
* @author wangwch
*/
public class CryptoUtils { private static final String DES = "DES";
private static final String DEFAULT_ENCODING = "UTF-8";
/** 加密算法 */
private final static String ALGORITHM = "DES/CBC/PKCS5Padding"; /**
* 传输的关键数据加密
* @param keyData 需加密值
* @param timestamp 时间戳
* @param dataSecret 密钥
* @return String 加密数据
*/
public static String encryptKeyData(String keyData, Long timestamp, String dataSecret) { DateFormat df = new SimpleDateFormat(Constants.DATE_TIME_FORMAT);
df.setTimeZone(TimeZone.getTimeZone(Constants.DATE_TIMEZONE));
keyData = df.format(new Date(timestamp)) + "aop" + keyData;
return desEncryptToBase64(keyData, dataSecret.getBytes());
}
public static void main(String args[]){
Long timestamp = new Long("20120321161413");
String dataSecret = "abcdefgh";
String keyDate = "11111";
System.out.println("------------------"+encryptKeyData(keyDate, timestamp, dataSecret));
} //-- DES function --//
/**
* 使用DES加密原始字符串, 返回Hex编码的结果.
*
* @param input 原始输入字符串
* @param keyBytes 符合DES要求的密钥
*/
public static String desEncryptToHex(String input, byte[] keyBytes) {
byte[] encryptResult = null;
try {
encryptResult = des(input.getBytes(DEFAULT_ENCODING), keyBytes, Cipher.ENCRYPT_MODE);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return EncodeUtils.hexEncode(encryptResult);
} /**
* 使用DES加密原始字符串, 返回Base64编码的结果.
*
* @param input 原始输入字符串
* @param keyBytes 符合DES要求的密钥
*/
public static String desEncryptToBase64(String input, byte[] keyBytes) {
byte[] encryptResult = null;
try {
encryptResult = des(input.getBytes(DEFAULT_ENCODING), keyBytes, Cipher.ENCRYPT_MODE);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return EncodeUtils.base64Encode(encryptResult);
} /**
* 使用DES解密Hex编码的加密字符串, 返回原始字符串.
*
* @param input Hex编码的加密字符串
* @param keyBytes 符合DES要求的密钥
*/
public static String desDecryptFromHex(String input, byte[] keyBytes) {
byte[] decryptResult = des(EncodeUtils.hexDecode(input), keyBytes, Cipher.DECRYPT_MODE);
String decryptString = null;
try {
decryptString = new String(decryptResult, DEFAULT_ENCODING);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return decryptString;
} /**
* 使用DES解密Base64编码的加密字符串, 返回原始字符串.
*
* @param input Base64编码的加密字符串
* @param keyBytes 符合DES要求的密钥
*/
public static String desDecryptFromBase64(String input, byte[] keyBytes) {
byte[] decryptResult = des(EncodeUtils.base64Decode(input), keyBytes, Cipher.DECRYPT_MODE);
String decryptString = null;
try {
decryptString = new String(decryptResult, DEFAULT_ENCODING);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return decryptString;
} /**
* 使用DES加密或解密无编码的原始字节数组, 返回无编码的字节数组结果.
* 通用的java .net php
* @param inputBytes 原始字节数组
* @param keyBytes 符合DES要求的密钥
* @param mode Cipher.ENCRYPT_MODE 或 Cipher.DECRYPT_MODE
*/
private static byte[] des(byte[] inputBytes, byte[] keyBytes, int mode) {
try {
DESKeySpec desKeySpec = new DESKeySpec(keyBytes);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
//密钥
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
// 偏移量
IvParameterSpec iv = new IvParameterSpec(keyBytes);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(mode, secretKey, iv);
return cipher.doFinal(inputBytes);
} catch (GeneralSecurityException e) {
throw convertRuntimeException(e);
}
} /**
* 生成符合DES要求的密钥, 长度为64位(8字节).
*/
public static byte[] generateDesKey() {
try {
KeyGenerator keyGenerator = KeyGenerator.getInstance(DES);
SecretKey secretKey = keyGenerator.generateKey();
return secretKey.getEncoded();
} catch (GeneralSecurityException e) {
throw convertRuntimeException(e);
}
} /**
* 生成符合DES要求的Hex编码密钥, 长度为16字符.
*/
public static String generateDesHexKey() {
return EncodeUtils.hexEncode(generateDesKey());
} private static IllegalStateException convertRuntimeException(GeneralSecurityException e) {
return new IllegalStateException("Security exception", e);
}
}