Rijndael用Python填充

时间:2019-06-02 17:12:24

标签: python encryption aes padding rijndael

我知道问题出在哪里,但我不知道如何解决。问题在于填充。我完全不知道它以及它如何工作。我尝试在线搜索,但似乎无济于事。我正在尝试实现此功能以与我的website一起使用。 python加密并发送数据,PHP解密。

这是我的python的实际代码:

from rijndael.cipher.crypt import new
from rijndael.cipher.blockcipher import MODE_CBC
import base64

PADDING = b'.'

def r_pad(payload, block_size=32):
    return payload + (block_size - len(payload) % block_size) * PADDING

KEY = 'lkirwf897+22#bbtrm8814z5qq=498j5'
IV = '741952hheeyy66#cs!9hjv887mxx7@8y'
plain_text = "A padded string to BLOCKSIZE length."

rjn = new(KEY, MODE_CBC, IV, blocksize=32)
encd = rjn.encrypt(r_pad(plain_text))
data = base64.b64encode(encd)
print(data)

rjn = new(KEY, MODE_CBC, IV, blocksize=32)
data = base64.b64decode(data)
decd = rjn.decrypt(r_pad(data))
print (decd)

这是输出:

  

Dv0Y / AFXdFMlDrcldFCu8v5o9zAlLNgyM + vO + PFeSrqWdzP1S1cumviFiEjNAjz5njnMMC9lfxsBl71x5y + xCw ==

     

一个填充字符串,长度为BLOCKSIZE。.....................................Å¿:è°⌐┘n┤«╞Px ╜:æC┬♣╬Q┤▼«U_♦â☻ìr

我需要加密的字符串的输出是这样的:

  

Dv0Y / AFXdFMlDrcldFCu8v5o9zAlLNgyM + vO + PFeSrpO8Ve82mdUcc4rkzp9afDYc75NmkSd4mdflt38kceOdA ==

     

填充字符串到BLOCKSIZE长度

我试图通过此代码使RIJNDAEL256功能:

EncryptRJ256("lkirwf897+22#bbtrm8814z5qq=498j5", "741952hheeyy66#cs!9hjv887mxx7@8y", "A padded string to BLOCKSIZE length.")

Public Function EncryptRJ256(ByVal prm_key As String, ByVal prm_iv As String, ByVal prm_text_to_encrypt As String) As String
    Dim s As String = prm_text_to_encrypt
    Dim managed2 As New RijndaelManaged With {
    .Padding = PaddingMode.Zeros,
    .Mode = CipherMode.CBC,
    .BlockSize = 256
}
    Dim stream As New MemoryStream
    Dim stream2 As New CryptoStream(stream, managed2.CreateEncryptor(Encoding.ASCII.GetBytes(prm_key), Encoding.ASCII.GetBytes(prm_iv)), CryptoStreamMode.Write)
    Dim bytes As Byte() = Encoding.ASCII.GetBytes(s)
    stream2.Write(bytes, 0, bytes.Length)
    stream2.FlushFinalBlock()
    Return Convert.ToBase64String(stream.ToArray)
End Function

有人可以帮忙吗?此时我迷路了。 :/

0 个答案:

没有答案