2014-10-03 2 views
-2

이 질문에 대한 죄송합니다. 이전 질문을 모두 읽었지만 내 코드는 여전히 작동하지 않습니다. 문제가있는 부분을 이해하는 데 도움이 될만한 사람에게 미리 감사드립니다.Android 및 PHP로 암호화/해독

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 기능을 테스트하기위한 스크립트.

<?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를 사용하여 텍스트를 해독,하지만 안드로이드 응용 프로그램과 함께 만들어 암호화 된 데이터를 사용하는 경우, PHP 나에게 해독의 오류를 줄이는 내 encrypt.php의 코드 .

감사합니다. 당신의 PHP에서

+0

무엇이 오류입니까? – 323go

답변

0

이있다 : CRYPT_RSA_ENCRYPTION_PKCS1

하지만 당신은 안드로이드에 Cipher 오브젝트를 만들 때 당신은 당신이 좋아하는 뭔가를 시도해야

cipher = Cipher.getInstance("RSA"); 

모드와 패딩을 생략

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

참조 번호 : http://developer.android.com/reference/javax/crypto/Cipher.html

+1

RSA는 AES와 완전히 동일하지 않습니다. – ntoskrnl

+0

사실, AES를 RSA로 변경하는 것을 잊어 버렸습니다. 두 경우 모두 동일한 옵션을 설정해야한다는 사실을 강조하고 싶었습니다. 암호 암호 = Cipher.getInstance ("RSA/없음/PKCS1Padding", "BC"); 이 더 적합 할 것입니다 –

+0

Cipher.getInstance에 RSA를 넣으려고했지만 KeyFactory.getInstance에도 넣었습니다. 그러면 오류가 발생했습니다. 정말 고마워요. – Katte