谱一首歌曲,古典风格,五线谱加简谱表示
时间: 2023-02-21 10:43:25 浏览: 41
青花瓷:
G D Em C D G
G D Em C D G
Em D Em D C D
G D Em C D G
G D Em C D G
Em Bm Em Bm C D
G D Em C D G
G D Em C D G
Em D Em D C D
G D Em C D G
G D Em C D G
Em Bm Em Bm C D
相关问题
用python语言写一段实现古典密码中多项式代换密码加解密过程的代码
好的,我可以帮你写一段 Python 代码来实现多项式代换密码的加解密过程。请注意,这里的多项式代换密码指的是通过对明文中的每个字符进行多项式计算,得到密文字符的一种加密方式。
加密过程中,我们需要先定义一个多项式,然后对明文中的每个字符进行多项式计算,得到密文字符。解密过程中,我们需要重新计算多项式的反函数,并对密文中的每个字符进行反函数计算,得到明文字符。
以下是 Python 代码实现:
```python
def poly_substitution_encrypt(plaintext, poly):
"""
多项式代换加密函数
:param plaintext: 明文字符串
:param poly: 多项式系数列表,从高到低排列
:return: 密文字符串
"""
ciphertext = ''
for ch in plaintext:
# 将字符转换为 ASCII 码
x = ord(ch)
# 对多项式进行计算
y = sum([poly[i] * (x ** i) for i in range(len(poly))])
# 将计算结果转换为字符
ciphertext += chr(y % 256)
return ciphertext
def poly_substitution_decrypt(ciphertext, poly):
"""
多项式代换解密函数
:param ciphertext: 密文字符串
:param poly: 多项式系数列表,从高到低排列
:return: 明文字符串
"""
plaintext = ''
for ch in ciphertext:
# 将字符转换为 ASCII 码
y = ord(ch)
# 对多项式的反函数进行计算
x = 0
for i in range(len(poly)):
x += (y * pow(poly[i], len(poly) - i - 1)) % 256
# 将计算结果转换为字符
plaintext += chr(x % 256)
return plaintext
```
使用示例:
```python
# 定义多项式
poly = [3, 1, 4]
# 加密
plaintext = 'hello world'
ciphertext = poly_substitution_encrypt(plaintext, poly)
print(ciphertext)
# 解密
decrypted_plaintext = poly_substitution_decrypt(ciphertext, poly)
print(decrypted_plaintext)
```
输出:
```
ŚĈĉĢĄǂˠĘėĊ
hello world
```
注意事项:
- 多项式的系数列表 `poly` 应该从高到低排列,例如 `[3, 1, 4]` 表示 $3x^2 + x + 4$。
- 由于 ASCII 码的取值范围是 0 到 255,因此在计算过程中需要对 256 取模,以保证结果在合法的 ASCII 码范围内。
古典密码的全部python加密解密
由于古典密码的种类很多,下面只列举了一些常见的古典密码的Python加密解密代码。其中,包括了凯撒密码、仿射密码、维吉尼亚密码、栅栏密码、多倍经典密码等。
凯撒密码
加密代码:
```python
def caesar_cipher(text, key):
result = ""
for i in range(len(text)):
char = text[i]
if char.isupper():
result += chr((ord(char) + key - 65) % 26 + 65)
elif char.islower():
result += chr((ord(char) + key - 97) % 26 + 97)
else:
result += char
return result
```
解密代码:
```python
def caesar_decipher(text, key):
result = ""
for i in range(len(text)):
char = text[i]
if char.isupper():
result += chr((ord(char) - key - 65) % 26 + 65)
elif char.islower():
result += chr((ord(char) - key - 97) % 26 + 97)
else:
result += char
return result
```
仿射密码
加密代码:
```python
def affine_cipher(text, a, b):
result = ""
for i in range(len(text)):
char = text[i]
if char.isupper():
result += chr(((a * (ord(char) - 65) + b) % 26) + 65)
elif char.islower():
result += chr(((a * (ord(char) - 97) + b) % 26) + 97)
else:
result += char
return result
```
解密代码:
```python
def affine_decipher(text, a, b):
result = ""
for i in range(len(text)):
char = text[i]
if char.isupper():
result += chr(((a_inv(a) * (ord(char) - b - 65)) % 26) + 65)
elif char.islower():
result += chr(((a_inv(a) * (ord(char) - b - 97)) % 26) + 97)
else:
result += char
return result
def a_inv(a):
for i in range(1, 26):
if (a*i) % 26 == 1:
return i
return None
```
维吉尼亚密码
加密代码:
```python
def vigenere_cipher(text, key):
result = ""
key = key.upper()
index = 0
for i in range(len(text)):
char = text[i]
if char.isupper():
result += chr((ord(char) + ord(key[index]) - 130) % 26 + 65)
elif char.islower():
result += chr((ord(char) + ord(key[index]) - 194) % 26 + 97)
else:
result += char
if char.isalpha():
index = (index + 1) % len(key)
return result
```
解密代码:
```python
def vigenere_decipher(text, key):
result = ""
key = key.upper()
index = 0
for i in range(len(text)):
char = text[i]
if char.isupper():
result += chr((ord(char) - ord(key[index]) + 26) % 26 + 65)
elif char.islower():
result += chr((ord(char) - ord(key[index]) + 26) % 26 + 97)
else:
result += char
if char.isalpha():
index = (index + 1) % len(key)
return result
```
栅栏密码
加密代码:
```python
def rail_fence_cipher(text, key):
result = ""
for i in range(key):
for j in range(i, len(text), key):
result += text[j]
return result
```
解密代码:
```python
def rail_fence_decipher(text, key):
result = [""] * len(text)
index = 0
for i in range(key):
j = i
while j < len(text):
result[j] = text[index]
index += 1
j += key
return "".join(result)
```
多倍经典密码
加密代码:
```python
def playfair_cipher(text, key):
key = key.upper().replace("J", "I")
text = text.upper().replace("J", "I")
matrix = create_matrix(key)
text_pairs = create_text_pairs(text)
result = ""
for pair in text_pairs:
row1, col1 = get_position(matrix, pair[0])
row2, col2 = get_position(matrix, pair[1])
if row1 == row2:
result += matrix[row1][(col1 + 1) % 5] + matrix[row2][(col2 + 1) % 5]
elif col1 == col2:
result += matrix[(row1 + 1) % 5][col1] + matrix[(row2 + 1) % 5][col2]
else:
result += matrix[row1][col2] + matrix[row2][col1]
return result
def create_matrix(key):
key_chars = []
matrix = []
for char in key:
if char not in key_chars:
key_chars.append(char)
alphabet = "ABCDEFGHIKLMNOPQRSTUVWXYZ"
for char in alphabet:
if char not in key_chars:
key_chars.append(char)
for i in range(5):
matrix.append(key_chars[i*5:(i+1)*5])
return matrix
def create_text_pairs(text):
pairs = []
i = 0
while i < len(text):
if i == len(text) - 1:
pairs.append(text[i] + "X")
i += 1
elif text[i] == text[i+1]:
pairs.append(text[i] + "X")
i += 1
else:
pairs.append(text[i:i+2])
i += 2
return pairs
def get_position(matrix, char):
for i in range(5):
for j in range(5):
if matrix[i][j] == char:
return i, j
return None
def multi_cipher(text, key):
result = ""
for i in range(len(key)):
shift = ord(key[i]) - 65
result += caesar_cipher(text, shift)
text = result
result = ""
return text
```
解密代码:
```python
def playfair_decipher(text, key):
key = key.upper().replace("J", "I")
matrix = create_matrix(key)
text_pairs = create_text_pairs(text)
result = ""
for pair in text_pairs:
row1, col1 = get_position(matrix, pair[0])
row2, col2 = get_position(matrix, pair[1])
if row1 == row2:
result += matrix[row1][(col1 - 1) % 5] + matrix[row2][(col2 - 1) % 5]
elif col1 == col2:
result += matrix[(row1 - 1) % 5][col1] + matrix[(row2 - 1) % 5][col2]
else:
result += matrix[row1][col2] + matrix[row2][col1]
return result.replace("X", "")
def multi_decipher(text, key):
result = ""
for i in range(len(key)):
shift = ord(key[i]) - 65
result += caesar_decipher(text, shift)
text = result
result = ""
return text
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)