-
-
Save thanhtoan1196/94d9eee003e33f1c8f860659df449c25 to your computer and use it in GitHub Desktop.
AES128 CBC mode in Java, Kotlin and Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.crypto.Cipher; | |
import javax.crypto.spec.IvParameterSpec; | |
import javax.crypto.spec.SecretKeySpec; | |
import java.util.Base64; | |
public class Encryptor { | |
public static String encrypt(String key, String initVector, String value) { | |
try { | |
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING"); | |
IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8")); | |
SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES"); | |
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv); | |
byte[] encrypted = cipher.doFinal(value.getBytes()); | |
String s = new String(Base64.getEncoder().encode(encrypted)); | |
return s; | |
} catch (Exception ex) { | |
ex.printStackTrace(); | |
} | |
return null; | |
} | |
public static String decrypt(String key, String initVector, String encrypted) { | |
try { | |
IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8")); | |
SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES"); | |
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING"); | |
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv); | |
byte[] original = cipher.doFinal(Base64.getDecoder().decode(encrypted)); | |
return new String(original); | |
} catch (Exception ex) { | |
ex.printStackTrace(); | |
} | |
return null; | |
} | |
public static void main(String[] args) { | |
String key = "Bar12345Bar12346"; // 128 bit key | |
String initVector = "RandomInitVector"; // 16 bytes IV | |
System.out.println(encrypt(key, initVector, "Hello World")); | |
System.out.println(decrypt(key, initVector, encrypt(key, initVector, "Hello World"))); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Encryptor.decrypt | |
import Encryptor.encrypt | |
import java.util.* | |
import javax.crypto.Cipher | |
import javax.crypto.spec.SecretKeySpec | |
import javax.crypto.spec.IvParameterSpec | |
object Encryptor { | |
fun encrypt(key: String, initVector: String, value: String): String? { | |
try { | |
val cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING") | |
val iv = IvParameterSpec(initVector.toByteArray(charset("UTF-8"))) | |
val skeySpec = SecretKeySpec(key.toByteArray(charset("UTF-8")), "AES") | |
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv) | |
val encrypted = cipher.doFinal(value.toByteArray()) | |
return String(Base64.getEncoder().encode(encrypted)) | |
} catch (ex: Exception) { | |
ex.printStackTrace() | |
} | |
return null | |
} | |
fun decrypt(key: String, initVector: String, encrypted: String?): String? { | |
try { | |
val iv = IvParameterSpec(initVector.toByteArray(charset("UTF-8"))) | |
val skeySpec = SecretKeySpec(key.toByteArray(charset("UTF-8")), "AES") | |
val cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING") | |
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv) | |
val original = cipher.doFinal(Base64.getDecoder().decode(encrypted)) | |
return String(original) | |
} catch (ex: Exception) { | |
ex.printStackTrace() | |
} | |
return null | |
} | |
} | |
fun main() { | |
val key = "Bar12345Bar12346" // 128 bit key | |
val initVector = "RandomInitVector" // 16 bytes IV | |
println(encrypt(key, initVector, "Hello world!")) | |
println(decrypt(key, initVector, encrypt(key, initVector, "Hello world!"))) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "openssl" | |
require "base64" | |
require 'byebug' | |
include Base64 | |
plain_text = "Hello world!" | |
cipher = OpenSSL::Cipher::AES128.new(:CBC) | |
cipher.encrypt | |
key = "Bar12345Bar12346" | |
iv = "RandomInitVector" | |
cipher.key = key | |
cipher.iv = iv | |
cipher_text = cipher.update(plain_text) + cipher.final | |
cipher = OpenSSL::Cipher::AES128.new(:CBC) | |
cipher.decrypt | |
cipher.key = key | |
cipher.iv = iv | |
decrypted_plain_text = cipher.update(cipher_text) + cipher.final | |
puts "AES128 in CBC mode" | |
#puts "Key: " + urlsafe_encode64(key) | |
#puts "Iv: " + urlsafe_encode64(iv) | |
puts "Plain text: " + plain_text | |
puts "Cipher text: " + urlsafe_encode64(cipher_text) | |
puts "Decrypted plain text: " + decrypted_plain_text |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment