C# 到 python RC2 加密/解密
寻求一些帮助,将一些代码从 c# 移植到 python。很久以前,编写了一些代码来加密解密数据库中的列。问题是代码是用 C# 编写的,我不熟悉该语言。主要是Python、Scala程序员。
通过阅读代码,我相信该代码使用 rc2 作为其算法。 从这两行中可以看出,
var rc2Csp = new RC2CryptoServiceProvider();
var encryptor = rc2Csp.CreateEncryptor(_Keys[keyOffset % numKeys], _Vs[keyOffset % numIVs]);
我不明白的是密码是如何存储的,有一个键值对,如下所示
(出于安全目的,下面代码片段中的实际值已更改)
private static readonly byte[][] _Keys =
{
new byte[] {0x7a,0xca,0x7a,0xca,0x7a,0xca,0x7a,0xca,0x7a,0xca,0x7a,0xca,0x7a,0xca,0x7a,0xca}
}
private static readonly byte[][] _Vs =
{
new byte[] {0xf3,0x1c,0xf3,0x1c,0xf3,0x1c,0xf3,0x1c}
}
我不确定如何使用python 中的键值对,以便密码库可以解码字符串。
这是我到目前为止不起作用的
from Crypto.Cipher import AES
import Crypto.Cipher.AES
from binascii import hexlify, unhexlify
key = unhexlify(b'0x7a,0xca,0x7a,0xca,0x7a,0xca,0x7a,0xca,0x7a,0xca,0x7a,0xca,0x7a,0xca,0x7a,0xca')
IV = unhexlify('0xf3,0x1c,0xf3,0x1c,0xf3,0x1c,0xf3,0x1c')
plaintext1 = unhexlify('6bc1bee22e409f96e93d7e117393172a')
cipher = AES.new(key,AES.MODE_CBC,IV)
ciphertext = cipher.encrypt(plaintext1)
hexlify(ciphertext)
Looking for some help porting some code from c# to python. A long time ago some code was written to encrypt decrypt a column in our database. The problem is the code was written in c# and i am not familiar with that language. Mostly python, scala programmer.
From reading through the code I believe that the code is using rc2 for their algorithm.
As seen on these 2 lines
var rc2Csp = new RC2CryptoServiceProvider();
var encryptor = rc2Csp.CreateEncryptor(_Keys[keyOffset % numKeys], _Vs[keyOffset % numIVs]);
what I don't understand is how the cypher is being stored there is a key value pair that looks as following
(the actual values in the snippet below have been changed for security purposes)
private static readonly byte[][] _Keys =
{
new byte[] {0x7a,0xca,0x7a,0xca,0x7a,0xca,0x7a,0xca,0x7a,0xca,0x7a,0xca,0x7a,0xca,0x7a,0xca}
}
private static readonly byte[][] _Vs =
{
new byte[] {0xf3,0x1c,0xf3,0x1c,0xf3,0x1c,0xf3,0x1c}
}
Im not sure how to use that key value pair inside python so that the cryptography library can decode the string.
Here is what i have so far that does not work
from Crypto.Cipher import AES
import Crypto.Cipher.AES
from binascii import hexlify, unhexlify
key = unhexlify(b'0x7a,0xca,0x7a,0xca,0x7a,0xca,0x7a,0xca,0x7a,0xca,0x7a,0xca,0x7a,0xca,0x7a,0xca')
IV = unhexlify('0xf3,0x1c,0xf3,0x1c,0xf3,0x1c,0xf3,0x1c')
plaintext1 = unhexlify('6bc1bee22e409f96e93d7e117393172a')
cipher = AES.new(key,AES.MODE_CBC,IV)
ciphertext = cipher.encrypt(plaintext1)
hexlify(ciphertext)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你需要找到支持RC2加密的Python库。
也许你可以尝试一下: https://github.com/0xEBFE /RC2-python/blob/master/rc2.py
创建 RC2 类型的类并将 Key 传递给它。然后调用具有 3 个参数的加密/解密方法:
在 C# 代码中, _Keys 和 _Vs 数组都包含多个字节数组(您只留下了一个,但它们被声明为 byte[][],因此您需要复制获取右键和 IV 的逻辑,
例如,
这里有数字参数
keyOffset
,代码将是。带索引的选择键You need to find Python library which support RC2 encryption.
Perhaps you can try that one: https://github.com/0xEBFE/RC2-python/blob/master/rc2.py
Create class of type RC2 and pass Key to it. Then call encrypt/decrypt methods which have 3 parameters:
In C# code, both _Keys and _Vs arrays contains multiple arrays of bytes (you left only one but they are declared as byte[][], so you need to replicate logic which picks up right key and IV.
For example,
here there is numberic parameter
keyOffset
, code would pick key with index