C# 到 python RC2 加密/解密

发布于 2025-01-11 08:43:26 字数 1209 浏览 0 评论 0原文

寻求一些帮助,将一些代码从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

惯饮孤独 2025-01-18 08:43:26

你需要找到支持RC2加密的Python库。
也许你可以尝试一下: https://github.com/0xEBFE /RC2-python/blob/master/rc2.py

创建 RC2 类型的类并将 Key 传递给它。然后调用具有 3 个参数的加密/解密方法:

  • input_buffer - 要加密/解密
  • 模式的数据 - MODE_CBC 或来自 C# 代码的任何其他
  • IV - IV

在 C# 代码中, _Keys 和 _Vs 数组都包含多个字节数组(您只留下了一个,但它们被声明为 byte[][],因此您需要复制获取右键和 IV 的逻辑,

例如,

_Keys[keyOffset % numKeys]

这里有数字参数 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:

  • input_buffer - data to encrypt/decrypt
  • mode - MODE_CBC or any other
  • IV - IV from C# code

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,

_Keys[keyOffset % numKeys]

here there is numberic parameter keyOffset, code would pick key with index

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文