如果密码不足16位
PHP的AES-128-ECB加解密方法不需要补全,但JAVA AES加解密要求至少16位秘钥,对于缺少的位数n,必须使用n个"\0"补全
PHP和JAVA等同的加密方法
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.misc.BASE64Encoder;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
public class AESUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(AESUtils.class);
/**
* 加密
*/
public static String Encrypt(String content, String key) {
if (content == null || key == null) return null;
try {
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES"));
byte[] bytes = cipher.doFinal(content.getBytes(StandardCharsets.UTF_8));
return new BASE64Encoder().encode(bytes);
} catch (Exception e){
LOGGER.error("AESUtils.Encrypt error!", e);
return null;
}
}
}
public function encrypt_user($data)
{
$encrypted = openssl_encrypt($data, 'AES-128-ECB', $this->secret_key, OPENSSL_RAW_DATA);
$encrypted = base64_encode($encrypted);
return $encrypted;
}