Python,替换密码 [英] Python, substitution cipher

查看:600
本文介绍了Python,替换密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为数字0,1,2,3,...,9替换0,1,2,3,...,9中的每个数字来替代密码。它可以表示为10位字符串,指定0,1,2,3,...,9中的每个数字如何被替换。例如,10位字符串'3941068257'指定一个替换密码,其中数字0被替换为数字3,1,9,2和4,依此类推。加密非负整数,用加密密钥指定的数字代替每个数字。实现功能密码(),其中包含一个10位字符串键和数字和数字字符串(即,要加密的明文),并返回明文的加密。

 >>>加密('3491068257','132')
'914'
>>> encrypt '3491068257','111')
'999'

感谢您的帮助: )

解决方案

  from string import maketrans 

zero_through_nine = .join(str(i)for i in range(10))
out_tab =3491068257
trantab = maketrans(zero_through_nine,out_tab)
print111.translate(trantab)

希望你看看它来理解它....


I need to write a substitution cipher for the digits 0,1,2,3,...,9 substitutes each digit in 0,1,2,3,...,9. It can be represented as a 10 digit string specifying how each digit in 0,1,2,3,...,9 is substituted. For example, the 10 digit string '3941068257' specifies a substitution cipher in which digit 0 is substituted with digit 3, 1 with 9, 2 with 4, and so on. To encrypt a non negative integer, substitute each of it's digits with the digit specified by the encryption key. Implement function cipher() that takes as in put a 10 digit string key and a digit and a digit string (i.e., the clear text to be encrypted) and returns the encryption of the clear text.

>>>encrypt('3491068257', '132')
'914'
>>>encrypt('3491068257', '111')
'999'

Thank you for your help :)

解决方案

from string import maketrans

zero_through_nine = "".join(str(i) for i in range(10))
out_tab = "3491068257"
trantab = maketrans(zero_through_nine,out_tab )
print "111".translate(trantab)

hopefully you look into it to understand it....

这篇关于Python,替换密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