Skip to content

Instantly share code, notes, and snippets.

@pauca
Created March 15, 2018 14:42
Show Gist options
  • Save pauca/24a194c867e11f1730bd4f172da97a0d to your computer and use it in GitHub Desktop.
Save pauca/24a194c867e11f1730bd4f172da97a0d to your computer and use it in GitHub Desktop.
scala php crypto ciper match
import javax.crypto._
import javax.crypto.spec._
import java.util.Base64
val message= "Hello"
val secret = "eGkmaYd3PE9x3Z221x7b2nXmJ7ACGrjf"
val ivString = "0123456789012345"
val ciper: Cipher = Cipher.getInstance("AES/CBC/PKCS5Padding")
val key: SecretKeySpec = new SecretKeySpec(secret.getBytes("UTF-8"), "AES")
val iv: IvParameterSpec = new IvParameterSpec( ivString.getBytes("UTF-8"), 0, ciper.getBlockSize)
//Encrypt
ciper.init(Cipher.ENCRYPT_MODE, key, iv)
val encryptedCiperBytes: Array[Byte] = Base64.getEncoder.encode((ciper.doFinal(message.getBytes)))
val cipermessage = new String(encryptedCiperBytes)
//Decrypt
ciper.init(Cipher.DECRYPT_MODE, key, iv)
val text: Array[Byte] = ciper.doFinal(Base64.getDecoder.decode(cipermessage.getBytes))
new String(text)
$data ="Hello"
$algorithm ="aes-256-cbc"
$secret = "eGkmaYd3PE9x3Z221x7b2nXmJ7ACGrjf";
$iv = '0123456789012345';
//Encrypt
$cipherText = openssl_encrypt(
$data,
$algorithm,
$secret,
OPENSSL_RAW_DATA,
$iv
);
$cipherText = base64_encode($cipherText);
$cipherText = base64_decode($cipherText);
//Decrypt
$plaintext = openssl_decrypt(
$cipherText,
$algorithm,
$secret,
OPENSSL_RAW_DATA,
$iv
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment