[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 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:12

[JavaSecurity] - AES Encryption的相关文章

[JavaSecurity] - RSA Encryption

1. RSA Algorithm RSA is one of the first practical public-key cryptosystems and is widely used for secure data transmission. In such a cryptosystem, the encryption key (public key) is public and differs from the decryption key (private key) which is

[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

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

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

关于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

aes加密/解密(转载)

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

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

自己总结的 iOS ,Mac 开源项目以及库,知识点------持续更新

自己在 git  上看到一个非常好的总结的东西,但是呢, fork  了几次,就是 fork  不到我的 git 上,干脆复制进去,但是,也是认真去每一个每一个去认真看了,并且也是补充了一些,感觉非常棒,所以好东西要分享,为啥用 CN 博客,有个好处,可以随时修改,可以持续更新,不用每次都要再发表,感觉这样棒棒的 我们 自己总结的iOS.mac开源项目及库,持续更新.... github排名 https://github.com/trending,github搜索:https://github.

Go语言(golang)开源项目大全

转http://www.open-open.com/lib/view/open1396063913278.html内容目录Astronomy构建工具缓存云计算命令行选项解析器命令行工具压缩配置文件解析器控制台用户界面加密数据处理数据结构数据库和存储开发工具分布式/网格计算文档编辑器Encodings and Character SetsGamesGISGo ImplementationsGraphics and AudioGUIs and Widget ToolkitsHardwareLangu