php AES 加密,java解密 CBC模式256位

能力一般 发布于 2015/01/20 10:15
阅读 2K+
收藏 0

如题,,下面的代码是java的aes(256位CBC模式)解密,现在请问,php加密怎么做???

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
import java.util.Arrays;


import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;


import org.apache.commons.codec.binary.Base64;


/**
 * Aes加密
 * 
 * @author debbie
 */
public class AesUtils {


// 加解密统一使用的编码方式  
    private final static String encoding = "utf-8";  
  
    private static class SingleBase64 {
          private static final Base64 instance = new Base64();
    }
     

    /**
* 加密字符串 in AES-256 with a given key
*
* @param context
* @param password
* @param text
* @return String Base64 and AES encoded String
*/
public static String encode(String stringToEncode,String secretKey) throws NullPointerException {
   if (stringToEncode.length() == 0 || stringToEncode == null) {
       throw new NullPointerException("Please give text");
   }
   try {
       SecretKeySpec skeySpec = getKey(secretKey);
       byte[] clearText = stringToEncode.getBytes("UTF8");
       
       // IMPORTANT TO GET SAME RESULTS ON iOS and ANDROID
       final byte[] iv = new byte[16];
       Arrays.fill(iv, (byte) 0x00);
       IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
       
       // Cipher is not thread safe
       Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
       Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
       cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivParameterSpec);
       
       byte[] encodeByte = cipher.doFinal(clearText);
       Base64 base64 = SingleBase64.instance;
       encodeByte = base64.encode(encodeByte);
       return new String(encodeByte,encoding);
       
   } catch (InvalidKeyException e) {
       e.printStackTrace();
   } catch (UnsupportedEncodingException e) {
       e.printStackTrace();
   } catch (NoSuchAlgorithmException e) {
       e.printStackTrace();
   } catch (BadPaddingException e) {
       e.printStackTrace();
   } catch (NoSuchPaddingException e) {
       e.printStackTrace();
   } catch (IllegalBlockSizeException e) {
       e.printStackTrace();
   } catch (InvalidAlgorithmParameterException e) {
       e.printStackTrace();
   }
   return "";
}
}

只要PHP的加密代码,,,这是一个反向的,!!!!

加载中
返回顶部
顶部