-
待加密字符串:315edfbdfrb5rgfhb1651656
-
密钥:com.sccin.cn
-
java 加密后的数据为:
cC94QUdYVnBJcW5HRmJyRU1IcUxGbUlzQ0pnQmQvNlh3K3FTMjA1MHl2WT0= -
php 加密后的数据:
yf1jBb6i/PPK3EtOw9VaJffT1FM8p+jbixHVXug+ygc= -
加密的结果以 java 的结果为准
php 代码:
$key = 'com.sccin.cn'; $str = '315edfbdfrb5rgfhb1651656'; $data = 'cC94QUdYVnBJcW5HRmJyRU1IcUxGbUlzQ0pnQmQvNlh3K3FTMjA1MHl2WT0='; var_dump(openssl_encrypt($str, 'AES-256-ECB', $key, OPENSSL_RAW_DATA, null)); java 代码
public static byte[] encrypt(String content, String password) { try { KeyGenerator kgen = KeyGenerator.getInstance("AES"); SecureRandom securerandom = new SecureRandom(tohash256Deal(password)); kgen.init(256, securerandom); SecretKey secretKey = kgen.generateKey(); byte[] enCodeFormat = secretKey.getEncoded(); SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES"); Security.addProvider(new BouncyCastleProvider()); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC"); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] byteCOntent= content.getBytes("utf-8"); byte[] cryptograph = cipher.doFinal(byteContent; return Base64.getEncoder().encode(cryptograph); } catch (Exception e) { e.printStackTrace(); } return null; } private static byte[] tohash256Deal(String datastr) { try { MessageDigest digester = MessageDigest.getInstance("SHA-256"); digester.update(datastr.getBytes()); byte[] hex = digester.digest(); return hex; } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e.getMessage()); } } 