이렇게하면 시작하실 수 있기를 바랍니다. 오류 처리가 누락되었으며 보안 키가 하드 코딩됩니다. 이들 모두는 생산 품질 코드를 위해 언급 될 필요가있다. 자바 측에서 당신이 Java Cryptography Architecture (JCA) 사용할 수는 PHP 측에서
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;
public final void testTesting() throws Exception {
final String plainText = "plain text";
final String result = encrypt(plainText);
System.out.println(result);
}
public String encrypt(final String plainText) throws Exception {
final byte[] data = plainText.getBytes("UTF-8");
final byte[] encoded = encrypt(data);
final String result = new String(encoded);
return result;
}
public byte[] encrypt(final byte[] data) throws Exception {
// this is just an example key, real code should use a properly generated shared secret
final byte[] secret = new byte[] {42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42};
final SecretKeySpec key = new SecretKeySpec(secret, "AES");
final Cipher encryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
encryptCipher.init(Cipher.ENCRYPT_MODE, key);
final byte[] iv = encryptCipher.getIV();
final byte[] encrypted = encryptCipher.doFinal(data);
final int outputLength = encrypted.length;
final int ivLength = iv.length;
final byte[] results = new byte[outputLength + ivLength];
System.arraycopy(iv, 0, results, 0, ivLength);
System.arraycopy(encrypted, 0, results, ivLength, outputLength);
return DatatypeConverter.printBase64Binary(encoded);
}
을, 당신은 Mcrypt을 필요로 할 것입니다. 이 PHP Mcrypt 라이브러리 PKCS5 패딩에 대한 지원을 포함하지 않는 것으로 보이기 때문에
<?php
$key = base64_decode('KioqKioqKioqKioqKioqKg==');
$input = base64_decode($_GET['input']);
$plain_text = substr($input, 16);
$initialization_vector = substr($input, 0, 16);
echo pkcs5_unpad(mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $plain_text, MCRYPT_MODE_CBC, $initialization_vector));
function pkcs5_unpad($text) {
$pad = ord($text{strlen($text)-1});
if ($pad > strlen($text)) {
return false;
}
if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) {
return false;
}
return substr($text, 0, -1 * $pad);
}
?>
기능 pkcs5_unpad
는 here에서 복사되었습니다. Java 코드는 데이터를 암호화하는 데 사용 된 초기화 벡터로 데이터 앞에 붙입니다. 이어서 PHP 코드는 초기화 벡터와 암호화 된 데이터를 두 개로 나눕니다.
이 코드는 대부분의 사용에 대해 충분히 안전해야하는 CBC 모드에서 128 비트 AES (Rijndael)을 사용합니다. 간단한 암호화 외에도 데이터가 변조되지 않았는지 확인하려면 (here)을 사용하는 것이 좋습니다. Java에서 HMAC를 수행하려면 Mac 클래스를 사용하십시오. PHP의 경우 Mhash을 참조하십시오.
와우 예, 이것이 내가 필요한 것입니다. 나는 이것을 시도해 볼 것이고,이 모든 것을 설정해 주셔서 감사 드리며, 다시보고 할 것입니다. – user246114