小编she*_*ydz的帖子

PBKDF2 Python密钥与.NET Rfc2898

我正在尝试编写一个Python模块,它将加密我们现有.NET类可以解密的文本.据我所知,我的代码行,但它没有解密(我在C#端得到'无效的填充长度'错误).我的pkcs7代码看起来不错,但研究表明无效密钥可能会导致同样的问题.

这两种设置有什么不同?蟒蛇:

derived_key = PBKDF2(crm_key, salt, 256 / 8, iterations)
iv = PBKDF2(crm_key, salt, 128 / 8, iterations)

encoder = pkcs7.PKCS7Encoder()

cipher = AES.new(derived_key, AES.MODE_CBC, iv)
decoded = cipher.decrypt(encoded_secret)

#encode - just stepped so i could debug. 
padded_secret = encoder.encode(secret)              # 1
encodedtext = cipher.encrypt(padded_secret)         # 2
based_secret = base64.b64encode(encodedtext)        # 3
Run Code Online (Sandbox Code Playgroud)

我认为based_secret可以传递给C#并在那里解码.但它失败了.相同的加密c#代码是:

var rfc = new Rfc2898DeriveBytes(key, saltBytes);


        // create provider & encryptor
        using (var cryptoProvider = new AesManaged())
        {
            // Set cryptoProvider parameters
            cryptoProvider.BlockSize = cryptoProvider.LegalBlockSizes[0].MaxSize;
            cryptoProvider.KeySize = …
Run Code Online (Sandbox Code Playgroud)

c# python encryption aes pbkdf2

8
推荐指数
1
解决办法
2743
查看次数

Python从django url参数解析int

我想在我的django应用程序中解析传入URL参数的日期.我提出了:

def month_transactions(request, month, year):
    current_month = calculate_current_month(month, year)
    next_month = calculate_next_month(current_month)
    debit_transactions = Transaction.objects.filter(is_credit=False,
                                                    due_date__range=(current_month, next_month))
    credit_transactions = Transaction.objects.filter(is_credit=True,
                                                     due_date__range=(current_month, next_month))
    return render(request, 'finances/index.html', {
        'debits': debit_transactions,
        'credits': credit_transactions,
    })
def calculate_current_month(month, year):
    current_month = re.match('\d{2}', month)
    current_year = re.match('\d{4}', year)
    return_month = datetime.date(
        int(current_year.group()), int(current_month.group()), 1)
    return return_month
Run Code Online (Sandbox Code Playgroud)

我的URL.conf看起来像:

url(r'^transactions/(?P<month>\d{2})/(?P<year>\d{4}/$)', views.month_transactions, name='index',),
Run Code Online (Sandbox Code Playgroud)

由于'month'和'year'作为unicode字符串进入month_transactions(年份带有尾随/),因此在从原始变量创建新日期时,我不断获得Type异常.

有没有更好的方法; 我错过了内置于Python或Django中的东西?

谢谢,

python django

3
推荐指数
1
解决办法
6346
查看次数

Python嵌套字典理解返回空字典

我正在使用字典理解来提取嵌套值.我有以下代码(作为示例):

d = {1: 'a', 2: 'b', 3: 'c'}
e = {4: 'd', 5: 'e', 6: 'f'}
f = {7: 'g', 8: 'h', 9: 'i'}

# stick them together into another dict
data_dict = {'one': d, 'two': e, 'three': f}
#say we're looking for the key 8

output = {outer_key: {inner_key: inner_value for inner_key, inner_value in outer_value.items() if inner_key == 8} for outer_key, outer_value in data_dict.items()}
output = {'one': {}, 'three': {8: 'h'}, 'two': {}}
Run Code Online (Sandbox Code Playgroud)

我需要做些什么来获得JUST'三'及其价值?我不想返回空的比赛.

谢谢

python dictionary list-comprehension

3
推荐指数
1
解决办法
1051
查看次数

标签 统计

python ×3

aes ×1

c# ×1

dictionary ×1

django ×1

encryption ×1

list-comprehension ×1

pbkdf2 ×1