AES加密解密(ECB模式)

AES加密解密(ECB模式)高级加密标准 英语 AdvancedEncr 缩写 AES 在密码学中又称 Rijndael 加密法 是美国联邦政府采用的一种区块加密标准 这个标准用来替代原先的 DES 已经被多方分析且广为全世界所使用 经过五年的甄选流程 高级加密标准由美国国家标准与技术研究院 NIST 于 2001 年 11 月 26 日发布于 FIPSPUB197 并在 2002 年 5 月 26 日成为有

MD5加密算法:http://blog.csdn.net/huangxiaoguo1/article/details/

Base64加密解密:http://blog.csdn.net/huangxiaoguo1/article/details/

异或加密解密:http://blog.csdn.net/huangxiaoguo1/article/details/

DES加密解密:http://blog.csdn.net/huangxiaoguo1/article/details/

AES自动生成base64密钥加密解密:http://blog.csdn.net/huangxiaoguo1/article/details/

AES加密解密(ECB模式):http://blog.csdn.net/huangxiaoguo1/article/details/

AES加密解密(CBC模式):http://blog.csdn.net/huangxiaoguo1/article/details/

非对称RSA加密解密:http://blog.csdn.net/huangxiaoguo1/article/details/

密码说明

加密标准

效果

这里写图片描述

代码

