待加密字符串: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());
}
}
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.