求解,
用PHP 实现 JAVA 默认的 AES-128加密
好像因为JAVA 与PHP的 补码方式不同,现在加密后生成的值不一致/* Import the java classes */
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;
/* Declare the variables */
private Cipher encryptCipher;
private Cipher decryptCipher;
private final static String UTF8_STRING = "UTF8";
private final static String ALGORITHM = "AES";
private final static String keyGen = "##201CBA##201CBA";
/* Create instance of the variable */
KeyGenerator kgen = KeyGenerator.getInstance(ALGORITHM);
kgen.init(128);
// Generate the secret key specs
SecretKeySpec skeySpec = new SecretKeySpec(keyGen.getBytes(), ALGORITHM);
// Get instances of the ciphers
encryptCipher = Cipher.getInstance(ALGORITHM);
// Initialize the ciphers
encryptCipher.init(Cipher.ENCRYPT_MODE, skeySpec);
public String encrypt(String text){
try{
// Encode the string into bytes using utf-8
byte[] utf8 = text.getBytes(“UTF-8”);
// Encrypt
byte[] enc = encryptCipher.doFinal(utf8);
// Encode bytes to base64 to get a string
return new sun.misc.BASE64Encoder().encode(enc);
用PHP 实现 JAVA 默认的 AES-128加密
好像因为JAVA 与PHP的 补码方式不同,现在加密后生成的值不一致/* Import the java classes */
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;
/* Declare the variables */
private Cipher encryptCipher;
private Cipher decryptCipher;
private final static String UTF8_STRING = "UTF8";
private final static String ALGORITHM = "AES";
private final static String keyGen = "##201CBA##201CBA";
/* Create instance of the variable */
KeyGenerator kgen = KeyGenerator.getInstance(ALGORITHM);
kgen.init(128);
// Generate the secret key specs
SecretKeySpec skeySpec = new SecretKeySpec(keyGen.getBytes(), ALGORITHM);
// Get instances of the ciphers
encryptCipher = Cipher.getInstance(ALGORITHM);
// Initialize the ciphers
encryptCipher.init(Cipher.ENCRYPT_MODE, skeySpec);
public String encrypt(String text){
try{
// Encode the string into bytes using utf-8
byte[] utf8 = text.getBytes(“UTF-8”);
// Encrypt
byte[] enc = encryptCipher.doFinal(utf8);
// Encode bytes to base64 to get a string
return new sun.misc.BASE64Encoder().encode(enc);
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货