package com.example.rog_pc.myapplication ;

import java.security.Key ;
import java.security.NoSuchAlgorithmException ;
import java.security.SecureRandom ;

import javax.crypto.Cipher ;
import javax.crypto.KeyGenerator ;
import javax.crypto.SecretKey ;
import javax.crypto.spec.SecretKeySpec ;

/**
 * Created by a12 on 2016/7/31.
 */
public class amAES {

    /**
     * 密钥算法
     */
    public  final String  KEY_ALGORITHM "AES" ;

    /**
     * 加密/解密算法 / 工作模式 / 填充方式
     * Java 6支持PKCS5Padding填充方式
     * Bouncy Castle支持PKCS7Padding填充方式
     */
    public  final String  CIPHER_ALGORITHM "AES/ECB/PKCS5Padding" ;

    /**
     * 生成密钥的数 ?
     *
     *  @return  byte[] 二进制key
     *  @throws  Exception
     */
    public  byte[]  generateSecretKey()  {
        // 实例
        KeyGenerator kg =  null;
        try {
            kg = KeyGenerator. getInstance( KEY_ALGORITHM) ;
       catch (NoSuchAlgorithmException e) {
            e.printStackTrace() ;
        }
        // AES 要求密钥长度   128位 192位或 256
        kg.init( 128 , new SecureRandom()) ;
        // 生成秘密密钥
        SecretKey secretKey = kg.generateKey() ;
        // 获得密钥的二进制编码形式
        return secretKey.getEncoded() ;
    }

    /**
     * 转换密钥
     *
     *  @param  key  二进制密 ?
     *  @return  Key 密钥
     *  @throws  Exception
     */
    private  SecretKey  toKey( byte[] key)  throws Exception {
        // 实例化AES密钥材料
        SecretKey secretKey =  new SecretKeySpec(key KEY_ALGORITHM) ;
        return secretKey ;
    }

    /**
     * 加密
     *
     *  @param  data  待加密数 ?
     *  @param  key  密钥
     *  @return  byte[] 加密数据
     *  @throws  Exception
     */
    public  byte[]  encrypt( byte[] data , byte[] key)  {

        // 还原密钥
        Key aeskey =  null;
        byte[] bytesRet =  null;
        try {
            aeskey = toKey(key) ;
            /*
       * 实例 ?
       * 使用PKCS7Padding填充方式,按如下方式实现
       * Cipher.getInstance(CIPHER_ALGORITHM, "BC");
       */
            Cipher cipher = Cipher. getInstance( CIPHER_ALGORITHM) ;

            // 初始化,设置为加密模 ?
            cipher.init(Cipher. ENCRYPT_MODE aeskey) ;

            // 执行操作
            bytesRet = cipher.doFinal(data) ;
       catch (Exception e) {
            e.printStackTrace() ;
        }
        return  bytesRet ;
    }

    /**
     * 解密
     *
     *  @param  data  待解密数 ?
     *  @param  key  密钥
     *  @return  byte[] 解密数据
     *  @throws  Exception
     */
    public  byte[]  decrypt( byte[] data , byte[] key)  {

        Key aeskey =  null;
        byte[] bytesRet =  null;
        try {
            aeskey = toKey(key) ;
            /*
       * 实例 ?
       * 使用PKCS7Padding填充方式,按如下方式实现
       * Cipher.getInstance(CIPHER_ALGORITHM, "BC");
       */
            Cipher cipher = Cipher. getInstance( CIPHER_ALGORITHM) ;

            // 初始化,设置为解密模 ?
            cipher.init(Cipher. DECRYPT_MODE aeskey) ;

            // 执行操作

            bytesRet = cipher.doFinal(data) ;
       catch (Exception e) {
            e.printStackTrace() ;
        }
        return  bytesRet ;
    }


}

更多相关文章

  1. android三种监听方法
  2. 在已有的Android(安卓)签名基础上添加地图key的方式
  3. Xamarin.Android-捕获未处理异常(全局异常)
  4. Android:Service生命周期方法与Service启动方式bindService与Star
  5. Android(安卓)刘海屏适配方案---NotchTools,适配国内四大厂商(华
  6. Android(安卓)手机端与服务器端通过http交换数据 Json
  7. Android进阶:十二、最简单的方式实现自定义阴影效果
  8. 关于android创建快捷方式会启动两个应用的问题(二)
  9. Android开发实战《手机安全卫士》——11.“进程管理”模块拓展 &

随机推荐

  1. ionic3打包安卓apk
  2. Android(安卓)TV的音量键实现流程
  3. Android(安卓)之 ViewDragHelper详解(二)
  4. Android(安卓)开发(三)使用Stirngs资源文件
  5. android c++ ndk 的编译环境搭建
  6. Android运行时报错:Activity not started
  7. Android(安卓)TableLayout TableRow点击
  8. Android的手势的保存
  9. 【Android基础】四种点击事件
  10. Android-Framework: Activity、Window、V