2014-10-03 69 views
-2

對不起,這個問題。我已閱讀所有以前的問題,但我的代碼仍然無法正常工作。 在此先感謝任何能夠幫助我理解問題出在哪裏的人。使用Android和PHP加密/解密

在Android中我使用此代碼用於讀取公鑰併產生密文:

public static PublicKey getPublicKeyFromString(String stringKey) throws Exception { 
    byte[] keyBytes = stringKey.getBytes(); 
    byte[] decode = Base64.decode(keyBytes, Base64.DEFAULT); 
    KeyFactory fact = KeyFactory.getInstance("RSA"); 
    X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(decode); 
    return (PublicKey) fact.generatePublic(x509KeySpec); 
} 
public static String RSAEncrypt(final String plain, final PublicKey publicKey) 
     throws NoSuchAlgorithmException, NoSuchPaddingException, 
     InvalidKeyException, IllegalBlockSizeException, BadPaddingException { 
    byte[] encryptedBytes; 
    Cipher cipher;  
    cipher = Cipher.getInstance("RSA"); 
    cipher.init(Cipher.ENCRYPT_MODE, publicKey); 
    encryptedBytes = cipher.doFinal(plain.getBytes()); 
    return Base64.encodeToString(encryptedBytes, Base64.DEFAULT); 
} 
//I call these functions in this manner. 
private final String pubKeyString = 
//"-----BEGIN PUBLIC KEY-----" + 
"MIG..." + 
"..."; 
//"-----END PUBLIC KEY-----" 
PublicKey pubKey = RSAFunctions.getPublicKeyFromString(pubKeyString); 
String encData = RSAFunctions.RSAEncrypt("prova", pubKey); 

的publickey.php和privatekey.php文件在PHP中生成與此代碼:

<?php 
include('./Crypt/RSA.php'); 
$rsa = new Crypt_RSA(); 
extract($rsa->createKey()); // == $rsa->createKey(1024) where 1024 is the key size 
$File1 = "./privatekey.php"; 
$Handle = fopen($File1, 'w'); 
fwrite($Handle, "<?php \$privatekey=\"" . $privatekey . "\"?>"); 
fclose($Handle); 
$File2 = "./publickey.php"; 
$Handle = fopen($File2, 'w'); 
fwrite($Handle, "<?php \$publickey=\"" . $publickey . "\"?>"); 
fclose($Handle); 
?> 

在PHP中我使用解密數據驗證碼:

<?php 
include('Crypt/RSA.php'); 
require('privatekey.php'); 

$rsa = new Crypt_RSA(); 
$rsa->loadKey($privatekey); // private key 

$base64_string = $_GET["data"]; 
$base64_string = str_replace(' ', '+', $base64_string); 

$ciphertext = base64_decode($base64_string); 

//$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_OAEP); 
$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1); 
$plaintext = $rsa->decrypt($ciphertext); 

echo $plaintext; 
?> 

我也做了一個PHP隱窩測試我的解密php功能的腳本。這是我encrypt.php的代碼:

<?php 
include('Crypt/RSA.php'); 
require('publickey.php'); 

$rsa = new Crypt_RSA(); 
$rsa->loadKey($publickey); // public key 

$plaintext = $_GET["data"]; 

//$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_OAEP); 
$ciphertext = $rsa->encrypt($plaintext); 
echo base64_encode($ciphertext); 
?> 

我沒有問題,當加密和只使用PHP解密的文字,但如果我用的Android應用進行加密的數據,PHP給我解密錯誤。

感謝您的關注。

+0

什麼是錯誤? – 323go 2014-10-03 13:35:36

回答

0

在你的PHP有: CRYPT_RSA_ENCRYPTION_PKCS1

但是當你在Android上創建Cipher對象省略了模式和填充

cipher = Cipher.getInstance("RSA"); 

你應該嘗試喜歡的東西

Cipher c = Cipher.getInstance("AES/CBC/PKCS1Padding"); 

ref: http://developer.android.com/reference/javax/crypto/Cipher.html

+1

RSA與AES不太一樣。 – ntoskrnl 2014-10-03 13:51:30

+0

是真的,對不起,我忘了將AES更改爲RSA,我只是想強調他應該在兩種情況下設置相同的選項。 Cipher cipher = Cipher.getInstance(「RSA/None/PKCS1Padding」,「BC」); 可能更適合 – 2014-10-03 14:00:46

+0

我試圖把RSA放在Cipher.getInstance中,但我也把它放在KeyFactory.getInstance中,它給了我一個錯誤。 非常感謝。 – Katte 2014-10-03 15:10:37