Scala中此加密和解密代码的等价物是什么?

时间:2015-04-17 08:37:03

标签: java php scala encryption rijndael

$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));
}

修改

我正在寻找确切的实现,而不仅仅是对库的引用,所以我自己发布了答案。我在Scala中编码,但起初我要求Java尽可能快地获得答案的机会,所以实现是用Scala语言实现的。

1 个答案:

答案 0 :(得分:1)

感谢评论,这里的Scala实现与上面的PHP代码完全相同:

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)
}

它使用bouncycastle,可以添加到build.sbt:

libraryDependencies += "org.bouncycastle" % "bcprov-jdk15on" % "1.52"