AES加密

AES加密叫Advanced Encryption Standard,是高级加密标准。

这个标准用来替代原来的DES

优点:

① 抵抗所有已知的攻击。
② 在多个平台上速度快,编码紧凑。
③ 设计简单。
 
Java加密代码如下:

  import javax.crypto.Cipher;
  import javax.crypto.spec.SecretKeySpec;
    private static String encrypt(String inputKey, String inputContent) {
        try {
            SecretKeySpec secretKeySpec = new SecretKeySpec(inputKey.getBytes("UTF-8"), "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
            byte[] encodedBytes = cipher.doFinal(inputContent.getBytes("UTF-8"));
            StringBuilder builder = new StringBuilder();
            for (byte b : encodedBytes) {
                String plainText = Integer.toHexString(0xff & b);
                if (plainText.length() < 2)
                    plainText = "0" + plainText;
                builder.append(plainText);
            }
            return builder.toString();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return "";
    }

如上代码的正确性有待商榷。

原文地址:https://www.cnblogs.com/shuada/p/7016635.html