古典密码的全部python加密解密
时间: 2023-10-19 20:19:27 浏览: 180
古典密码技术-Python:密码:具有古典加密算法(例如Caesar密码,Hill密码和Vigenere密码)的Python程序
5星 · 资源好评率100%
由于古典密码的种类很多,下面只列举了一些常见的古典密码的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
```
阅读全文