安卓AES加密

摘要:
AES是一种迭代对称密钥块密码。AES算法加密强度高,执行效率高,使用简单。建议在实际开发中选择AES算法。注意,16个字符的应用程序项目是关于json传输的Mapm
一:什么是AES加密
AES高级加密标准(英语:Advanced Encryption Standard,缩写:AES),在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准。
这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用。
AES 是一个迭代的、对称密钥分组的密码,AES算法加密强度大,执行效率高,使用简单,实际开发中建议选择AES 算法。
 
二:AES加密安卓中的实现
安卓中AES加密方案如下:
安卓里面提供了2个API:SecretKeySpec和Cipher
明文和密文,由Ciper提供加密和解密的函数
 
三:下面看看在项目中的应用
/**
 * Created by 磊磊tua on 2019/9/23/023.
 */
public class AesUtils {
    public static final String VIPARA = "1234567890123456";
    public static final String bm = "UTF-8";
    public static final String password = "1234567890123456";//password 生成秘钥的关键字


    /**
     * 字节数组转化为大写16进制字符串
     *
     * @param b
     * @return
     */
    private static String byte2HexStr(byte[] b) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < b.length; i++) {
            String s = Integer.toHexString(b[i] & 0xFF);
            if (s.length() == 1) {
                sb.append("0");
            }

            sb.append(s.toUpperCase());
        }

        return sb.toString();
    }

    /**
     * 16进制字符串转字节数组
     *
     * @param s
     * @return
     */
    private static byte[] str2ByteArray(String s) {
        int byteArrayLength = s.length() / 2;
        byte[] b = new byte[byteArrayLength];
        for (int i = 0; i < byteArrayLength; i++) {
            byte b0 = (byte) Integer.valueOf(s.substring(i * 2, i * 2 + 2), 16)
                    .intValue();
            b[i] = b0;
        }

        return b;
    }


    /**
     * AES 加密
     *
     * @param content  明文
     * @param
     * @return
     */

    public static String aesEncrypt(String content) {
        try {
            IvParameterSpec zeroIv = new IvParameterSpec(VIPARA.getBytes());
            SecretKeySpec key = new SecretKeySpec(password.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);
            byte[] encryptedData = cipher.doFinal(content.getBytes(bm));
            // return new String(encryptedData,bm);
            return Base64.encode(encryptedData);
//          return byte2HexStr(encryptedData);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * AES 解密
     *
     * @param content  密文
     * @param password 生成秘钥的关键字
     * @return
     */

    public static String aesDecrypt(String content, String password) {
        try {
            byte[] byteMi = Base64.decode(content);
//          byte[] byteMi=  str2ByteArray(content);
            IvParameterSpec zeroIv = new IvParameterSpec(VIPARA.getBytes());
            SecretKeySpec key = new SecretKeySpec(password.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, key, zeroIv);
            byte[] decryptedData = cipher.doFinal(byteMi);
            return new String(decryptedData, "utf-8");
        } 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();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        }
        return null;
    }

}

 注意:在ios和后台上

VIPARA(偏移量)
password (键值)都得统一标准 来实现多终端的解密
public static final String VIPARA = ""; //注意需要16字符
public static final String password = "";//password 生成秘钥的关键字。注意需要16字符

 应用项目中是关于json传输的(只要是String字符串就可以)

Map<String, String> map = new HashMap<>();
map.put("order_id", order_id);
map.put("input_password", payWord);
String aesStr = AesUtils.aesEncrypt(JsonUtil.getInstance().toJson(map));

  磊磊tua

 

免责声明:内容来源于网络,仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇求实(FireBird ytht)入门软件调试工具——GDB下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

pycryto模块的终极安装方式

安装环境:windows7,64位 Python版本3.4.3 今天费了好久的功夫,也是没有认真弄,终于安装好了,pycryto模块,这个模块是干什么的呢,百度粘贴如下 高级加密标准(Advanced Encryption Standard,AES),是美国联邦政府采用的一种区块加密标准。这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用。经过...

Crypto++ AES 加密解密流程

// aesdemo.cpp : 定义控制台应用程序的入口点。 // #include <stdio.h>#include <tchar.h>#include <iostream> #include "aes.h" using namespace std; using namespace CryptoPP;...

AES加解密代码

package com.albedo.security; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; import java.secu...

TLS1.2协议设计原理

目录 前言 为什么需要TLS协议 发展历史 协议设计目标 记录协议 握手步骤 握手协议 Hello Request Client Hello Server Hello Certificate Server Key Exchange Certificate Request Server Hello Done Client Certificate C...

java加解密(一)

1、杂谈   1、古典密码学     核心:替换法/位移法(凯撒加密)     破解方法:频率分析法,即研究字母和字母组合在文本中出现的概率。   2、近代密码学:     恩尼格玛机     被图灵破解   3、现代密码学:     1、散列函数:散列函数,也叫杂凑函数、摘要函数或哈希函数,可将任意长度的消息经过运算,变成固定长度数值,常...

R绘图 第七篇:绘制条形图(ggplot2)

使用geom_bar()函数绘制条形图,条形图的高度通常表示两种情况之一:每组中的数据的个数,或数据框中列的值,高度表示的含义是由geom_bar()函数的参数stat决定的,stat在geom_bar()函数中有两个有效值:count和identity。默认情况下,stat="count",这意味着每个条的高度等于每组中的数据的个数,并且,它与映射到y的...