Android AES+Base64加解密

import android.util.Base64;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
 * 加密
 * Created by fxf on 2018/1/9.
 */

public class AESUtil {

    /**
     * 秘钥长度
     */
    private static final int SECURE_KEY_LENGTH = 16;

    private static final String IV_STRING = "16-Bytes--String";


    /**
     * 采用AES128加密
     *
     * @param content 要加密的内容
     * @param secureKey 密钥
     * @return
     */
    public static String encrypt(String content, String secureKey) {
        if (content == null) {
            return null;
        }
        try {
            // 获得密匙数据
            byte[] rawKeyData = getAESKey(secureKey);
            // 从原始密匙数据创建KeySpec对象
            SecretKeySpec key = new SecretKeySpec(rawKeyData, "AES");
            // Cipher对象实际完成加密操作
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            // 用密匙初始化Cipher对象
            byte[] initParam = IV_STRING.getBytes();
            IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam);
            cipher.init(Cipher.ENCRYPT_MODE, key, ivParameterSpec);
            // 正式执行加密操作

            byte[] encryptByte = cipher.doFinal(content.getBytes());

            return  Base64.encodeToString(encryptByte,Base64.NO_WRAP);

        }catch (UnsupportedEncodingException e){

        }catch (NoSuchAlgorithmException e){

        }catch (NoSuchPaddingException E){

        }catch (InvalidAlgorithmParameterException e){

        }catch (InvalidKeyException e){

        }catch (IllegalBlockSizeException e){

        }catch (BadPaddingException e){

        }
        return null;

    }

    public static byte[] getAESKey(String key)
            throws UnsupportedEncodingException {
        byte[] keyBytes;
        keyBytes = key.getBytes("UTF-8");
        byte[] keyBytes16 = new byte[SECURE_KEY_LENGTH];
        System.arraycopy(keyBytes, 0, keyBytes16, 0,
                Math.min(keyBytes.length, SECURE_KEY_LENGTH));
        return keyBytes16;
    }

    /**
     * 采用AES128解密
     *
     * @param content
     * @param secureKey
     * @return
     * @throws Exception
     *             ,Exception
     * @throws Exception
     */
    public static String decrypt(String content, String secureKey) {
        if (content == null) {
            return null;
        }

        byte[] data =  Base64.decode(content,Base64.NO_WRAP);

        try {
            // 获得密匙数据
            byte[] rawKeyData = getAESKey(secureKey); // secureKey.getBytes();
            // 从原始密匙数据创建一个KeySpec对象
            SecretKeySpec key = new SecretKeySpec(rawKeyData, "AES");
            // Cipher对象实际完成解密操作
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            // 用密匙初始化Cipher对象
            byte[] initParam = IV_STRING.getBytes();
            IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam);
            cipher.init(Cipher.DECRYPT_MODE, key, ivParameterSpec);
            return new String(cipher.doFinal(data),"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {

        } catch (NoSuchPaddingException e) {

        } catch (InvalidKeyException e) {

        } catch (InvalidAlgorithmParameterException e) {

        } catch (IllegalBlockSizeException e) {

        } catch (BadPaddingException e) {

        }


        return null;
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容