Java AES 加密解密实现

Java AES 加密解密实现

  • 遇到问题:
  • 解决方式

遇到问题:

1.在window平台加密解密正常,在Linux平台,加密正常解密失败或异常?

解决方式

1.修改代码,指定密匙生成方式,修改后的代码


import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;



public final class  EncryptUtil {

    private static String SIGN_ALGORITHMS = "SHA1PRNG";
    private static String KEY_GENERATOR = "AES";
    private static String ENCODING = "UTF-8";

    /**
     * 加密
     *
     * @param content  需要加密的内容
     * @param password 加密密码
     * @return 加密后字符串
     */
    public static String encrypt(String content, String password) {
            SecretKey key = createSecretKey(password);
            Cipher cipher = Cipher.getInstance(KEY_GENERATOR);
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] result = cipher.doFinal(content.getBytes(ENCODING));

            return parseByte2HexStr(result);
    }


    /**
     * 解密
     *
     * @param content  待解密内容
     * @param password 解密密钥
     * @return 解密后字符串
     */
    public static String decrypt(String content, String password) {
            byte[] bytes = parseHexStr2Byte(content);

            SecretKey key = createSecretKey(password);
            Cipher cipher = Cipher.getInstance(KEY_GENERATOR);
            cipher.init(Cipher.DECRYPT_MODE, key);
            byte[] result = cipher.doFinal(bytes);

            return new String(result, ENCODING);
    }

    private static SecretKey createSecretKey(String password) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_GENERATOR);
        SecureRandom random = SecureRandom.getInstance(SIGN_ALGORITHMS);
        random.setSeed(password.getBytes(ENCODING));
        keyGenerator.init(128, random);
        return new SecretKeySpec(keyGenerator.generateKey().getEncoded(), KEY_GENERATOR);
    }


    /**
     * 将16进制转换为二进制
     *
     * @param context 16进制字符串
     * @return 二进制数据
     */
    public static byte[] parseHexStr2Byte(String context) {
        int length = context.length();
        if (length < 1) {
            return null;
        }
        int two = 2;
        int size = length / two;
        byte[] result = new byte[size];
        for (int i = 0; i < size; i++) {
            int high = Integer.parseInt(context.substring(i * two, i * two + 1), 16);
            int low = Integer.parseInt(context.substring(i * two + 1, i * two + two), 16);
            result[i] = (byte) (high * 16 + low);
        }
        return result;
    }


    /**
     * 将二进制转换成16进制
     *
     * @param bytes 二进制内容
     * @return 16进制字符串
     */
    public static String parseByte2HexStr(byte[] bytes) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0, len = bytes.length; i < len; i++) {
            String hex = Integer.toHexString(bytes[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            sb.append(hex.toUpperCase());
        }
        return sb.toString();
    }

}

主要是在Linux AES加解密异常,在此记录一下。

你可能感兴趣的:(JavaSE)