cbs加密
‘壹’ 求教php AES/CBS/PKCS5Padding加密
要实现一个功能, 就是把字符串加密...
有一个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 mole
$mole = mcrypt_mole_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, $localIV) ;
//print "mole = $mole <br/>" ;
mcrypt_generic_init($mole, $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($mole, $encryptStr);
//Close
mcrypt_generic_deinit($mole);
mcrypt_mole_close($mole);
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();
?>
‘贰’ 怎么防止开发出来的程序被别人反编译
1.本地数据加密
iOS应用防反编译加密技术之一:对NSUserDefaults,sqlite存储文件数据加密,保护帐号和关键信息
2.URL编码加密
iOS应用防反编译加密技术之二:对程序中出现的URL进行编码加密,防止URL被静态分析
3.网络传输数据加密
iOS应用防反编译加密技术之三:对客户端传输数据提供加密方案,有效防止通过网络接口的拦截获取数据
4.方法体,方法名高级混淆
iOS应用防反编译加密技术之四:对应用程序的方法名和方法体进行混淆,保证源码被逆向后无法解析代码
5.程序结构混排加密
iOS应用防反编译加密技术之五:对应用程序逻辑结构进行打乱混排,保证源码可读性降到最低