AES暗号解読のJAVA実現【二】
使用環境JDK 1.8
この方法の鍵は16ビットでなくてもよく、オフセット量は16ビットである.
この方法の鍵は16ビットでなくてもよく、オフセット量は16ビットである.
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.Security;
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.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
* AES / [ 128 ]
* main , iv key
* : AES , 16 , 16 ; 16 , ;
* , 128 , , , 。
*
*
*/
public class AES {
public static String key = "ThisIsMyTestKey12345";
public static String iv = "1234567890123456";
/**
*
* @return
* @throws UnsupportedEncodingException
*/
private static IvParameterSpec getIV() throws UnsupportedEncodingException {
return new IvParameterSpec(iv.getBytes("UTF-8"));
}
/**
*
* @return
* @throws UnsupportedEncodingException
* @throws NoSuchAlgorithmException
*/
public static SecretKeySpec getKey() throws UnsupportedEncodingException, NoSuchAlgorithmException {
Security.addProvider(new BouncyCastleProvider());
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128, new SecureRandom(key.getBytes()));
SecretKey keygen = kgen.generateKey();
SecretKeySpec keySpec = new SecretKeySpec(keygen.getEncoded(), "AES");
return keySpec;
}
/**
*
*
* @param content
* @return
*/
public static String decrypt(String content) {
try {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, getKey(), getIV());
byte[] decodedContent = new BASE64Decoder().decodeBuffer(content);
byte[] original = cipher.doFinal(decodedContent);
return new String(original);
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
;
return null;
}
/**
*
*
* @param content
* @return
*/
public static String encrypt(String content) {
try {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, getKey(), getIV());
byte[] contentByte = content.getBytes("UTF-8");
byte[] result = cipher.doFinal(contentByte);
return new BASE64Encoder().encode(result);
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
};
return null;
}
/**
*
* @param args
* @throws UnsupportedEncodingException
*/
public static void main(String[] args) throws UnsupportedEncodingException {
String content = " , ";
AES.key +="ThisIsMyTestKey12345ThisIsMyTestKey12345ThisIsMyTestKey12345ThisIsMyTestKey12345ThisIsMyTestKey12345ThisIsMyTestKey12345";
//
System.out.println(" :" + content);
String resultString = AES.encrypt(content);
System.out.println(" :" + resultString);
//
String decryptResult = AES.decrypt(resultString);
System.out.println(" :" + decryptResult);
}
}