[security] - AES Encryption

1. AES Algorithm

  • The Advanced Encryption Standard (AES), also as known as Rijndael (its original name), is a specification for encryption of electronic data established by the U.S. National Institute of Standard and Technology (NIST) in 2001.
  • It uses a fixed long key to encrypt and decrypt data, available key size, 128, 192 and 256 bits.
  • Use case: A want to send a message to friend B, and A does not want anyone else to see it. So A use a key to encrypt his message and share this key with B, tell B he need decrypt the message with this key later.

2. Encryption

  1. Generate a key
  2. Share this key with B
  3. Encrypt data with this key
  4. Transmit encrypted data to B
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.ShortBufferException;

/**
 *
 */
public class AESEncrypt {

    public static void main(String[] args) throws NoSuchAlgorithmException, IOException,
            NoSuchPaddingException, InvalidKeyException, ShortBufferException,
            IllegalBlockSizeException, BadPaddingException {

        // Generate key and store into file
        SecureRandom random = new SecureRandom(); // see below
        KeyGenerator keyGen = KeyGenerator.getInstance("AES");
        keyGen.init(random);
        SecretKey secretKey = keyGen.generateKey();

        FileOutputStream secretKeyOut = new FileOutputStream(Util.PATH_SECRETKEY);
        secretKeyOut.write(secretKey.getEncoded());
        secretKeyOut.close();

        // Cipher
        Cipher aesCipher = Cipher.getInstance("AES");
        aesCipher.init(Cipher.ENCRYPT_MODE, secretKey);

        // Encrypt
        BufferedInputStream dataIn = new BufferedInputStream(new FileInputStream(Util.PATH_DATA));
        BufferedOutputStream encryptedDataOut = new BufferedOutputStream(new FileOutputStream(Util.PATH_DATA_ENCRYPTED));

        byte[] inBytes = new byte[aesCipher.getBlockSize()];
        byte[] outByte;
        int len;
        while ((len = dataIn.read(inBytes)) >= 0) {
            outByte = aesCipher.update(inBytes, 0, len);
            encryptedDataOut.write(outByte);
        }
        outByte = aesCipher.doFinal();
        encryptedDataOut.write(outByte);

        dataIn.close();
        encryptedDataOut.close();
    }

}

3. Decryption

  1. Get and restore the key
  2. Decrypt data with key
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

/**
 * Class documentation to be filled TODO
 */
public class AESDecrypt {

    public static void main(String[] args) throws IOException, ClassNotFoundException,
            NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
            IllegalBlockSizeException, BadPaddingException {

        // Get key
        FileInputStream secretKeyIn = new FileInputStream(Util.PATH_SECRETKEY);
        byte[] secretKeyBytes = new byte[secretKeyIn.available()];
        secretKeyIn.read(secretKeyBytes);
        secretKeyIn.close();
        SecretKey secretKey = new SecretKeySpec(secretKeyBytes, "AES");

        // Cipher
        Cipher aesCipher = Cipher.getInstance("AES");
        aesCipher.init(Cipher.DECRYPT_MODE, secretKey);

        // Decrypt
        BufferedInputStream encryptedDataIn = new BufferedInputStream(new FileInputStream(Util.PATH_DATA_ENCRYPTED));
        BufferedOutputStream decryptedDataOut = new BufferedOutputStream(new FileOutputStream(Util.PATH_DATA_DECRYPTED));
        byte[] inBytes = new byte[aesCipher.getBlockSize()];
        byte[] outBytes;
        int len;
        while ((len = encryptedDataIn.read(inBytes)) >= 0) {
            outBytes = aesCipher.update(inBytes, 0, len);
            decryptedDataOut.write(outBytes);
        }
        outBytes = aesCipher.doFinal();
        decryptedDataOut.write(outBytes);

        encryptedDataIn.close();
        decryptedDataOut.close();
    }
}

Defect

If key is intercepted puzzle the encrypted data is very easy.

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-26 23:39:08

[security] - AES Encryption的相关文章

[JavaSecurity] - AES Encryption

1. AES Algorithm The Advanced Encryption Standard (AES), also as known as Rijndael (its original name), is a specification for encryption of electronic data established by the U.S. National Institute of Standard and Technology (NIST) in 2001. It uses

aes加密/解密(转载)

这篇文章是转载的康奈尔大学ece5760课程里边的一个final project,讲的比较通俗易懂,所以转载过来.附件里边是工程文件,需要注意一点,在用modelsim仿真过程中会出现错误,提示非法引用memory,网上搜了一下是因为verilog还不支持数组引用,但是system verilog是支持的,而且modelsim也支持system verilog的仿真,那么就需要把.v扩展名改为.sv就可以了. Advanced Encryption Standard (AES), a Feder

AES加密 C++调用Crypto++加密库 例子

这阵子写了一些数据加密的小程序,对比了好几种算法后,选择了AES,高级加密标准(英语:Advanced Encryption Standard,缩写:AES),听这名字就很厉害的样子 估计会搜索到这文章的,对AES算法已经有了些基本了解了吧,下面先简单介绍一下AES加密算法吧 (1)AES在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准.2006年,高级加密标准已然成为对称密钥加密中最流行的算法之一. (2)AES加密数据块分组长度必须为128比特,密钥长度可以是128

Cisco IOS Security command Guide

copy system:running-config nvram:startup-config : to save your configuration changes to the startup configuration so that the changes will not be lost if the software reloads or a power outage occurs command | {begin | include | exclude} regular-expr

关于httpd 2.x,mod_auth_mysql模块的安装配置以及对aes加密的支持

前言 在之前的一篇博文<Apache httpd2.2版本以及2.4版本部分实验>的实验二里面,提到了协议认证使用了mod_auth_mysql.so模块,本文将阐述该模块的安装,配置,以及对于aes加密特性的支持. 基于开发者文档的安装步骤 注:在笔者的CentOS7测试环境下并不支持aes加密 首先从模块提供的官方站点下载mod_auth_mysql-3.0.0.tar.gz,并下载对应的补丁mod_auth_mysql_3.0.0_patch_apache2.4.diff,解压缩之后,将

PHP的AES加密类

aes.php <?php /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  */ /*  AES implementation in PHP (c) Chris Veness 2005-2011. Right of free use is granted for all    */ /*    commercial or non-commercial

C#调用Crypto++库AES ECB加解密

本文章使用上一篇<C#调用C++类库例子>的项目代码作为Demo.本文中,C#将调用C++的Crypto++库,实现AES和ECB加解密. 一.下载Crypto 1.进入Crypto的官网下载openssl.网址是: https://www.cryptopp.com/. 2.点击“DownLoad”,选择最新的可下载的版本即可.此时我下载的是cryptopp820.zip,如下图所示的. 3.解压 cryptopp820.zip. 4.打开cryptopp820文件夹中的cryptest.sl

【干货】国外程序员整理的 C++ 资源大全【转】

来自 https://github.com/fffaraz/awesome-cpp A curated list of awesome C/C++ frameworks, libraries, resources, and shiny things. Inspired by awesome-... stuff Standard Libraries C++ Standard Library - including STL Containers, STL Aglorithm, STL Functio

网上搜集第三方(二)

這是我收集了一些第三方的 Library,大部份都還沒用過Orz,只是這樣整理以後要用到的時候會比較好找到. UI UI Design alertView Menu(選單) Side ViewController UICollectionView Segment Control Others 動畫 影像特效 模糊.毛玻璃效果(Blur) 畫面轉換 各種統計圖表 Line Bar Pie Others Pull To ReFresh Notification Auto Layout Data St