2012-03-12 2 views
5

내 안드로이드 응용 프로그램에서 보내고 응답 한 데이터가 AES 암호화로 암호화 된 웹 서비스와 통신 중입니다.AES 암호화 자바 -> PHP -> Java

그럼 내가하는 일은 다음과 같습니다. 나는 base64로 인코딩 된 AES 암호화 된 JSON String을 share.php로 보내고있다.

Share.php는이 문자열을 해독하여 데이터베이스에 삽입한다. 그런 다음 PHP는 응답을 암호화합니다.

내 Android 애플리케이션에서 디코딩해야하고이 메시지의 암호를 해독해야합니다.

그러나 PHP 응답의 암호 해독은 잘 진행되지 않습니다.

public class AES { 
private final String characterEncoding = "UTF-8"; 
private final String cipherTransformation = "AES/ECB/PKCS5Padding"; 
private final String aesEncryptionAlgorithm = "AES"; 

public byte[] decrypt(byte[] cipherText, byte[] key, byte [] initialVector) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException 
{ 
    Cipher cipher = Cipher.getInstance(cipherTransformation); 
    SecretKeySpec secretKeySpecy = new SecretKeySpec(key, aesEncryptionAlgorithm); 
    //IvParameterSpec ivParameterSpec = new IvParameterSpec(initialVector); 
    //cipher.init(Cipher.DECRYPT_MODE, secretKeySpecy, ivParameterSpec); 
    cipher.init(Cipher.DECRYPT_MODE, secretKeySpecy); 
    System.out.println("Do final: "+cipherText); 

    cipherText = cipher.doFinal(cipherText); 
    return cipherText; 
} 

public byte[] encrypt(byte[] plainText, byte[] key, byte [] initialVector) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException 
{ 
    Cipher cipher = Cipher.getInstance(cipherTransformation); 
    SecretKeySpec secretKeySpec = new SecretKeySpec(key, aesEncryptionAlgorithm); 
    //IvParameterSpec ivParameterSpec = new IvParameterSpec(initialVector); 
    //cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec); 
    cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec); 
    plainText = cipher.doFinal(plainText); 
    return plainText; 
} 

private byte[] getKeyBytes(String key) throws UnsupportedEncodingException{ 
    byte[] keyBytes= new byte[16]; 
    byte[] parameterKeyBytes= key.getBytes(characterEncoding); 
    System.arraycopy(parameterKeyBytes, 0, keyBytes, 0, Math.min(parameterKeyBytes.length, keyBytes.length)); 
    return keyBytes; 
} 

/// <summary> 
/// Encrypts plaintext using AES 128bit key and a Chain Block Cipher and returns a base64 encoded string 
/// </summary> 
/// <param name="plainText">Plain text to encrypt</param> 
/// <param name="key">Secret key</param> 
/// <returns>Base64 encoded string</returns> 
public String encrypt(String plainText, String key) throws UnsupportedEncodingException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException{ 
    byte[] plainTextbytes = plainText.getBytes(characterEncoding); 
    byte[] keyBytes = getKeyBytes(key); 
    //return Base64.encodeToString(encrypt(plainTextbytes,keyBytes, keyBytes), Base64.DEFAULT); 
    return Base64.encodeToString(encrypt(plainTextbytes,keyBytes, new byte[0]), Base64.DEFAULT); 
} 

/// <summary> 
/// Decrypts a base64 encoded string using the given key (AES 128bit key and a Chain Block Cipher) 
/// </summary> 
/// <param name="encryptedText">Base64 Encoded String</param> 
/// <param name="key">Secret Key</param> 
/// <returns>Decrypted String</returns> 
public String decrypt(String encryptedText, String key) throws KeyException, GeneralSecurityException, GeneralSecurityException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, IOException{ 
    byte[] cipheredBytes = Base64.decode(encryptedText, Base64.DEFAULT); 
    byte[] keyBytes = getKeyBytes(key); 
    //return new String(decrypt(cipheredBytes, keyBytes, keyBytes), characterEncoding); 
    return new String(decrypt(cipheredBytes, keyBytes, new byte[0]), characterEncoding); 
} 

}

그리고 이것은 PHP의 응답 암호화 EN 인코딩 할 수있는 코드입니다 :

AES.java입니다 내가 추측하고있어

function mc_encrypt($encrypt, $mc_key) { 
    $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB), MCRYPT_RAND); 
    $passcrypt = trim(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $mc_key, trim($encrypt), MCRYPT_MODE_ECB, $iv)); 
    $encode = base64_encode($passcrypt); 
    return $encode; 
} 

function mc_decrypt($decrypt, $mc_key) { 
    $decoded = base64_decode($decrypt); 
    $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB), MCRYPT_RAND); 
    $decrypted = trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $mc_key, trim($decoded), MCRYPT_MODE_ECB, $iv)); 
    return $decrypted; 
} 

을 그의 설정 PHP 암호화가 Java 부분의 설정과 일치하지 않습니다.

03-12 13:44:09.661: W/System.err(15717): javax.crypto.BadPaddingException: pad block corrupted 
+2

왜 https를 사용하지 않습니까? – kirilloid

+1

한 가지는 패딩 모드가 일치하지 않는다는 것입니다. http://www.php.net/manual/de/ref.mcrypt.php#69782 – Niko

답변

0

당신이 http://phpaes.com/를 살펴 보시기 바랍니다 :

수 나는 다음과 같은 오류를 받고 있어요. 그것은 순수하게 PHP로 구현 된 무료 AES 암호화 라이브러리입니다. 빠르고 사용하기가 매우 쉽습니다.

적어도이 기능을 사용하면 문제의 진정한 원인을 파악하는 데 한 걸음 더 다가 갈 수 있습니다.

+1

주목할만한 또 하나의 점은'base64 encoding'은 많은 다른 것들이 있습니다 모양과 크기. base64에서 이진 데이터를 인코딩 할 때는 클라이언트 측 코드와 서버 측 코드가 모두 올바르게 작동해야합니다. 암호화 된 데이터보다 간단한 것으로 시작하고, 귀하의 전제를 테스트하고,보다 기본적인 기반을 다 갖추 었는지 확인하는 것이 좋습니다. – infomaniac

-4

이것은 사용자가 찾고있는 대답이 아닐 수도 있지만 SSL/HTTPS 대신이 데이터를 수동으로 암호화하는 특별한 이유가 있습니까?

대부분의 경우 HTTPS는 대칭 암호를 수동으로 구현하는 것보다 구현하기 쉽고 안전합니다.

+0

SSL/HTTPS가 대칭 암호화를 대체하지 않으며 SSL/HTTPS 채널도 신뢰할 수없는 경우가 있습니다. –