Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用从Scala到Python的AES加密和解密_Python_Scala_Aes_Pycrypto_Cbc Mode - Fatal编程技术网

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

如何使用从Scala到Python的AES加密和解密,python,scala,aes,pycrypto,cbc-mode,Python,Scala,Aes,Pycrypto,Cbc Mode,我在scala中有一个代码,其中有我的加密和解密代码,工作正常,代码如下: import java.util.Base64 import javax.crypto.Cipher import javax.crypto.spec.{IvParameterSpec, SecretKeySpec} class Encryption { val key = "enIntVecTest2020" val initVector = "encryptionIntVec"

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

val encryptobj = new Encryption()
val pwd = "#test@12345"
val result =encryptobj.encrypt(pwd)
pwd:字符串=#test@12345
结果:String=lHhq1OzMSYnj+0XxiNzKhQ==

val pwd1  = encryptobj.decrypt(result)
println(pwd1)
pwd1:字符串=#test@12345
#test@12345

我尝试用Python实现同样的效果,但没有给出预期的加密结果,下面是我的代码(我得到了其他类似答案的帮助):

b'ZW5jcnlwdGlvbkludFZlY5R4atTszEmJ4/tF8YjcyoU='10〕


正如你所看到的,这两种加密都不对,我不知道我哪里做错了。请帮助我在python中获得与scala中相同的加密结果,我将非常感谢。

感谢@Topaco answer,经过一些搜索,它成功了

import base64
from Crypto.Cipher import AES

BS = 16
pad = lambda s: bytes(s + (BS - len(s) % BS) * chr(BS - len(s) % BS), 'utf-8')
unpad = lambda s : s[0:-ord(s[-1:])]
class AESCipher:

    def __init__( self, key ):
        self.key = bytes(key, 'utf-8')

    def encrypt( self, raw ):
        raw = pad(raw)
        iv = "encryptionIntVec".encode('utf-8')
        cipher = AES.new(self.key, AES.MODE_CBC, iv )
        return base64.b64encode(cipher.encrypt( raw ) )
    def decrypt( self, enc ):
        iv = "encryptionIntVec".encode('utf-8')
        enc = base64.b64decode(enc)
        cipher = AES.new(self.key, AES.MODE_CBC, iv )
        return unpad(cipher.decrypt( enc )).decode('utf8')
cipher = AESCipher('enIntVecTest2020')
encrypted = cipher.encrypt('#test@12345')
print(encrypted.decode('utf-8'))
-> lHhq1OzMSYnj+0XxiNzKhQ==
decrypted = cipher.decrypt(encrypted)
print(decrypted)
-> #test@12345

在Python代码中,IV和密文在加密期间串联,在解密期间分离,而在Scala代码中,串联和分离缺失。因此密文不同。Python代码的设计符合常见的做法,但是没有使用固定的IV,而是使用随机生成的每个加密的IV。谢谢@Topaco让我知道。太好了,它适合我!
import base64
from Crypto.Cipher import AES

BS = 16
pad = lambda s: bytes(s + (BS - len(s) % BS) * chr(BS - len(s) % BS), 'utf-8')
unpad = lambda s : s[0:-ord(s[-1:])]
class AESCipher:

    def __init__( self, key ):
        self.key = bytes(key, 'utf-8')

    def encrypt( self, raw ):
        raw = pad(raw)
        iv = "encryptionIntVec".encode('utf-8')
        cipher = AES.new(self.key, AES.MODE_CBC, iv )
        return base64.b64encode(cipher.encrypt( raw ) )
    def decrypt( self, enc ):
        iv = "encryptionIntVec".encode('utf-8')
        enc = base64.b64decode(enc)
        cipher = AES.new(self.key, AES.MODE_CBC, iv )
        return unpad(cipher.decrypt( enc )).decode('utf8')
cipher = AESCipher('enIntVecTest2020')
encrypted = cipher.encrypt('#test@12345')
print(encrypted.decode('utf-8'))
-> lHhq1OzMSYnj+0XxiNzKhQ==
decrypted = cipher.decrypt(encrypted)
print(decrypted)
-> #test@12345