【问题标题】:How to use AES Encryption and Decryption from Scala to Python如何使用 AES 加密和解密从 Scala 到 Python
【发布时间】:2020-07-13 09:55:19
【问题描述】:

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

密码:字符串 = #test@12345
结果:字符串 = lHhq1OzMSYnj+0XxiNzKhQ==

val pwd1  = encryptobj.decrypt(result)
println(pwd1)

pwd1: 字符串 = #test@12345
#test@12345

我尝试在 Python 中实现相同但没有给出预期的加密结果,这是我的代码(我在其他类似答案的帮助下):

from hashlib import sha256
import base64
from Crypto import Random
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( iv + cipher.encrypt( raw ) )

    def decrypt( self, enc ):
        enc = base64.b64decode(enc)
        iv = enc[:16]
        cipher = AES.new(self.key, AES.MODE_CBC, iv )
        return unpad(cipher.decrypt( enc[16:] )).decode('utf8')

    cipher = AESCipher('enIntVecTest2020')
    encrypted = cipher.encrypt('#test@12345')
    decrypted = cipher.decrypt(encrypted)
    
    print(encrypted)

b'ZW5jcnlwdGlvbkludFZlY5R4atTszEmJ4/tF8YjcyoU='

正如您所见,加密都不正确,我不知道我在哪里做错了。请帮助在 python 中实现与在 scala 中显示的相同的加密结果,我将不胜感激。

【问题讨论】:

  • Python代码中,IV和密文在加密时连接,解密时分离,Scala代码中缺少连接和分离。因此密文不同。 Python 代码的设计与通行做法相对应,但不是使用固定的 IV,而是使用每个加密随机生成的 IV。
  • 哦,我的错,这是一个愚蠢的错误。谢谢@Topaco 让我知道。

标签: python scala aes pycrypto cbc-mode


【解决方案1】:

感谢@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

【讨论】:

    猜你喜欢
    • 2018-10-03
    • 2018-09-07
    • 1970-01-01
    • 1970-01-01
    • 2015-01-20
    • 2014-01-05
    • 2019-01-06
    • 1970-01-01
    • 2019-11-04
    相关资源
    最近更新 更多