小编Moh*_*wan的帖子

如何从 Scala 到 Python 使用 AES 加密和解密

我在 scala 中有一段代码,其中有我的加密和解密代码,它工作正常,代码是:

import java.util.Base64
import javax.crypto.Cipher
import javax.crypto.spec.{IvParameterSpec, SecretKeySpec}
class Encryption {
val key = "enIntVecTest2020"
val initVector = "encryptionIntVec"

def encrypt(text: String): String = {

val  iv = new IvParameterSpec(initVector.getBytes("UTF-8"))
val  skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES")

val  cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING")
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv)

val  encrypted = cipher.doFinal(text.getBytes())
return Base64.getEncoder().encodeToString(encrypted)
}

def decrypt(text:String) :String={
val  iv = new IvParameterSpec(initVector.getBytes("UTF-8"))
val  skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES")

val  cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING")
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv)
val  original = cipher.doFinal(Base64.getDecoder.decode(text))

new String(original)
} …
Run Code Online (Sandbox Code Playgroud)

python scala aes pycrypto cbc-mode

4
推荐指数
1
解决办法
6776
查看次数

标签 统计

aes ×1

cbc-mode ×1

pycrypto ×1

python ×1

scala ×1