java实现AES加密

128位密钥,用jdk自带库实现

[参考1](https://blog.csdn.net/u013871100/article/details/80100992)
[参考2](https://blog.csdn.net/qq_18870023/article/details/52183755)

AES涉及几个参数:加密模式、补码方式、偏移量
经验证jdk默认的是ECB模式,并不是参考中所说的CBC
因为,ECB模式是不需要偏移的,默认情况下插入偏移,从jdk报错信息提示来看,默认确定是ECB
代码在下,加密和解密都没问题。可是加密结果和在线加密工具出的结果不一致
很费解为什么不一致,参数设置都已经一样了,只能先这样了
public class AesUtil {

	public static String encrypt(String content, String password) {
        try {
            KeyGenerator kgen = KeyGenerator.getInstance("AES");
            kgen.init(128, new SecureRandom(password.getBytes()));
            SecretKey secretKey = kgen.generateKey();
            byte[] enCodeFormat = secretKey.getEncoded();
            SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
            Cipher cipher = Cipher.getInstance("AES");//默认ECB模式
            byte[] byteContent = content.getBytes("utf-8");
            cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化
            byte[] result = cipher.doFinal(byteContent);
            return new Base64().encodeToString(result); // 加密
//            return byteToHexString(result);
        } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
        } catch (NoSuchPaddingException e) {
                e.printStackTrace();
        } catch (InvalidKeyException e) {
                e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
                e.printStackTrace();
        } catch (BadPaddingException e) {
                e.printStackTrace();
        }
        return null;
	}
	
	public static String decrypt(String content, String password) {
	    try {
        	KeyGenerator kgen = KeyGenerator.getInstance("AES");
         	kgen.init(128, new SecureRandom(password.getBytes()));
         	SecretKey secretKey = kgen.generateKey();
            byte[] enCodeFormat = secretKey.getEncoded();
            SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");            
            Cipher cipher = Cipher.getInstance("AES");// 创建密码器
            cipher.init(Cipher.DECRYPT_MODE, key);// 初始化
            byte[] result = cipher.doFinal(Base64.decodeBase64(content));
            return new String(result); // 加密
	    } catch (NoSuchAlgorithmException e) {
	            e.printStackTrace();
	    } catch (NoSuchPaddingException e) {
	            e.printStackTrace();
	    } catch (InvalidKeyException e) {
	            e.printStackTrace();
	    } catch (IllegalBlockSizeException e) {
	            e.printStackTrace();
	    } catch (BadPaddingException e) {
	            e.printStackTrace();
	    }
	    return null;
	}
	
//	public static String byteToHexString(byte[] bytes) {
//        StringBuffer sb = new StringBuffer(bytes.length);
//        String sTemp;
//        for (int i = 0; i < bytes.length; i++) {
//            sTemp = Integer.toHexString(0xFF & bytes[i]);
//            if (sTemp.length() < 2)
//                sb.append(0);
//            sb.append(sTemp.toUpperCase());
//        }
//        return sb.toString();
//	}
}

你可能感兴趣的:(杂项)