2015-04-17 47 views
-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)); 
} 

編輯斯卡拉的這個加密和解密代碼的等價物是什麼?

我在尋找確切的實施不只是庫的引用,所以我自己張貼的答案。我在Scala編碼,但起初我要求Java增加儘快獲得答案的機會,因此實現採用Scala語言。

+3

沒有等價物,因爲Sun JCE不提供Rijndael-256。 –

+0

那麼我怎樣才能使用他們的API?他們不能改變他們的實現,他們用.NET編寫應用程序,只是有.NET和PHP的文檔! –

+1

在[這個問題]中的實現(http://stackoverflow.com/questions/15804895/rijndael-256-encryption-java-and-net-do-not-match)似乎接近你所需要的。使用BouncyCastle Rijndael引擎。 –

回答

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

它使用可以被添加到build.sbt BouncyCastle的:

libraryDependencies += "org.bouncycastle" % "bcprov-jdk15on" % "1.52" 
相關問題