AES加密解密(ECB模式) - 黄孝果的博客 - CSDN博客

版权声明:本文为博主原创文章,转载请注明地址。 https://blog.csdn.net/huangxiaoguo1/article/details/78043098

高级加密标准(英语:Advanced Encryption
Standard,缩写:AES),在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准。这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用。经过五年的甄选流程,高级加密标准由美国国家标准与技术研究院(NIST)于2001年11月26日发布于FIPS
PUB 197,并在2002年5月26日成为有效的标准。2006年,高级加密标准已然成为对称密钥加密中最流行的算法之一。

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

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

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

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

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

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

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

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

密码说明

严格地说,AES和Rijndael加密法并不完全一样(虽然在实际应用中二者可以互换),因为Rijndael加密法可以支持更大范围的区块和密钥长度:AES的区块长度固定为128
比特,密钥长度则可以是128,192或256比特;而Rijndael使用的密钥和区块长度可以是32位的整数倍,以128位为下限,256比特为上限。加密过程中使用的密钥是由Rijndael密钥生成方案产生。
大多数AES计算是在一个特别的有限域完成的。
AES加密过程是在一个4×4的字节矩阵上运作,这个矩阵又称为“状态(state)”,其初值就是一个明文区块(矩阵中一个元素大小就是明文区块中的一个Byte)。(Rijndael加密法因支持更大的区块,其矩阵行数可视情况增加)加密时,各轮AES加密循环(除最后一轮外)均包含4个步骤:
AddRoundKey — 矩阵中的每一个字节都与该次轮秘钥(round key)做XOR运算;每个子密钥由密钥生成方案产生。
SubBytes — 通过非线性的替换函数,用查找表的方式把每个字节替换成对应的字节。 ShiftRows —
将矩阵中的每个横列进行循环式移位。 MixColumns — 为了充分混合矩阵中各个直行的操作。这个步骤使用线性转换来混合每列的四个字节。
最后一个加密循环中省略MixColumns步骤,而以另一个AddRoundKey取代。

加密标准

对称密码体制的发展趋势将以分组密码为重点。分组密码算法通常由密钥扩展算法和加密(解密)算法两部分组成。密钥扩展算法将b字节用户主密钥扩展成r个子密钥。加密算法由一个密码学上的弱函数f与r个子密钥迭代r次组成。混乱和密钥扩散是分组密码算法设计的基本原则。抵御已知明文的差分和线性攻击,可变长密钥和分组是该体制的设计要点。
AES是美国国家标准技术研究所NIST旨在取代DES的21世纪的加密标准。
AES的基本要求是,采用对称分组密码体制,密钥的长度最少支持为128、192、256,分组长度128位,算法应易于各种硬件和软件实现。1998年NIST开始AES第一轮分析、测试和征集,共产生了15个候选算法。1999年3月完成了第二轮AES2的分析、测试。2000年10月2日美国政府正式宣布选中比利时密码学家Joan
Daemen 和 Vincent Rijmen 提出的一种密码算法RIJNDAEL 作为 AES.
在应用方面,尽管DES在安全上是脆弱的,但由于快速DES芯片的大量生产,使得DES仍能暂时继续使用,为提高安全强度,通常使用独立密钥的三级DES。但是DES迟早要被AES代替。流密码体制较之分组密码在理论上成熟且安全,但未被列入下一代加密标准。
AES加密数据块分组长度必须为128比特,密钥长度可以是128比特、192比特、256比特中的任意一个(如果数据块及密钥长度不足时,会补齐)。AES加密有很多轮的重复和变换。大致步骤如下:1、密钥扩展(KeyExpansion),2、初始轮(Initial
Round),3、重复轮(Rounds),每一轮又包括:SubBytes、ShiftRows、MixColumns、AddRoundKey,4、最终轮(Final
Round),最终轮没有MixColumns。

效果

这里写图片描述

代码

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加密,异或加密


Original url: Access
Created at: 2019-08-12 15:36:41
Category: default
Tags: none

请先后发表评论
  • 最新评论
  • 总共0条评论