JAVA AES256

programming/JAVA_JSP 2017. 11. 22. 15:05
반응형

 

 

public static byte ivBytes = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }

암호화 함수

public static String encrypt(String plainText) throws Exception
{
    byte convKeyBytes = Hex.decodeHex("{AES KEY}".toCharArray())
    byte convTextBytes = plainText.getBytes("UTF8")

    AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes)
    SecretKeySpec newKey = new SecretKeySpec(convKeyBytes, "AES")

    Cipher cipher = null
    cipher = Cipher.getInstance("AESCBCPKCS5Padding")
    cipher.init(Cipher.ENCRYPT_MODE, newKey, ivSpec)

    String encodeing = new String(Base64.encodeBase64(cipher.doFinal(convTextBytes), false))

    return encodeing
}

복호화 함수

public static String decrypt(String encryptedText) throws Exception
{
    byte convKeyBytes = Hex.decodeHex("{AES KEY}".toCharArray())
    byte convTextBytes = Base64.decodeBase64(encryptedText)

    AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes)
    SecretKeySpec newKey = new SecretKeySpec(convKeyBytes, "AES")

    Cipher cipher = Cipher.getInstance("AESCBCPKCS5Padding")
    cipher.init(Cipher.DECRYPT_MODE, newKey, ivSpec)

    return new String(cipher.doFinal(convTextBytes), "UTF8")
}

 

 

 

반응형
Posted by 공간사랑
,