[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.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值