Java 在Scala中,这种加密和解密代码的等价物是什么?
编辑Java 在Scala中,这种加密和解密代码的等价物是什么?,java,php,scala,encryption,rijndael,Java,Php,Scala,Encryption,Rijndael,编辑 我正在寻找确切的实现,而不仅仅是对库的引用,所以我自己发布了答案。我正在用Scala编写代码,但首先我要求使用Java以增加尽快得到答案的机会,因此实现是用Scala语言进行的。感谢这些评论,下面是Scala实现,其工作原理与上述PHP代码完全相同: $KEY = "Your KEY"; $IV = "Your IV"; function addpadding($string, $blocksize = 32) { $len = strlen($string); $pad
我正在寻找确切的实现,而不仅仅是对库的引用,所以我自己发布了答案。我正在用Scala编写代码,但首先我要求使用Java以增加尽快得到答案的机会,因此实现是用Scala语言进行的。感谢这些评论,下面是Scala实现,其工作原理与上述PHP代码完全相同:
$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));
}
它使用可添加到build.sbt的bouncycastle:
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)
}
因为Sun JCE不提供Rijndael-256,所以没有等效的API。那么我如何使用他们的API呢?他们不能改变他们的实现,他们用.NET编写应用程序,只有.NET和PHP的文档!中的实现似乎接近您所需要的。使用BouncyCastle Rijndael引擎。可能重复的请不要关闭此问题,因为它包含帮助开发人员尽快实现的直接答案。
libraryDependencies += "org.bouncycastle" % "bcprov-jdk15on" % "1.52"