php 和java的sha1 结果不一致。。还有就是php的mcrypt_generic_init()函数问题
mcrypt_generic_init() [function.mcrypt-generic-init]: Key size too large; supplied length: 36, max: 24 现在是php 去请求.net的接口,需要使用sha1 base64 3des加密给的3des key是36位的java代码如下
package com.encrypt;import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.*;public class Cryptogram {
private static byte[] defaultIV = {1,2,3,4,5,6,7,8}; private static byte chr2hex(String chr) {
if (chr.equals("0")) {
return 0x00;
} else if (chr.equals("1")) {
return 0x01;
} else if (chr.equals("2")) {
return 0x02;
} else if (chr.equals("3")) {
return 0x03;
} else if (chr.equals("4")) {
return 0x04;
} else if (chr.equals("5")) {
return 0x05;
} else if (chr.equals("6")) {
return 0x06;
} else if (chr.equals("7")) {
return 0x07;
} else if (chr.equals("8")) {
return 0x08;
} else if (chr.equals("9")) {
return 0x09;
} else if (chr.equals("A")) {
return 0x0a;
} else if (chr.equals("B")) {
return 0x0b;
} else if (chr.equals("C")) {
return 0x0c;
} else if (chr.equals("D")) {
return 0x0d;
} else if (chr.equals("E")) {
return 0x0e;
} else if (chr.equals("F")) {
return 0x0f;
}
return 0x00;
} public static byte[] HexStringToByteArray(String s) {
byte[] buf = new byte[s.length() / 2];
for (int i = 0; i < buf.length; i++) {
buf[i] = (byte) (chr2hex(s.substring(i * 2, i * 2 + 1)) * 0x10 + chr2hex(s
.substring(i * 2 + 1, i * 2 + 2)));
}
return buf;
} /**
* Encrypt the data by the key.
* @param OriSource
* @return strResult
* @throws Exception
*/
public static String encryptByKey(String OriSource, String key) throws Exception {
String strResult = "";
try {
Cipher c3des = Cipher.getInstance("DESede/CBC/PKCS5Padding");
SecretKeySpec myKey = new SecretKeySpec(HexStringToByteArray(key),
"DESede"); IvParameterSpec ivspec = new IvParameterSpec(defaultIV);
c3des.init(Cipher.ENCRYPT_MODE, myKey, ivspec); byte[] testSrc = OriSource.getBytes();
byte[] encoded = c3des.doFinal(testSrc);
strResult = Base64Encrypt.getBASE64_byte(encoded);
} catch (Exception e) {
strResult="";
System.out.println("Encrypt failure!!!");
} return strResult;
}
/**
* Decrypt the encrypted data with the key.
* @param strData
* @return strResult
* @throws Exception
*/
public static String decryptByKey(String encryptedData, String key) throws Exception {
String strResult = "";
try {
Cipher c3des = Cipher.getInstance("DESede/CBC/PKCS5Padding");
SecretKeySpec myKey = new SecretKeySpec(HexStringToByteArray(key),
"DESede"); IvParameterSpec ivspec = new IvParameterSpec(defaultIV);
c3des.init(Cipher.DECRYPT_MODE, myKey, ivspec); byte[] s= Base64Encrypt.getByteArrFromBase64(encryptedData);
byte[] encoded = c3des.doFinal(s);
strResult = new String(encoded);
} catch (Exception e) {
strResult="";
System.out.println("Decrypt failure!!!");
} return strResult;
}
/**
* Decrypt the encrypted data with the key.
* @param strData
* @return strResult
* @throws Exception
*/
public static String getBase64HashString(String str) throws Exception{
byte[] testSrc = str.getBytes();
MessageDigest alga = MessageDigest.getInstance("SHA-1");
alga.update(testSrc);
byte[] digesta = alga.digest();
return Base64Encrypt.getBASE64_byte(digesta);
}
/**
* Decrypt the encrypted data with the key.
* @param strData
* @return strResult
* @throws Exception
*/
public static String getAuthenicator(String sourceStr,String key) throws Exception{
String strResult = "";
try {
Cipher c3des = Cipher.getInstance("DESede/CBC/PKCS5Padding");
SecretKeySpec myKey = new SecretKeySpec(HexStringToByteArray(key),
"DESede"); IvParameterSpec ivspec = new IvParameterSpec(defaultIV);
c3des.init(Cipher.ENCRYPT_MODE, myKey, ivspec);
byte[] testSrc = sourceStr.getBytes();
MessageDigest alga = MessageDigest.getInstance("SHA-1");
alga.update(testSrc);
byte[] digesta = alga.digest(); byte[] encoded = c3des.doFinal(digesta);
strResult = Base64Encrypt.getBASE64_byte(encoded);
} catch (Exception e) {
strResult="";
System.out.println("Decrypt failure!!!" + e.getMessage());
} return strResult;
}
public static void main(String args[]) throws Exception {
String SysID = "0005";
String TimeStamp = "2009-10-22 13:15:20";
String ReturnURL = "http://vnet.cn/passportinterface/test2.aspx";
String Key = "86A659D3035B51B1B66DF3139F1AEC33F6651334F1E65170";
try{
//Get Digest.
String Digest = getBase64HashString(SysID + TimeStamp + ReturnURL);
System.out.println("The Base64HashString data :" + Digest);
//Get 3DES data.
String EncryptStr = encryptByKey(TimeStamp + "$"+ReturnURL + "$"+ Digest,Key);
System.out.println("The Encrypted data :" + EncryptStr);
String DecryptStr = decryptByKey(EncryptStr,Key);
System.out.println("The Decrypted data :" + DecryptStr);
String A = "guo";
A = getAuthenicator(A,Key);
System.out.print("The Encrypted data :" + A);
}
catch(Exception Ex){
Ex.printStackTrace();
}
}
}package com.encrypt;
public class Base64Encrypt {
public static String getBASE64(String s) {
if (s == null)
return null;
return (new sun.misc.BASE64Encoder()).encode(s.getBytes());
} public static String getBASE64_byte(byte[] s) {
if (s == null)
return null;
return (new sun.misc.BASE64Encoder()).encode(s);
}
public static byte[] getByteArrFromBase64(String s) throws Exception{
if (s == null)
return null;
return (new sun.misc.BASE64Decoder()).decodeBuffer(s);
}
}
//Get Digest.
String Digest = getBase64HashString(SysID + TimeStamp + ReturnURL);
这是base64(sha1(xxx))这种加密方式
php加密是base64_encode(sha1($SysID.$TimeStamp.$ReturnURL))php加密出来结果和 java执行出来的结果不一样php 结果 YjcyZmU2ZjBlMGEyZTNhNjQ1MWRiZmEyNmI1YzBjOTlhOWU1ZDI5MQ==java 结果 ty/m8OCi46ZFHb+ia1wMmanl0pE=php3des加密 key 是86A659D3035B51B1B66DF3139F1AEC33F6651334F1E65170 36位串
而mcrypt_generic_init()函数的key只能接24 长度的串这个问题 如何解决?求教各位 高人问题比较急,语言比较混乱。抱歉
mcrypt_generic_init() [function.mcrypt-generic-init]: Key size too large; supplied length: 36, max: 24 现在是php 去请求.net的接口,需要使用sha1 base64 3des加密给的3des key是36位的java代码如下
package com.encrypt;import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.*;public class Cryptogram {
private static byte[] defaultIV = {1,2,3,4,5,6,7,8}; private static byte chr2hex(String chr) {
if (chr.equals("0")) {
return 0x00;
} else if (chr.equals("1")) {
return 0x01;
} else if (chr.equals("2")) {
return 0x02;
} else if (chr.equals("3")) {
return 0x03;
} else if (chr.equals("4")) {
return 0x04;
} else if (chr.equals("5")) {
return 0x05;
} else if (chr.equals("6")) {
return 0x06;
} else if (chr.equals("7")) {
return 0x07;
} else if (chr.equals("8")) {
return 0x08;
} else if (chr.equals("9")) {
return 0x09;
} else if (chr.equals("A")) {
return 0x0a;
} else if (chr.equals("B")) {
return 0x0b;
} else if (chr.equals("C")) {
return 0x0c;
} else if (chr.equals("D")) {
return 0x0d;
} else if (chr.equals("E")) {
return 0x0e;
} else if (chr.equals("F")) {
return 0x0f;
}
return 0x00;
} public static byte[] HexStringToByteArray(String s) {
byte[] buf = new byte[s.length() / 2];
for (int i = 0; i < buf.length; i++) {
buf[i] = (byte) (chr2hex(s.substring(i * 2, i * 2 + 1)) * 0x10 + chr2hex(s
.substring(i * 2 + 1, i * 2 + 2)));
}
return buf;
} /**
* Encrypt the data by the key.
* @param OriSource
* @return strResult
* @throws Exception
*/
public static String encryptByKey(String OriSource, String key) throws Exception {
String strResult = "";
try {
Cipher c3des = Cipher.getInstance("DESede/CBC/PKCS5Padding");
SecretKeySpec myKey = new SecretKeySpec(HexStringToByteArray(key),
"DESede"); IvParameterSpec ivspec = new IvParameterSpec(defaultIV);
c3des.init(Cipher.ENCRYPT_MODE, myKey, ivspec); byte[] testSrc = OriSource.getBytes();
byte[] encoded = c3des.doFinal(testSrc);
strResult = Base64Encrypt.getBASE64_byte(encoded);
} catch (Exception e) {
strResult="";
System.out.println("Encrypt failure!!!");
} return strResult;
}
/**
* Decrypt the encrypted data with the key.
* @param strData
* @return strResult
* @throws Exception
*/
public static String decryptByKey(String encryptedData, String key) throws Exception {
String strResult = "";
try {
Cipher c3des = Cipher.getInstance("DESede/CBC/PKCS5Padding");
SecretKeySpec myKey = new SecretKeySpec(HexStringToByteArray(key),
"DESede"); IvParameterSpec ivspec = new IvParameterSpec(defaultIV);
c3des.init(Cipher.DECRYPT_MODE, myKey, ivspec); byte[] s= Base64Encrypt.getByteArrFromBase64(encryptedData);
byte[] encoded = c3des.doFinal(s);
strResult = new String(encoded);
} catch (Exception e) {
strResult="";
System.out.println("Decrypt failure!!!");
} return strResult;
}
/**
* Decrypt the encrypted data with the key.
* @param strData
* @return strResult
* @throws Exception
*/
public static String getBase64HashString(String str) throws Exception{
byte[] testSrc = str.getBytes();
MessageDigest alga = MessageDigest.getInstance("SHA-1");
alga.update(testSrc);
byte[] digesta = alga.digest();
return Base64Encrypt.getBASE64_byte(digesta);
}
/**
* Decrypt the encrypted data with the key.
* @param strData
* @return strResult
* @throws Exception
*/
public static String getAuthenicator(String sourceStr,String key) throws Exception{
String strResult = "";
try {
Cipher c3des = Cipher.getInstance("DESede/CBC/PKCS5Padding");
SecretKeySpec myKey = new SecretKeySpec(HexStringToByteArray(key),
"DESede"); IvParameterSpec ivspec = new IvParameterSpec(defaultIV);
c3des.init(Cipher.ENCRYPT_MODE, myKey, ivspec);
byte[] testSrc = sourceStr.getBytes();
MessageDigest alga = MessageDigest.getInstance("SHA-1");
alga.update(testSrc);
byte[] digesta = alga.digest(); byte[] encoded = c3des.doFinal(digesta);
strResult = Base64Encrypt.getBASE64_byte(encoded);
} catch (Exception e) {
strResult="";
System.out.println("Decrypt failure!!!" + e.getMessage());
} return strResult;
}
public static void main(String args[]) throws Exception {
String SysID = "0005";
String TimeStamp = "2009-10-22 13:15:20";
String ReturnURL = "http://vnet.cn/passportinterface/test2.aspx";
String Key = "86A659D3035B51B1B66DF3139F1AEC33F6651334F1E65170";
try{
//Get Digest.
String Digest = getBase64HashString(SysID + TimeStamp + ReturnURL);
System.out.println("The Base64HashString data :" + Digest);
//Get 3DES data.
String EncryptStr = encryptByKey(TimeStamp + "$"+ReturnURL + "$"+ Digest,Key);
System.out.println("The Encrypted data :" + EncryptStr);
String DecryptStr = decryptByKey(EncryptStr,Key);
System.out.println("The Decrypted data :" + DecryptStr);
String A = "guo";
A = getAuthenicator(A,Key);
System.out.print("The Encrypted data :" + A);
}
catch(Exception Ex){
Ex.printStackTrace();
}
}
}package com.encrypt;
public class Base64Encrypt {
public static String getBASE64(String s) {
if (s == null)
return null;
return (new sun.misc.BASE64Encoder()).encode(s.getBytes());
} public static String getBASE64_byte(byte[] s) {
if (s == null)
return null;
return (new sun.misc.BASE64Encoder()).encode(s);
}
public static byte[] getByteArrFromBase64(String s) throws Exception{
if (s == null)
return null;
return (new sun.misc.BASE64Decoder()).decodeBuffer(s);
}
}
//Get Digest.
String Digest = getBase64HashString(SysID + TimeStamp + ReturnURL);
这是base64(sha1(xxx))这种加密方式
php加密是base64_encode(sha1($SysID.$TimeStamp.$ReturnURL))php加密出来结果和 java执行出来的结果不一样php 结果 YjcyZmU2ZjBlMGEyZTNhNjQ1MWRiZmEyNmI1YzBjOTlhOWU1ZDI5MQ==java 结果 ty/m8OCi46ZFHb+ia1wMmanl0pE=php3des加密 key 是86A659D3035B51B1B66DF3139F1AEC33F6651334F1E65170 36位串
而mcrypt_generic_init()函数的key只能接24 长度的串这个问题 如何解决?求教各位 高人问题比较急,语言比较混乱。抱歉
base64_encode(sha1($SysID.$TimeStamp.$ReturnURL))这就是php 代码啊
sha1加密 再base64 出来结果不一样。
3des的马上贴上。。
<?php
class Crypt3Des
{
public $key = "86A659D3035B51B1B66DF3139F1AEC33F6651334F1E65170";
public $iv = "12345678"; //like java: private static byte[] myIV = { 50, 51, 52, 53, 54, 55, 56, 57 };
//加密
public function encrypt($input)
{
$input = $this->padding( $input );
$key = base64_decode($this->key);
$td = mcrypt_module_open( MCRYPT_3DES, '', MCRYPT_MODE_CBC, '');
//使用MCRYPT_3DES算法,cbc模式
mcrypt_generic_init($td, $key, $this->iv);
//初始处理
$data = mcrypt_generic($td, $input);
//加密
mcrypt_generic_deinit($td);
//结束
mcrypt_module_close($td);
$data = $this->removeBR(base64_encode($data));
return $data;
}
//解密
public function decrypt($encrypted)
{
$encrypted = base64_decode($encrypted);
$key = base64_decode($this->key);
$td = mcrypt_module_open( MCRYPT_3DES,'',MCRYPT_MODE_CBC,'');
//使用MCRYPT_3DES算法,cbc模式
mcrypt_generic_init($td, $key, $this->iv);
//初始处理
$decrypted = mdecrypt_generic($td, $encrypted);
//解密
mcrypt_generic_deinit($td);
//结束
mcrypt_module_close($td);
$decrypted = $this->removePadding($decrypted);
return $decrypted;
}
//填充密码,填充至8的倍数
public function padding( $str )
{
$len = 8 - strlen( $str ) % 8;
for ( $i = 0; $i < $len; $i++ )
{
$str .= chr( 0 );
}
return $str ;
}
//删除填充符
public function removePadding( $str )
{
$len = strlen( $str );
$newstr = "";
$str = str_split($str);
for ($i = 0; $i < $len; $i++ )
{
if ($str[$i] != chr( 0 ))
{
$newstr .= $str[$i];
}
}
return $newstr;
}
//删除回车和换行
public function removeBR( $str )
{
$len = strlen( $str );
$newstr = "";
$str = str_split($str);
for ($i = 0; $i < $len; $i++ )
{
if ($str[$i] != 'n' and $str[$i] != 'r')
{
$newstr .= $str[$i];
}
}
return $newstr;
}
}
//test
$input = "1qaz2ws";
echo "plainText:" . $input."<br/>";
$crypt = new Crypt3Des();
echo "Encode:".$crypt->encrypt($input)."<br/>";
echo "Decode:".$crypt->decrypt($crypt->encrypt($input));网上找的一个。
执行encrypt 3des加密就会报错,
Warning: mcrypt_generic_init() [function.mcrypt-generic-init]: Key size too large; supplied length: 36, max: 24 in D:\AppServ\www\api\3des.php on line 29加密出来的和Java 加密出来的也不一样。
php
3des key的长度为24字节,iv为8字节.
如果提供的key为48位,可以用pack("H48", $key) , 同理iv也可以用pack("H16", $iv);
如果提供的key为32位,可以用base64_decode解码,正好也是24位.
function pkcs5_pad($text, $blocksize)
{
$pad = $blocksize - (strlen($text) % $blocksize);
return $text . str_repeat(chr($pad), $pad);
}
function pkcs5_unpad($text)
{
$pad = ord($text{strlen($text)-1});
if ($pad > strlen($text))
{
return false;
}
if( strspn($text, chr($pad), strlen($text) - $pad) != $pad)
{
return false;
}
return substr($text, 0, -1 * $pad);
}
$key = "20C86125F86DB932D0139D32D9208CEF52BAEC98F1E9BA2A";
$key = pack("H48", $key);
$iv = "0102030405060708";
$iv = pack("H16", $iv);
$td = mcrypt_module_open(MCRYPT_3DES, '', MCRYPT_MODE_CBC, '');
mcrypt_generic_init($td, $key, $iv);
$str = base64_encode(mcrypt_generic($td,pkcs5_pad("helloworld",8)));
echo $str ."";
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
$td = mcrypt_module_open(MCRYPT_3DES, '', MCRYPT_MODE_CBC, '');
mcrypt_generic_init($td, $key, $iv);
$ttt = pkcs5_unpad(mdecrypt_generic($td, base64_decode($str)));
mcrypt_generic_deinit($td);
mcrypt_module_close($td);