JAVAはAES 128とAES 256で暗号化

3610 ワード

import org.apache.commons.codec.binary.Hex;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Security;
import java.util.Arrays;


import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
 *
 * @author jacky
 * AES128   
 * CBC   
 * NoPadding      
 * CBC          iv
 *
 *   java    PKCS7Padding,   PKCS5Padding   PKCS7Padding   PKCS5Padding       
 *     java  PKCS7Padding  ,    bouncycastle     
 */
public class AesEncoder {
    /*
     *     Key    26             AES-128-CBC    ,key   16 。
     */
    private byte[] ivParameter = {0x68, 0x7c, 0x39, 0x30, 0x2a, 0x71, 0x51, 0x5e, 0x0f, 0x13, 0x2a, 0x76, 0x4d, 0x76, 0x61, 0x7b};
    public static byte[] cSrc = {0x6c, 0x26, 0x66, 0x4e, 0x23, 0x1c, 0x76, 0x6c, 0x4f, 0x67, 0x37, 0x6d, 0x12, 0x3e};
    private static AesEncoder instance = null;

    private AesEncoder() {

    }

    public static AesEncoder getInstance() {
        if (instance == null)
            instance = new AesEncoder();
        return instance;
    }

    //   
    public byte[] encrypt(byte[]  sSrc, String sKey) throws Exception {
        Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
        byte[] raw = sKey.getBytes();
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        IvParameterSpec iv = new IvParameterSpec(ivParameter);//   CBC  ,      iv,          
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
        return cipher.doFinal(sSrc);
    }

    //   
    public String decrypt(byte[] sSrc, String sKey) throws Exception {
        try {
            byte[] raw = sKey.getBytes("ASCII");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
            IvParameterSpec iv = new IvParameterSpec(ivParameter);
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
            //byte[] encrypted1 = new BASE64Decoder().decodeBuffer(sSrc);//   base64  
            byte[] original = cipher.doFinal(sSrc);
            String originalString = new String(original, "utf-8");
            return originalString;
        } catch (Exception ex) {
            return null;
        }
    }

    public static void main(String[] args) throws Exception {
        //        
        byte[] cSrc = {0x12,0x34,0x6c, 0x26, 0x66, 0x4e, 0x23, 0x1c, 0x76, 0x6c, 0x4f, 0x67, 0x37, 0x6d, 0x12, 0x3e};
        //String cSrc = "123456789";
       /* System.out.print("       :");
        for (byte b : cSrc) {
            System.out.print(b);
        }*/
        System.out.println();
        //   
        byte[] enString = AesEncoder.getInstance().encrypt(cSrc,"12345678901234561234567890123456");
        System.out.println("         :" + enString.length);
        for (byte b : enString) {
            System.out.printf("%02x ",b);
        }
        byte[] bSrc = {0x12,0x34,0x6c, 0x26, 0x66, 0x4e, 0x23, 0x1c, 0x76, 0x6c, 0x4f, 0x67, 0x37, 0x6d, 0x12, 0x3e};
        System.out.println("      :"+Arrays.equals(enString,bSrc));
        String desc = AesEncoder.getInstance().decrypt(enString,"12345678901234561234567890123456");
       System.out.println("       :" + desc);
    }
}

注:128から256に変更鍵長を16から32に変更すればよい