Android加密篇 AES

Android加密篇 AESAES加密是一种高级加密标准,是一种区块加密标准。它是一个对称密码,就是说加密和解密用相同的密钥。WPA/WPA2经常用的加密方式就是AES加密算法。importjava.io.UnsupportedEncodingException;importjavax.crypto.Cipher;importjavax.crypto.spec.SecretKeySpec;publicclassAESUtils3{/*算法/模式/填充*/privatestatic.

大家好,又见面了,我是你们的朋友全栈君。

在这里插入图片描述

AES加密是一种高级加密标准,是一种区块加密标准。它是一个对称密码,就是说加密和解密用相同的密钥。WPA/WPA2经常用的加密方式就是AES加密算法。

import java.io.UnsupportedEncodingException;

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

public class AESUtils3 { 
   
    /* 算法/模式/填充 */
    private static final String CipherMode = "AES/ECB/PKCS5Padding";

    /* 创建密钥 */
    private static SecretKeySpec createKey(String password) { 
   
        byte[] data = null;
        if (password == null) { 
   
            password = "";
        }
        StringBuffer sb = new StringBuffer(32);
        sb.append(password);
        while (sb.length() < 32) { 
   
            sb.append("0");
        }
        if (sb.length() > 32) { 
   
            sb.setLength(32);
        }

        try { 
   
            data = sb.toString().getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) { 
   
            e.printStackTrace();
        }
        return new SecretKeySpec(data, "AES");
    }

    /* 加密字节数据 */
    public static byte[] encrypt(byte[] content, String password) { 
   
        try { 
   
            SecretKeySpec key = createKey(password);
            System.out.println(key);
            Cipher cipher = Cipher.getInstance(CipherMode);
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] result = cipher.doFinal(content);
            return result;
        } catch (Exception e) { 
   
            e.printStackTrace();
        }
        return null;
    }

    /*加密(结果为16进制字符串) */
    public static String encrypt(String content, String password) { 
   
        byte[] data = null;
        try { 
   
            data = content.getBytes("UTF-8");
        } catch (Exception e) { 
   
            e.printStackTrace();
        }
        data = encrypt(data, password);
        String result = byte2hex(data);
        return result;
    }

    /*解密字节数组*/
    public static byte[] decrypt(byte[] content, String password) { 
   
        try { 
   
            SecretKeySpec key = createKey(password);
            Cipher cipher = Cipher.getInstance(CipherMode);
            cipher.init(Cipher.DECRYPT_MODE, key);
            byte[] result = cipher.doFinal(content);
            return result;
        } catch (Exception e) { 
   
            e.printStackTrace();
        }
        return null;
    }

    /*解密16进制的字符串为字符串 */
    public static String decrypt(String content, String password) { 
   
        byte[] data = null;
        try { 
   
            data = hex2byte(content);
        } catch (Exception e) { 
   
            e.printStackTrace();
        }
        data = decrypt(data, password);
        if (data == null) return null;
        String result = null;
        try { 
   
            result = new String(data, "UTF-8");
        } catch (UnsupportedEncodingException e) { 
   
            e.printStackTrace();
        }
        return result;
    }

    /*字节数组转成16进制字符串 */
    public static String byte2hex(byte[] b) { 
    // 一个字节的数,
        StringBuffer sb = new StringBuffer(b.length * 2);
        String tmp = "";
        for (int n = 0; n < b.length; n++) { 
   
            // 整数转成十六进制表示
            tmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
            if (tmp.length() == 1) { 
   
                sb.append("0");
            }
            sb.append(tmp);
        }
        return sb.toString().toUpperCase(); // 转成大写
    }

    /*将hex字符串转换成字节数组 */
    private static byte[] hex2byte(String inputString) { 
   
        if (inputString == null || inputString.length() < 2) { 
   
            return new byte[0];
        }
        inputString = inputString.toLowerCase();
        int l = inputString.length() / 2;
        byte[] result = new byte[l];
        for (int i = 0; i < l; ++i) { 
   
            String tmp = inputString.substring(2 * i, 2 * i + 2);
            result[i] = (byte) (Integer.parseInt(tmp, 16) & 0xFF);
        }
        return result;
    }
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/145447.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 阿里笔试题(2015)持续更新中

    阿里笔试题(2015)持续更新中第一次做阿里笔试题,除了ACM题之外从来没有做过校招网络题呀,完全是裸考,总体感觉吧,对于我来说,感觉时间不够用,不是题不会,感觉时间紧,大脑很混乱,总结这一次的笔试题废话不多说,直接上题和答案平均每个人逗留时间为20分钟,那么开场前20分钟一共来了400人,且有20个人逗留时间已经到,应该容纳400人双向循环列表,从任何一个元素开始可以遍历全部元素先和后面的元素相

    2022年5月24日
    40
  • java写helloworld_Java编写的第一个程序:HelloWorld

    java写helloworld_Java编写的第一个程序:HelloWorld原理:Java文件需要编译后才能运行,编译命令为javacHelloWorld.java(使用javac.exe命令),编译之后会出现以.class结尾的字节码文件(HelloWorld.class)。运行的是字节码文件,运行命令为javaHelloWorld在桌面上创建一个code文件夹,在code文件夹中创建一个HelloWorld.java文件1、编写代码在HelloWorld.jav…

    2022年7月9日
    28
  • 三维点云拼接的方法_图像拼接算法研究

    三维点云拼接的方法_图像拼接算法研究apap算法:mdltmatlab很多内置函数都是对列操作,如mean()VLFEAT库检测和匹配SIFT关键点kp1,kp2,matches关键点坐标齐次化:(x,y,1)归一化:normalise2dpts,Functiontranslatesandnormalisesasetof2Dhomogeneouspointssothatthei…

    2022年9月22日
    5
  • 计算机网络基础知识整理「建议收藏」

    参考书目:计算机网络(第6版谢希仁)一、概述1、三网:电信网络、有线电视网络和计算机网络2、电路交换、报文交换、分组交换的比较电路交换公共电话网(PSTN网)和移动网(包括GSM和CDMA网)采用的都是电路交换技术,它的基本特点是采用面向连接的方式,在双方进行通信之前,需要为通信双方分配一条具有固定宽带的通信电路,通信双方在通信过程中一直占用所分配的资源,直到通信结束,并且在电…

    2022年4月17日
    81
  • SCL语言_scl语言优势

    SCL语言_scl语言优势西门子SCL语言处理数据的“先进先出”处理案例先进先出,是指根据先入库先发出的原则,对于发出的存货以先入库存货的单价计算发出存货成本的方法。(1)、建立数据(自己设计,自己理解的,如有错误请指教)建立一个DB块,在DB块中建立上面图的数据,“先进”的解释:在名称read的数据写入变量,把名称位read_start的值写为1,read的数据写入下面write/read_no的数组中,反复操作,先进来的数据会排列到数组数据的最上层,依次是后进来的数据。直到把数组填充满而无法写入。“先出”..

    2022年10月6日
    3
  • linux efi shell,EFI Shell 命令说明「建议收藏」

    linux efi shell,EFI Shell 命令说明「建议收藏」EFIShell命令说明引导命令—EFIShell与nPartition引导有关的命令。autoboot设置(查看)自动引导超时变量。bcfg显示(或修改)驱动程序(或引导配置)。boottest设置(或查看)BootTest位。dbprofile显示/修改要由lanboot使用的直接引导配置文件。lanboot在LAN上引导。reconfigrese…

    2022年7月24日
    42

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号