AES-128 javaによる復号化


AESの紹介
AES:高度な暗号化規格(Advanced Encryption Standard)は、一般的な対称復号化技術であり、暗号化と復号化は同じ鍵を使用するため、この鍵は保存され、漏らさないようにしなければならない.一般に、呼び出し元とオフラインで固定された鍵列を約束したり、サーバ側がRSAでAESの鍵を暗号化してフロントエンドに戻して使用したりし、フロントエンドに共通のRSA公開鍵を施してAESを取得した鍵を復号する.
AESの原理
要求発起人は,鍵key+明文contentにより暗号化関数を用いて密文secreContentを暗号化し,密文を受信者に送信し,受信者は鍵keyと密文転送復号関数で元の明文contentを復号化する.大まかな流れは以下の通りです.
次に、AES-128に基づいて実装されるjava AES復号化、上コードを示す.
package com.lsk.util;

import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.UUID;

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

public class AesUtil {
     
	private final static String AES = "AES";
	private final static String UTF8 = "UTF-8";
	//    16byte     
	private static final String IV_STRING = "Linsk110011ksniL";
	/**
	 * 	    16       
	 * @return
	 */
	public static String generateSecreKey() {
     
		String uuid = UUID.randomUUID().toString();
		uuid = uuid.replaceAll("-", "");
		return uuid.substring(0, 16);
	}
	public static String aesEncry(String content,String key) throws UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException {
     
		byte[] contentByte = content.getBytes(UTF8);
		byte[] keyByte = key.getBytes();
		//         
		SecretKeySpec keySpec = new SecretKeySpec(keyByte ,AES);
		//         ,     ,     0     
		byte[] initParam = IV_STRING.getBytes();
		IvParameterSpec ivSpec = new IvParameterSpec(initParam);
		//        、         
		Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
		cipher.init(Cipher.ENCRYPT_MODE, keySpec,ivSpec);
		byte[] encryptedBytes = cipher.doFinal(contentByte);
		String encodedString = Base64.getEncoder().encodeToString(encryptedBytes);
		return encodedString;
	}
	
	public static String aesDecry(String content,String key) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException, InvalidAlgorithmParameterException {
     
		byte[] contentByte = Base64.getDecoder().decode(content);
		byte[] keyByte = key.getBytes();
		//         
		SecretKeySpec keySpec = new SecretKeySpec(keyByte ,AES);
		//         ,     ,     0     
		byte[] initParam = IV_STRING.getBytes();
		IvParameterSpec ivSpec = new IvParameterSpec(initParam);
		//        、         
		Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
		cipher.init(Cipher.DECRYPT_MODE, keySpec,ivSpec);
		byte[] result  = cipher.doFinal(contentByte);
		return new String(result,UTF8);
	}

	public static void main(String[] args) throws Exception{
     
		// TODO Auto-generated method stub
		String secrekey = generateSecreKey();
		System.out.println("secrekey="+secrekey);
		String sourceStr = "Linsk123456";
		String secreStr = aesEncry(sourceStr,secrekey);
		System.out.println("secreStr="+secreStr);
		String decodeStr = aesDecry(secreStr,secrekey);
		System.out.println("decodeStr="+decodeStr);
	}
}


出力結果:secrekey=2983 ce 249 a 2 a 4 e 5 b secreStr=cqRG 3 WhpsGbV 7 oOT 8 wSnfw==decodeStr=Linsk 123456