可以找到工作版本-https: //github.com/chaudhuri-
ab/CrossPlatformCiphers
需要记住的一点是,如果未在PHP中指定OPENSSL_RAW_DATA,则数据将被加密为base64。那让我失望了。
PHP:
class PHP_AES_Cipher { private static $OPENSSL_CIPHER_NAME = "aes-128-cbc"; //Name of OpenSSL Cipher private static $CIPHER_KEY_LEN = 16; //128 bits static function encrypt($key, $iv, $data) { if (strlen($key) < PHP_AES_Cipher::$CIPHER_KEY_LEN) { $key = str_pad("$key", PHP_AES_Cipher::$CIPHER_KEY_LEN, "0"); //0 pad to len 16 } else if (strlen($key) > PHP_AES_Cipher::$CIPHER_KEY_LEN) { $key = substr($str, 0, PHP_AES_Cipher::$CIPHER_KEY_LEN); //truncate to 16 bytes } $enpredEncryptedData = base64_enpre(openssl_encrypt($data, PHP_AES_Cipher::$OPENSSL_CIPHER_NAME, $key, OPENSSL_RAW_DATA, $iv)); $enpredIV = base64_enpre($iv); $encryptedPayload = $enpredEncryptedData.":".$enpredIV; return $encryptedPayload; } static function decrypt($key, $data) { if (strlen($key) < PHP_AES_Cipher::$CIPHER_KEY_LEN) { $key = str_pad("$key", PHP_AES_Cipher::$CIPHER_KEY_LEN, "0"); //0 pad to len 16 } else if (strlen($key) > PHP_AES_Cipher::$CIPHER_KEY_LEN) { $key = substr($str, 0, PHP_AES_Cipher::$CIPHER_KEY_LEN); //truncate to 16 bytes } $parts = explode(':', $data); //Separate Encrypted data from iv. $decryptedData = openssl_decrypt(base64_depre($parts[0]), PHP_AES_Cipher::$OPENSSL_CIPHER_NAME, $key, OPENSSL_RAW_DATA, base64_depre($parts[1])); return $decryptedData; }}
Java:
package ciphers;import javax.crypto.Cipher;import javax.crypto.spec.IvParameterSpec;import javax.crypto.spec.SecretKeySpec;import java.util.base64;public class Java_AES_Cipher { private static String CIPHER_NAME = "AES/CBC/PKCS5PADDING"; private static int CIPHER_KEY_LEN = 16; //128 bits public static String encrypt(String key, String iv, String data) { try { if (key.length() < Java_AES_Cipher.CIPHER_KEY_LEN) { int numPad = Java_AES_Cipher.CIPHER_KEY_LEN - key.length(); for(int i = 0; i < numPad; i++){ key += "0"; //0 pad to len 16 bytes } } else if (key.length() > Java_AES_Cipher.CIPHER_KEY_LEN) { key = key.substring(0, CIPHER_KEY_LEN); //truncate to 16 bytes } IvParameterSpec initVector = new IvParameterSpec(iv.getBytes("UTF-8")); SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES"); Cipher cipher = Cipher.getInstance(Java_AES_Cipher.CIPHER_NAME); cipher.init(Cipher.ENCRYPT_MODE, skeySpec, initVector); byte[] encryptedData = cipher.doFinal((data.getBytes())); String base64_EncryptedData = base64.getEnprer().enpreToString(encryptedData); String base64_IV = base64.getEnprer().enpreToString(iv.getBytes("UTF-8")); return base64_EncryptedData + ":" + base64_IV; } catch (Exception ex) { ex.printStackTrace(); } return null; } public static String decrypt(String key, String data) { try { String[] parts = data.split(":"); IvParameterSpec iv = new IvParameterSpec(base64.getDeprer().depre(parts[1])); SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES"); Cipher cipher = Cipher.getInstance(Java_AES_Cipher.CIPHER_NAME); cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv); byte[] depredEncryptedData = base64.getDeprer().depre(parts[0]); byte[] original = cipher.doFinal(depredEncryptedData); return new String(original); } catch (Exception ex) { ex.printStackTrace(); } return null; }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)