假设待加密的字符串为:
$json = '{"user":"zhangsan","pwd":"zhangsan888"}';如何用php来实现加密呢?以下为java加解密部分public class javaAES {
public static final String KEY_ALGORITHM = "AES";
public static final String CIPHER_ALGORITHM = "AES/ECB/PKCS7Padding";
private static final String AESKEY="bNjo+uoYgvdThJhGP*******qVh5LDXDztjsG0=";
public static byte[] initkey() throws Exception {
 return new byte[] { 0x07, 0x07, 0x03, 0x0b, 0x02, 0x0f, 0x0b, 0x0c,
 0x01, 0x03, 0x09, 0x07, 0x0c, 0x03, 0x07, 0x0a, 0x04, 0x0f,
 0x06, 0x0f, 0x0e, 0x09, 0x05, 0x01, 0x0a, 0x0a, 0x01, 0x09,
 0x06, 0x07, 0x09, 0x0d };
}

public static String genEncryptString(String data) throws Exception
{
if(StringUtils.isNullOrEmpty(data))
return null;
byte[] key = initkey();
byte[] temp =encrypt(data.getBytes(), key);
String result= new String( Base64.encode(temp));

return StringUtils.isNullOrEmpty(result)?null:result;
}

public static  String  genDecryptString(String data) throws Exception{
if(StringUtils.isNullOrEmpty(data))
return null;
return  new String( decrypt(Base64.decode(data.getBytes()),initkey()));
 
} public static Key toKey(byte[] key) throws Exception {
SecretKey secretKey = new SecretKeySpec(key, KEY_ALGORITHM);
return secretKey;
}  
public static byte[] encrypt(byte[] data, byte[] key) throws Exception {
Key k = toKey(key);
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM, "BC");
cipher.init(Cipher.ENCRYPT_MODE, k);
return cipher.doFinal(data);
}
public static byte[] decrypt(byte[] data, byte[] key) throws Exception {
Key k = toKey(key);
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, k);
return cipher.doFinal(data);
}

public static byte[] Base64StringToByte(String data) {
if (data == null || data.length() == 0) {
return null;
}
return Base64.decode(data.getBytes());
} public static String getAeskey() {
return AESKEY;
}
public static String Base64ByteToString(byte[] data) {
if (data == null || data.length == 0) {
return null;
} return new String(Base64.encode(data));
}
}

解决方案 »

  1.   

    http://blog.csdn.net/tsxw24/article/details/7644244
      

  2.   

    先谢过版主回复。。我现在有个问题是:public static String genEncryptString(String data) throws Exception{
      if(StringUtils.isNullOrEmpty(data))
      return null;
      byte[] key = initkey();
      byte[] temp =encrypt(data.getBytes(), key);
      String result= new String( Base64.encode(temp));
      return StringUtils.isNullOrEmpty(result)?null:result;
    }byte[] temp =encrypt(data.getBytes(), key);data.getBytes() 是待加密的data数组
    key  好像也是个数组而php好像只能对字符串加密:static public function encode( $key, $str ){  
            $iv = mcrypt_create_iv(mcrypt_get_iv_size(self::CIPHER,self::MODE),MCRYPT_RAND);  
            return mcrypt_encrypt(self::CIPHER, $key, $str, self::MODE, $iv);  
        }  $key 和$str 都必须是字符串,怎么转过来呢
      

  3.   

    在 Java、C++、C# 中的 byte[] 数组,在 php 中就是字符串
    byte[] 称为字节数组与 php 不同的是,在那些语言中,字符串是按字处理的
    一个字可以是 1字节(ascii)、2字节(Unicode)、3字节(utf-8汉字),等等.....
    而所有的内置算法都是按字节运算的,所以 byte[] 就产生了。实际就是只按字节计算