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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Dubbo负载均衡策略之 一致性哈希

    Dubbo负载均衡策略之 一致性哈希Dubbo负载均衡策略之一致性哈希1负载均衡在这里引用dubbo官网的一段话——LoadBalance中文意思为负载均衡,它的职责是将网络请求,或者其他形式的负载“均摊”到不同的机器上。避免集群中部分服务器压力过大,而另一些服务器比较空闲的情况。通过负载均衡,可以让每台服务器获取到适合自己处理能力的负载。在为高负载服务器分流的同时,还可以避免资源浪费,一举两得。负载均衡可分为软件负载均衡和硬件负载均衡。在我们日常开发中,一般很难接触到硬件负载均衡。但软件负载均衡还是可以接触到的,比如Nginx

    2022年7月27日
    9
  • cms模板「建议收藏」

    cms模板「建议收藏」http://www.zhicheng.com/

    2022年10月9日
    3
  • Polar码概述[通俗易懂]

    Polar码概述[通俗易懂]2016年11月18日,在美国内华达州里诺召开的3GPPRAN1#87次会议,确定PolarCode作为5GeMBB(增强移动宽带)场景下控制信道编码方案。2008年,ErdalArikan在国际信息论ISIT会议上首次提出了信道极化(ChannelPolarization)的概念;2009年在“IEEETransactiononInformationTheory”期刊上

    2025年7月29日
    2
  • nacivat错误生成激活码_最新在线免费激活

    (nacivat错误生成激活码)最近有小伙伴私信我,问我这边有没有免费的intellijIdea的激活码,然后我将全栈君台教程分享给他了。激活成功之后他一直表示感谢,哈哈~IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html1S…

    2022年3月27日
    77
  • 移动端app开发,框架的选择。

    移动端app开发,框架的选择。从事java开发快三年了,最近公司因项目要求需要做一款app,个人对这方面兴趣比较大,于是网上收集资料,框架的选择,技术的论证,到今天项目需求的各个功能都做出了相应的demo同时也打好了框架,接下来就是完成细节的部分了。接下来自己会在github更新自己相应的demo,最后也将会更新整个项目,写博客的目的就是希望自己养成做笔记的习惯,同时鞭策自己不断的学习新的知识。框架的选择,因为一直在关注前端方

    2022年6月29日
    32
  • flashfxp 5.4.0.3970 绿色汉化版注册码[通俗易懂]

    flashfxp 5.4.0.3970 绿色汉化版注册码[通俗易懂]FlashFXPRegistrationDataSTARTFLASHFXP0wC2kbML0wAAAADEW5MNJwTnsl790jgG5F4CTA4jUAdMi66HHqFbShaEpE

    2022年7月1日
    139

发表回复

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

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