求教php AES/CBS/PKCS5Padding加密
1个回答
2017-06-04 · 知道合伙人软件行家
关注
展开全部
要实现一个功能, 就是把字符串加密...
有一个java端的加密DEMO, 现在要转换成PHP实现..
但是PHP的得到的结果和JAVA得到的真的是完全不一样....
求大家帮帮忙,,, 看看哪里出问题了... THKS...
下面贴出JAVA端的DEMO:
Java code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
public class Aes
{
private static byte[] iv = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6};
public static String encrptAesBase64(String encryptString, String encryptKey)
throws Exception
{
if (encryptKey == null)
{
return null;
}
if (encryptString == null)
{
return null;
}
if (encryptKey.length() != 16)
{
return null;
}
IvParameterSpec zeroIv = new IvParameterSpec(iv);
byte[] keys = encryptKey.getBytes("UTF8");
SecretKeySpec key = new SecretKeySpec(keys, "AES");
Cipher cipher = Cipher.
getInstance("AES/CBC/PKCS5Padding");// 算法/模式/补码方式
cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);
byte[] encryptedData = cipher.doFinal(encryptString.getBytes("UTF8"));
String base64Str = new String(Base64.encodeBase64(encryptedData), "UTF8");
return URLEncoder.encode(base64Str, "utf-8");
}
}
下面贴出PHP的实现:
PHP code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
class MagicCrypt
{
private $iv = "1234567890123456" ;
private $encryptKey="abcdefghijklmnop" ;
function encrypt($encryptStr)
{
$localIV = $this->iv ;
$encryptKey = $this->encryptKey ;
//Open module
$module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, $localIV) ;
//print "module = $module <br/>" ;
mcrypt_generic_init($module, $encryptKey, $localIV) ;
//Padding
$block = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC) ;
$pad = $block - (strlen($encryptStr) % $block); //Compute how many characters need to pad
$encryptStr .= str_repeat(chr($pad), $pad); // After pad, the str length must be equal to block or its integer multiples
//encrypt
$encrypted = mcrypt_generic($module, $encryptStr);
//Close
mcrypt_generic_deinit($module);
mcrypt_module_close($module);
return urlencode(base64_encode($encrypted)) ;
}
}
function main() {
$appid = 110 ;
$openid = "abcdefghijklmnop" ;
$appName="应用名称" ;
$encryptKey="abcdefghijklmnop" ;
$param0="abcdefghijklmnop" ;
$encryptString= "openId=" . $openid . "&appName=" . $appName . "¶m0=" . $param0 ;
$encryptObj = new MagicCrypt() ;
$result = $encryptObj->encrypt($encryptString) ;
print "hello new2 php result = $result <br/>" ;
$result = urlencode($result) ;
print "hello new3 php result = $result <br/> " ;
}
main();
?>
有一个java端的加密DEMO, 现在要转换成PHP实现..
但是PHP的得到的结果和JAVA得到的真的是完全不一样....
求大家帮帮忙,,, 看看哪里出问题了... THKS...
下面贴出JAVA端的DEMO:
Java code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
public class Aes
{
private static byte[] iv = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6};
public static String encrptAesBase64(String encryptString, String encryptKey)
throws Exception
{
if (encryptKey == null)
{
return null;
}
if (encryptString == null)
{
return null;
}
if (encryptKey.length() != 16)
{
return null;
}
IvParameterSpec zeroIv = new IvParameterSpec(iv);
byte[] keys = encryptKey.getBytes("UTF8");
SecretKeySpec key = new SecretKeySpec(keys, "AES");
Cipher cipher = Cipher.
getInstance("AES/CBC/PKCS5Padding");// 算法/模式/补码方式
cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);
byte[] encryptedData = cipher.doFinal(encryptString.getBytes("UTF8"));
String base64Str = new String(Base64.encodeBase64(encryptedData), "UTF8");
return URLEncoder.encode(base64Str, "utf-8");
}
}
下面贴出PHP的实现:
PHP code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
class MagicCrypt
{
private $iv = "1234567890123456" ;
private $encryptKey="abcdefghijklmnop" ;
function encrypt($encryptStr)
{
$localIV = $this->iv ;
$encryptKey = $this->encryptKey ;
//Open module
$module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, $localIV) ;
//print "module = $module <br/>" ;
mcrypt_generic_init($module, $encryptKey, $localIV) ;
//Padding
$block = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC) ;
$pad = $block - (strlen($encryptStr) % $block); //Compute how many characters need to pad
$encryptStr .= str_repeat(chr($pad), $pad); // After pad, the str length must be equal to block or its integer multiples
//encrypt
$encrypted = mcrypt_generic($module, $encryptStr);
//Close
mcrypt_generic_deinit($module);
mcrypt_module_close($module);
return urlencode(base64_encode($encrypted)) ;
}
}
function main() {
$appid = 110 ;
$openid = "abcdefghijklmnop" ;
$appName="应用名称" ;
$encryptKey="abcdefghijklmnop" ;
$param0="abcdefghijklmnop" ;
$encryptString= "openId=" . $openid . "&appName=" . $appName . "¶m0=" . $param0 ;
$encryptObj = new MagicCrypt() ;
$result = $encryptObj->encrypt($encryptString) ;
print "hello new2 php result = $result <br/>" ;
$result = urlencode($result) ;
print "hello new3 php result = $result <br/> " ;
}
main();
?>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询