Created
May 14, 2017 15:43
-
-
Save Haehnchen/3cb18c40fea2ab883ef1a7a8e25422dc to your computer and use it in GitHub Desktop.
PHP encrypt and JAVA decrypt with openssl and AES-128-CBC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static String decrypt(@NotNull String input, @NotNull String key){ | |
byte[] bytes = Base64.decodeBase64(input); | |
if(bytes.length < 17) { | |
return null; | |
} | |
byte[] ivBytes = Arrays.copyOfRange(bytes, 0, 16); | |
byte[] contentBytes = Arrays.copyOfRange(bytes, 16, bytes.length); | |
try { | |
Cipher ciper = Cipher.getInstance("AES/CBC/PKCS5Padding"); | |
SecretKeySpec keySpec = new SecretKeySpec(key.getBytes("UTF-8"),"AES"); | |
IvParameterSpec iv = new IvParameterSpec(ivBytes,0, ciper.getBlockSize()); | |
ciper.init(Cipher.DECRYPT_MODE, keySpec, iv); | |
return new String(ciper.doFinal(contentBytes)); | |
} catch ( | |
NoSuchAlgorithmException | | |
NoSuchPaddingException | | |
UnsupportedEncodingException | | |
InvalidAlgorithmParameterException | | |
InvalidKeyException | | |
IllegalBlockSizeException | | |
BadPaddingException ignored | |
) { | |
} | |
return null; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$iv = openssl_random_pseudo_bytes(16, $secure); | |
if (false === $secure || false === $iv) { | |
throw new \RuntimeException('iv generation failed'); | |
} | |
$data = $iv . openssl_encrypt($data, 'AES-128-CBC', $this->key, OPENSSL_RAW_DATA, $iv); |
sorry my fault, everything is working fine, had an issue with file permissions.
Not compatible with android
It is compatible, I am using it.
…On Thu, Aug 20, 2020 at 6:21 PM sumeet-mibo ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
Not compatible with android
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<https://gist.github.com/3cb18c40fea2ab883ef1a7a8e25422dc#gistcomment-3424754>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AGXLAONFVI7E5UQUPP4IQATSBUL4RANCNFSM4K7LWXMA>
.
--
Rohit Mahto
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@rohit267 l am not sure if this encryption is used for file encryption.