宁夏免费做网站,大鱼直播,周浦高端网站建设公司,网站策划制作公司 北京这里写自定义目录标题 背景JAVA代码解决思路PHP解密 背景
公司PHP开发对接一个Java项目接口#xff0c;接口返回数据有用DESede/ECB/PKCS5Padding加密#xff0c;并且key也使用了SHA1PRNG加密了#xff0c;网上找了各种办法都不能解密#xff0c;耗了一两天的时间#xf… 这里写自定义目录标题 背景JAVA代码解决思路PHP解密 背景
公司PHP开发对接一个Java项目接口接口返回数据有用DESede/ECB/PKCS5Padding加密并且key也使用了SHA1PRNG加密了网上找了各种办法都不能解密耗了一两天的时间一度怀疑人生……
JAVA代码
public class DESUtil {private static final String KEY_ALGORITHM DESede;private static final String DEFAULT_CIPHER_ALGORITHM DESede/ECB/PKCS5Padding;/* DES加密 */public static String encrypt(String content, final String key) {try {Cipher cipher Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);byte[] byteContent content.getBytes(StandardCharsets.UTF_8.name());cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(key));byte[] result cipher.doFinal(byteContent);return Base64.encodeBase64String(result);} catch (Exception ex) {log.error(【DES加密失败】:, ex);}return null;}/* DES解密 */public static String decrypt(String content, final String key) {try {Cipher cipher Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);cipher.init(Cipher.DECRYPT_MODE, getSecretKey(key));byte[] result cipher.doFinal(Base64.decodeBase64(content));return new String(result, StandardCharsets.UTF_8.name());} catch (Exception ex) {log.error(【DES解密失败】:, ex);}return null;}public static SecretKeySpec getSecretKey(final String key) {KeyGenerator kg null;try {kg KeyGenerator.getInstance(KEY_ALGORITHM);SecureRandom secureRandom SecureRandom.getInstance(SHA1PRNG);secureRandom.setSeed(key.getBytes(StandardCharsets.UTF_8.name()));kg.init(secureRandom);SecretKey secretKey kg.generateKey();return new SecretKeySpec(secretKey.getEncoded(), KEY_ALGORITHM);} catch (NoSuchAlgorithmException ex) {log.error(【DES生成秘钥失败】:, ex);} catch (UnsupportedEncodingException e) {log.error(【DES生成秘钥失败】:, e);}return null;}
}解决思路
终于找到了一个类似问题可是要会员才能看作为一名穷DS,哪有这么富有灵机一动在某鱼花了1大洋看了解决思路果然是金钱的力量功夫不负有心人啊终于寻到stackoverflow有个类似问题。 上面Java代码中的secretKey变量的比特值使用Base64Utils.encode后得到base64字符串 然后在php中使用base64_decode解码串得到密钥key
PHP解密
/* 3DES解密 */public function desDecrypt($content) {$key$this-config[desKey];$keybase64_decode($key);$content base64_decode($content);// 解密数据$plaintext openssl_decrypt($content, DES-EDE3, $key, OPENSSL_RAW_DATA);return $plaintext;}