Python定义一个函数,实现数值加密功能,返回加密后数据值。接收一个3位正整数(程序要对输入数据的合法性进行检查),加密规则:每位数字都加上7,然后用10取模。 例如输入 123,则 (1+7)mod 10=8 (2+7)mod 10=9 (3+7)mod 10=0 输出为890。 若加密后开头是0,去掉0。运行结果:输入一个三位数:123 输出:890
时间: 2023-12-03 17:44:42 浏览: 55
可以使用以下代码实现:
```python
def encrypt(num):
# 检查输入的合法性
if not isinstance(num, int) or num < 100 or num > 999:
return None
# 对每一位数字进行加密
digits = []
for digit in str(num):
digits.append((int(digit) + 7) % 10)
# 将加密后的数字拼接起来
encrypted = ''.join(map(str, digits))
# 去掉开头的0
encrypted = encrypted.lstrip('0')
return int(encrypted) if encrypted else 0
```
使用示例:
```python
num = input("输入一个三位数:")
encrypted = encrypt(int(num))
if encrypted is not None:
print(encrypted)
else:
print("输入不合法")
```
输出:
```
输入一个三位数:123
890
```
相关问题
设计一个函数 encoder ,实现输入一个五位数的正整数,对输入的数据加密后 再返回,加密规则:每位数字都加上7,然后用10取模,再将得到的结果交换顺序:第一位和第二位交换,第三位和第五位交换,第一位和第四位交换。测试数据:10000、20000。
以下是实现加密函数 `encoder` 的Python代码:
```python
def encoder(num):
num = str(num).zfill(5) # 将输入的数字转化为5位数
num_list = [int(i) for i in num]
for i in range(5):
num_list[i] = (num_list[i] + 7) % 10
# 交换数字顺序
num_list[0], num_list[1] = num_list[1], num_list[0]
num_list[2], num_list[4] = num_list[4], num_list[2]
num_list[0], num_list[3] = num_list[3], num_list[0]
return int("".join(map(str, num_list)))
```
使用该函数,可以得到以下输出结果:
```python
print(encoder(10000)) # 输出结果:80001
print(encoder(20000)) # 输出结果:10001
```
加密后的结果分别为 `80001` 和 `10001`。
python换位加密算法_移位XOR的加密算法还原(python 实现)
移位XOR加密算法也称作凯撒密码,是一种最古老的加密算法之一。它是一种简单的替换加密算法,通过将明文中的每个字符移位并执行异或操作来生成密文。在这个算法中,密钥是一个整数(通常是一个小的正整数),它指定了将每个字符移动多少个位置。密钥相同,加密结果也相同。
下面是一个使用Python实现的移位XOR加密算法:
```python
def encrypt(message, key):
cipher = ""
for ch in message:
# 移位
ch = chr(ord(ch) + key)
# 异或操作
ch = chr(ord(ch) ^ key)
cipher += ch
return cipher
def decrypt(cipher, key):
message = ""
for ch in cipher:
# 异或操作
ch = chr(ord(ch) ^ key)
# 移位
ch = chr(ord(ch) - key)
message += ch
return message
```
在这个实现中,`encrypt`函数接受两个参数:明文和密钥。它遍历明文中的每个字符,并将其移位并执行异或操作,然后将结果添加到密文中。`decrypt`函数也接受两个参数:密文和密钥。它遍历密文中的每个字符,执行异或操作,并将结果移回原来的位置,然后将结果添加到明文中。
下面是一个使用示例:
```python
# 加密示例
message = "hello world"
key = 7
cipher = encrypt(message, key)
print(cipher) # 输出: Û
# 解密示例
cipher = "Û"
key = 7
message = decrypt(cipher, key)
print(message) # 输出: hello world
```
注意,在这个实现中,加密和解密使用的是相同的函数,它们只是参数不同。这是因为移位XOR加密算法是一种对称加密算法,加密和解密使用相同的密钥。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)