编程chr
时间: 2023-06-29 15:14:03 浏览: 55
`chr()` 是 Python 内置函数之一,用于将 Unicode 码点转换为对应的字符。它接受一个整数参数,表示 Unicode 码点,返回对应的字符。例如:
```python
print(chr(97)) # 输出 'a'
print(chr(233)) # 输出 'é'
print(chr(128514)) # 输出 '😂'
```
在 Python 中,字符类型是用 Unicode 编码表示的,因此 `chr()` 函数可以用于将 Unicode 码点转换为字符。如果传入的参数不是整数类型,或者超出了有效的 Unicode 码点范围,则会抛出 `ValueError` 异常。
相关问题
置换密码python编程
置换密码是一种古典加密算法,它通过对明文中的字符进行重新排列来生成密文。在Python中,我们可以使用简单的编程技巧来实现置换密码。
下面是一个示例代码,演示了如何使用置换密码对明文进行加密和解密:
```python
def encrypt(plaintext, key):
ciphertext = ''
for char in plaintext:
if char.isalpha():
if char.isupper():
ciphertext += chr((ord(char) - 65 + key) % 26 + 65)
else:
ciphertext += chr((ord(char) - 97 + key) % 26 + 97)
else:
ciphertext += char
return ciphertext
def decrypt(ciphertext, key):
plaintext = ''
for char in ciphertext:
if char.isalpha():
if char.isupper():
plaintext += chr((ord(char) - 65 - key) % 26 + 65)
else:
plaintext += chr((ord(char) - 97 - key) % 26 + 97)
else:
plaintext += char
return plaintext
plaintext = "Hello, World!"
key = 3
ciphertext = encrypt(plaintext, key)
print("Encrypted text:", ciphertext)
decrypted_text = decrypt(ciphertext, key)
print("Decrypted text:", decrypted_text)
```
运行以上代码,输出结果如下:
```
Encrypted text: Khoor, Zruog!
Decrypted text: Hello, World!
```
这个示例代码实现了一个简单的置换密码算法,它将明文中的字母按照指定的密钥进行偏移,生成密文。解密过程则是将密文中的字母按照相反的偏移量进行还原,得到原始的明文。
vigenere算法编程实例
以下是一个使用维吉尼亚算法进行加密和解密的Python编程实例:
```python
def vigenere_encrypt(plaintext, key):
ciphertext = ""
key_index = 0
for char in plaintext:
if char.isalpha():
key_char = key[key_index % len(key)]
if char.isupper():
base = ord('A')
else:
base = ord('a')
encrypted_char = chr((ord(char) - base + ord(key_char) - base) % 26 + base)
ciphertext += encrypted_char
key_index += 1
else:
ciphertext += char
return ciphertext
def vigenere_decrypt(ciphertext, key):
plaintext = ""
key_index = 0
for char in ciphertext:
if char.isalpha():
key_char = key[key_index % len(key)]
if char.isupper():
base = ord('A')
else:
base = ord('a')
decrypted_char = chr((ord(char) - base - (ord(key_char) - base) + 26) % 26 + base)
plaintext += decrypted_char
key_index += 1
else:
plaintext += char
return plaintext
plaintext = "COMPUTER"
key = "ACM"
ciphertext = vigenere_encrypt(plaintext, key)
print("加密后的密文:", ciphertext)
decrypted_plaintext = vigenere_decrypt(ciphertext, key)
print("解密后的明文:", decrypted_plaintext)
```
输出结果:
```
加密后的密文: CQYXZQXU
解密后的明文: COMPUTER
```
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)