-2
$KEY = "Your KEY";
$IV = "Your IV";

function addpadding($string, $blocksize = 32)
{
    $len = strlen($string);
    $pad = $blocksize - ($len % $blocksize);
    $string .= str_repeat(chr($pad), $pad);
    return $string;
}

function strippadding($string)
{
    $slast = ord(substr($string, -1));
    $slastc = chr($slast);
    $pcheck = substr($string, -$slast);
    if(preg_match("/$slastc{".$slast."}/", $string)){
        $string = substr($string, 0, strlen($string)-$slast);
        return $string;
    } else {
        return false;
    }
}

function encrypt($string = "")
{
    global $KEY,$IV;
    $key = base64_decode($KEY);
    $iv = base64_decode($IV);
    return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key,  addpadding($string), MCRYPT_MODE_CBC, $iv));
}

function decrypt($string = "")
{
    global $KEY,$IV;
    $key = base64_decode($KEY);
    $iv = base64_decode($IV);
    $string = base64_decode($string);
    return strippadding(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $string, MCRYPT_MODE_CBC, $iv));
}

EDIT

I'm looking for the exact implementation not just references to libraries so I posted the answer by myself. I'm coding in Scala but at first I asked for Java to increase the chance of getting an answer as quickly as possible, so the implementation is in Scala language.

Amir Karimi
  • 5,401
  • 4
  • 32
  • 51
  • 3
    There is no equivalent, because Sun JCE doesn't provide Rijndael-256. – Artjom B. Apr 17 '15 at 08:38
  • So how could I use their API? they can't change their implementation and they wrote they application with .NET and just have documentation for .NET and PHP! – Amir Karimi Apr 17 '15 at 08:39
  • 1
    Implementation in [this question](http://stackoverflow.com/questions/15804895/rijndael-256-encryption-java-and-net-do-not-match) seems to be close to what you need. Uses the BouncyCastle Rijndael engine. – Robby Cornelissen Apr 17 '15 at 08:42
  • 1
    possible duplicate of [How to encrypt or decrypt with Rijndael and a block-size of 256 bits?](http://stackoverflow.com/questions/8083144/how-to-encrypt-or-decrypt-with-rijndael-and-a-block-size-of-256-bits) – Artjom B. Apr 17 '15 at 08:44
  • Please don't close this question as it contains the direct answer which helps developers to get the implementation as fast as possible. – Amir Karimi Apr 17 '15 at 14:05

1 Answers1

1

Thanks to the comments, here is the Scala implementation which works exactly the same as the above PHP code:

import org.apache.commons.codec.binary.Base64
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher
import org.bouncycastle.crypto.modes.CBCBlockCipher
import org.bouncycastle.crypto.engines.RijndaelEngine
import org.bouncycastle.crypto.paddings.PKCS7Padding
import org.bouncycastle.crypto.params._

class EncryptionUtil(keyBase64: String, ivBase64: String) {
  private val keyBytes = Base64.decodeBase64(keyBase64)
  private val ivBytes = Base64.decodeBase64(ivBase64)

  def encrypt(message: String): String = {
    val cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new RijndaelEngine(256)), new PKCS7Padding());
    val keySize = keyBytes.length;
    val ivAndKey = new ParametersWithIV(new KeyParameter(keyBytes, 0, keySize), ivBytes, 0, keySize);
    cipher.init(true, ivAndKey);
    val messageBytes = message.getBytes("UTF-8")
    val encrypted  = new Array[Byte](cipher.getOutputSize(messageBytes.length));
    val oLen = cipher.processBytes(messageBytes, 0, messageBytes.length, encrypted, 0);
    cipher.doFinal(encrypted, oLen);
    Base64.encodeBase64String(encrypted)
  }

  def decrypt(inputBase64: String): String = {
    val cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new RijndaelEngine(256)), new PKCS7Padding());
    val keySize = keyBytes.length;
    val ivAndKey = new ParametersWithIV(new KeyParameter(keyBytes, 0, keySize), ivBytes, 0, keySize);
    cipher.init(false, ivAndKey);
    val messageBytes = Base64.decodeBase64(inputBase64)
    val decrypted  = new Array[Byte](cipher.getOutputSize(messageBytes.length));
    val oLen = cipher.processBytes(messageBytes, 0, messageBytes.length, decrypted, 0);
    cipher.doFinal(decrypted, oLen);

    val zeroTerminationIndex = decrypted.indexOf(0)
    new String(decrypted, 0, zeroTerminationIndex, "UTF-8")
  }
}

object EncryptionUtil {
  def apply(keyBase64: String, ivBase64: String) = new EncryptionUtil(keyBase64, ivBase64)
}

It uses bouncycastle which can be added to build.sbt:

libraryDependencies += "org.bouncycastle" % "bcprov-jdk15on" % "1.52"
Amir Karimi
  • 5,401
  • 4
  • 32
  • 51