0
我需要一個PHP腳本執行以下操作順序:加密Jar文件,alterate數據,再次對其進行加密,並保存
- 1)用戶輸入一個密鑰,我們稱之爲$ user_key
- 2)讀取默認的文件內容(的file_get_contents保存在$數據)
- 3)3個的隨機密鑰加密在AES 128中的$數據(保存在$數據)
- 4)的端部串聯那些3個鍵$ data
- 5)填充數據
- 6)加密與$數據用戶的密鑰
- 7)$的數據寫入一個唯一的文件
它的工作原理(Java的解密)如果我只是做了部分1,2,4 ,5,6和7.如果我用隨機密鑰對文件進行加密,它根本不起作用(Java解密不起作用)。
我在Php中犯了什麼錯誤(比如缺少填充或什麼的)?
這是我使用的代碼。我已經標記了第1行到第3行,這些腳本不能很好地工作。
$data = file_get_contents('default_file.jar');
// Generate 3 AES keys
$ramdom_key_1 = randomAESKey();
$ramdom_key_2 = randomAESKey();
$ramdom_key_3 = randomAESKey();
// Encrypt three times the raw data with the user key
$data = AESEncrypt(pad($data, 16), $ramdom_key_1); // LINE 1
$data = AESEncrypt($data, $ramdom_key_2); // LINE 2
$data = AESEncrypt($data, $ramdom_key_3); // LINE 3
// Add the 3 keys to the data raw
$data .= $ramdom_key_3 . $ramdom_key_2 . $ramdom_key_1;
// Final encryption with the user's key
$data = AESEncrypt(pad($data, 16), $user_key);
// Write the raw data to an unique file
file_put_contents('new_file.jar', $data);
這是我的Java代碼解密文件:
byte[] content = download(url);
content = Crypto.decrypt(content, user_key);
String content = new String(data);
String keys = content.substring(content.length() - 48, content.length());
String[] keys = new String[] { keys.substring(0, 16), keys.substring(16, 32), keys.substring(32, 48) };
byte[] cleared_content = new byte[content.length - 48];
System.arraycopy(content, 0, cleared_content, 0, content.length - 48);
// For each keys, decrypt the file data
for (String key : keys)
{
cleared_content = Crypto.decrypt(cleared_content, key.getBytes());
}
return cleared_content;
我的加密類看起來是這樣的(它只是一小部分):
public static byte[] decrypt(byte[] input)
{
try
{
SecretKeySpec skey = new SecretKeySpec(KEY.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, skey);
return cipher.doFinal(input);
}
catch (Exception e) {}
return input;
}
由於鍵按desc順序存儲,我只需要執行解密就行了。
快速編輯,以顯示我的墊功能在PHP中:
function pad($data, $blocksize)
{
$pad = $blocksize - (strlen($data) % $blocksize);
return $data . str_repeat(chr($pad), $pad);
}
首先,請更新與實際的錯誤描述的問題。 「它根本不工作」不是一個好的錯誤描述。如果可能,請添加Java解密代碼。 –
從編程的角度來看,代碼本身並沒有什麼錯誤。我想知道是否使用正確的模式和填充在Java中進行解密,並按照正確的順序使用密鑰。如果您僅對加密通道#1應用填充,則不應期望在Java中通過#1和#2進行解密時使用填充。我不會去加密某些東西並將密鑰附加到密文上。 –
用戶不能輸入密鑰,用戶只能輸入密碼短語。密鑰不是密碼短語。您需要密鑰派生函數來將密碼短語安全地轉換爲密鑰,例如PBKDF2,bcrypt或scrypt。如果你這樣做,你可以使用RSA實驗室的PKCS#5公共標準中定義的基於密碼的加密。 –