import java.security.SecureRandom;
import java.util.Base64;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
public class AESQRCodeEncryptor2 {
// private static final String TOKEN = "HLB434324324234==$#";
private static final String TOKEN = "MTBNWVI3MDAyNTcxNXwxNTUwODAxMTAyNjU0fFltTXhaRGt3TlRGbU5qbGlPV000WmpsaE0yRTRORGM0TXpSaFlqRTBZVGt6TURNd00yWm1NbVkyTW1FNVltUXdNelUyTkdFM09HVXhaak0xWldJek1nPT18MWxTY3VxbDFVYjdJM1FZSnV4Zkx2aFNwbHdLQVJ4";
// private static final String salt="dh#345345435@kkj$PK#";
private static final String salt="qFcUMn5lVw";
private int pwdIterations = 65536;
private int keySize = 256;
private byte[] ivBytes;
private String keyAlgorithm = "AES";
private String encryptAlgorithm = "AES/CBC/PKCS5Padding";
private String secretKeyFactoryAlgorithm = "PBKDF2WithHmacSHA1";
public static void main(String[] args) throws Exception {
// String enString=new AESQRCodeEncryptor().encyrpt("dsada");
// String enString = "sK8gDfQvomDMbFj4FocwCJAaYEAdcJvF3dYvw8TTPmY8uiJM325da6fNQ56ZP/pmPdCTTn6XPzkuZORnyreh0E+VbZHjuO/JThudSPvexFo=";
String encrypt = new AESQRCodeEncryptor2().encrypt("{\"shopCode\":\"10MYR70025715\",\"amount\":\"0.10\",\"refId\":\"1000000001\",\"walletType\":\"HC\"}");
System.out.println("encrypt:"+encrypt);
//System.out.println("decrypt:" + AESQRCodeEncryptor2().decrypt(encrypt));
}
/**
*
* @param plainText
* @return encrypted text
* @throws Exception
*/
public String encrypt(String plainText) throws Exception {
// generate key
byte[] saltBytes = salt.getBytes("UTF-8");
byte[] iv = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
IvParameterSpec ivspec = new IvParameterSpec(iv);
SecretKeyFactory skf = SecretKeyFactory.getInstance(this.secretKeyFactoryAlgorithm);
PBEKeySpec spec = new PBEKeySpec(TOKEN.toCharArray(), saltBytes, this.pwdIterations, this.keySize);
SecretKey secretKey = skf.generateSecret(spec);
SecretKeySpec key = new SecretKeySpec(secretKey.getEncoded(), keyAlgorithm);
// AES initialization
Cipher cipher = Cipher.getInstance(encryptAlgorithm);
cipher.init(Cipher.ENCRYPT_MODE, key,ivspec);
// generate IV
this.ivBytes = cipher.getParameters().getParameterSpec(IvParameterSpec.class).getIV();
byte[] encryptedText = cipher.doFinal(plainText.getBytes("UTF-8"));
return Base64.getEncoder().encodeToString(encryptedText);
}
/**
*
* @param encryptText
* @return decrypted text
* @throws Exception
*/
public String decrypt(String encryptText) throws Exception {
byte[] saltBytes = salt.getBytes("UTF-8");
byte[] iv = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
IvParameterSpec ivspec = new IvParameterSpec(iv);
byte[] encryptTextBytes = Base64.getDecoder().decode(encryptText);
SecretKeyFactory skf = SecretKeyFactory.getInstance(this.secretKeyFactoryAlgorithm);
PBEKeySpec spec = new PBEKeySpec(TOKEN.toCharArray(), saltBytes, this.pwdIterations, this.keySize);
SecretKey secretKey = skf.generateSecret(spec);
SecretKeySpec key = new SecretKeySpec(secretKey.getEncoded(), keyAlgorithm);
System.out.println("key:"+Base64.getEncoder().encodeToString(key.getEncoded()));
// decrypt the message
Cipher cipher = Cipher.getInstance(encryptAlgorithm);
cipher.init(Cipher.DECRYPT_MODE, key,ivspec);
byte[] decyrptTextBytes = null;
try {
decyrptTextBytes = cipher.doFinal(encryptTextBytes);
} catch (IllegalBlockSizeException e) {
// TODO: handle exception
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
String text = new String(decyrptTextBytes);
return text;
}
}
一个第三方加密的java demo,我要使用php加密,看不懂java,求大佬帮忙看看有什么方法用到php
<?php
class AES {
/**
*
* @param string $string 需要加密的字符串
* @param string $key 密钥
* @return string
*/
public static function encrypt($string, $key)
{
$key = substr(openssl_digest(openssl_digest($key, 'sha1', true), 'sha1', true), 0, 16);
$data = openssl_encrypt($string, 'AES-128-ECB', $key, OPENSSL_RAW_DATA);
$data = strtoupper(bin2hex($data)); return $data;
}
/**
* @param string $string 需要解密的字符串
* @param string $key 密钥
* @return string
*/
public static function decrypt($string, $key)
{
$key = substr(openssl_digest(openssl_digest($key, 'sha1', true), 'sha1', true), 0, 16);
$decrypted = openssl_decrypt(hex2bin($string), 'AES-128-ECB', $key, OPENSSL_RAW_DATA); return $decrypted;
}
}
?>