/** * Encrypts a string using AES encryption in ECB mode with PKCS5Padding. * * @param data The string to be encrypted. * @param key The encryption key. * @return The encrypted string in hex format. * @throws Exception If an encryption error occurs. */ publicstaticStringencrypt(String data, String key)throws Exception {
/** * Decrypts a string using AES encryption in ECB mode with PKCS5Padding. * * @param encryptedData The encrypted string in hex format to be decrypted. * @param key The decryption key. * @return The decrypted string. * @throws Exception If a decryption error occurs. */ publicstaticStringdecrypt(String encryptedData, String key)throws Exception { KeysecretKey=newSecretKeySpec(key.getBytes(), "AES"); Ciphercipher= Cipher.getInstance(ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, secretKey); byte[] decryptedBytes= cipher.doFinal(Hex.decodeHex(encryptedData.toCharArray())); returnnewString(decryptedBytes); }
publicstaticvoidmain(String[] args) { try { Stringkey="R/xxxxxxxxxxxxxx=="; // Replace this with your actual key StringoriginalString="hello1111111111";
/** * Encrypts a string using AES encryption in ECB mode with PKCS5Padding. * @param string $data The string to be encrypted. * @param string $key The encryption key. * @return string The encrypted string in hex format. * @throws Exception If an encryption error occurs. */ publicstaticfunctionencrypt($data, $key) { $cipher = openssl_encrypt( $data, self::ALGORITHM, (string)$key, OPENSSL_RAW_DATA ); returnbin2hex($cipher); }
/** * Decrypts a string using AES encryption in ECB mode with PKCS5Padding. * @param string $encryptedData The encrypted string in hex format to be decrypted. * @param string $key The decryption key. * @return string The decrypted string. * @throws Exception If a decryption error occurs. */ publicstaticfunctiondecrypt($encryptedData, $key) { $encryptedData = hex2bin($encryptedData); $decrypted = openssl_decrypt($encryptedData, self::ALGORITHM, (string)$key, OPENSSL_RAW_DATA); return$decrypted; } }