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應用防反編譯加密技術之五:對應用程序邏輯結構進行打亂混排,保證源碼可讀性降到最低