赞
踩
aes 对称加密算法,通过解密密文,可以得到被加密的数据!
先上代码吧!
- import javax.crypto.Cipher;
- import javax.crypto.spec.SecretKeySpec;
- import org.apache.commons.codec.binary.Base64;
- import java.net.URLDecoder;
- import java.net.URLEncoder;
-
- public static String Encrypt(String sSrc, String sKey) throws Exception {
- if (sKey == null) {
- return null;
- }
- // 判断Key是否为16位
- if (sKey.length() != 16) {
- return null;
- }
- byte[] raw = sKey.getBytes("utf-8");
- SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
- Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");// "算法/模式/补码方式"
- cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
- byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
- return Base64.encodeBase64String(encrypted);// 此处使用BASE64做转码功能,同时能
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。