AESECBActivity

 import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.text.TextUtils; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import tsou.com.encryption.R; import tsou.com.encryption.aesecb.AESUtils; / * ECB模式自设定秘钥 */ public class AESECBActivity extends AppCompatActivity implements View.OnClickListener { 
     private EditText encryptionContext; private Button encryption; private TextView tvEncryption; private Button decode; private TextView tvDecode; private Activity mActivity; private Context mContext; private String key = "huangxiaoguo1234";//必须16位 private byte[] encrypt; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_aes); mActivity = this; mContext = this; encryptionContext = (EditText) findViewById(R.id.et_encryption_context); encryption = (Button) findViewById(R.id.btn_encryption); tvEncryption = (TextView) findViewById(R.id.tv_encryption); decode = (Button) findViewById(R.id.btn_decode); tvDecode = (TextView) findViewById(R.id.tv_decode); initListener(); } private void initListener() { encryption.setOnClickListener(this); decode.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.btn_encryption://加密 String encryptionString = encryptionContext.getText().toString().trim(); if (TextUtils.isEmpty(encryptionString)) { Toast.makeText(mContext, "请输入加密内容", Toast.LENGTH_SHORT).show(); return; } encrypt = AESUtils.encrypt(encryptionString.getBytes(), key.getBytes()); tvEncryption.setText(new String(encrypt)); break; case R.id.btn_decode://解密 String decodeString = tvEncryption.getText().toString().trim(); if (TextUtils.isEmpty(decodeString)) { Toast.makeText(mContext, "请先加密", Toast.LENGTH_SHORT).show(); return; } byte[] decrypt = AESUtils.decrypt(encrypt, key.getBytes()); tvDecode.setText(new String(decrypt)); break; } } } 

AESUtils

package tsou.com.encryption.aesecb; import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; / * AES加密解密工具 * * @author huangxiaoguo */ public class AESUtils { 
    / * AES加密 * * @param data * 将要加密的内容 * @param key * 密钥 * @return 已经加密的内容 */ public static byte[] encrypt(byte[] data, byte[] key) { //不足16字节,补齐内容为差值  int len = 16 - data.length % 16; for (int i = 0; i < len; i++) { byte[] bytes = { (byte) len }; data = ArrayUtils.concat(data, bytes); } try { SecretKeySpec skeySpec = new SecretKeySpec(key, "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec); return cipher.doFinal(data); } catch (Exception e) { e.printStackTrace(); } return new byte[] {}; } / * AES解密 * * @param data * 将要解密的内容 * @param key * 密钥 * @return 已经解密的内容 */ public static byte[] decrypt(byte[] data, byte[] key) { data = ArrayUtils.noPadding(data, -1); try { SecretKeySpec skeySpec = new SecretKeySpec(key, "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding"); cipher.init(Cipher.DECRYPT_MODE, skeySpec); byte[] decryptData = cipher.doFinal(data); int len = 2 + ByteUtils.byteToInt(decryptData[4]) + 3; return ArrayUtils.noPadding(decryptData, len); } catch (Exception e) { e.printStackTrace(); } return new byte[] {}; } } 

ArrayUtils

package tsou.com.encryption.aesecb; / * 数组工具 * * @author huangxiaoguo */ public class ArrayUtils { 
    / * 合并数组 * * @param firstArray * 第一个数组 * @param secondArray * 第二个数组 * @return 合并后的数组 */ public static byte[] concat(byte[] firstArray, byte[] secondArray) { if (firstArray == null || secondArray == null) { return null; } byte[] bytes = new byte[firstArray.length + secondArray.length]; System.arraycopy(firstArray, 0, bytes, 0, firstArray.length); System.arraycopy(secondArray, 0, bytes, firstArray.length, secondArray.length); return bytes; } / * 去除数组中的补齐 * * @param paddingBytes * 源数组 * @param dataLength * 去除补齐后的数据长度 * @return 去除补齐后的数组 */ public static byte[] noPadding(byte[] paddingBytes, int dataLength) { if (paddingBytes == null) { return null; } byte[] noPaddingBytes = null; if (dataLength > 0) { if (paddingBytes.length > dataLength) { noPaddingBytes = new byte[dataLength]; System.arraycopy(paddingBytes, 0, noPaddingBytes, 0, dataLength); } else { noPaddingBytes = paddingBytes; } } else { int index = paddingIndex(paddingBytes); if (index > 0) { noPaddingBytes = new byte[index]; System.arraycopy(paddingBytes, 0, noPaddingBytes, 0, index); } } return noPaddingBytes; } / * 获取补齐的位置 * * @param paddingBytes * 源数组 * @return 补齐的位置 */ private static int paddingIndex(byte[] paddingBytes) { for (int i = paddingBytes.length - 1; i >= 0; i--) { if (paddingBytes[i] != 0) { return i + 1; } } return -1; } } 

ByteUtils

package tsou.com.encryption.aesecb; public class ByteUtils { public static int byteToInt(byte b) { return (b) & 0xff; } } 

Demo下载地址:java-android:AES加密,RAS加密,DES加密,MD5加密,Base64加密,异或加密

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

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

(0)
上一篇 2026年3月19日 上午9:27
下一篇 2026年3月19日 上午9:27


相关推荐

  • google earth使用方法_国内使用google earth

    google earth使用方法_国内使用google earth文件 导入是最重要的功能,可以导入路径、图像、模型。 编辑 复制,如果选中路径对象将会复制为KML的XML语言文本。 复制为航迹,可以复制路径,但不清楚用途。 复制图像就是将当前窗口截屏。 复制视图位置会将当前的经纬度以度,分,秒的格式复制到剪贴板。 重命名是为除我的地点、临时位置不可用外,其余的都可以用。 快照视图是所有对象可用的,包括文件夹、地标、图像、路径、游览,只有在左侧窗格选中对象,这个功能才可以用。 按名称排

    2026年1月24日
    5
  • java 23中设计模式认识及自我理解

    java 23中设计模式认识及自我理解java 23中设计模式认识及自我理解

    2022年4月24日
    45
  • python安装包代码_linux编译程序

    python安装包代码_linux编译程序python下载Python源码包下载下载python源码压缩包安装详解步骤tarxfPython-3.5.2.tgzcdPython-3.5.2./configure–prefix=/usr/local–enable-sharedmakemakeinstall命令详解Linux下源码的安装一般由3个步骤组成:配置(configure)、编译(make)、安装(m

    2022年8月23日
    17
  • Tomcat安装及配置教程(超详细的图文教程)「建议收藏」

    Tomcat安装及配置教程(超详细的图文教程)「建议收藏」Tomcat安装及配置教程(超详细的图文教程)1.什么是TomcatTomcat服务器是一个免费的开放源代码的Web应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP程序的首选。对于一个初学者来说,可以这样认为,当在一台机器上配置好Apache服务器,可利用它响应HTML(标准通用标记语言下的一个应用)页面的访问请求。实际上T…

    2022年5月4日
    62
  • 即梦ai融图教程

    即梦ai融图教程

    2026年3月13日
    5
  • 基于python的电影推荐系统_python为什么叫python

    基于python的电影推荐系统_python为什么叫python好莱坞知名媒体THR《好莱坞报道者》,邀请了2800多名好莱坞影视从业人员,包括779名演员,365名制片人,268名导演等等,由他们选出自己最爱的剧集,最终汇总成为这个百大经典美(英)剧清单。看看你追的剧上榜了吗?看到第一名时,瞬间热泪盈眶!果然是他,最经典,没有之一!100、绝望主妇DesperateHousewives(2004-2012)ABC99、弗尔蒂旅馆FawltyTowe…

    2022年9月30日
    5

发表回复

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

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