使用Python和Haskell编写密码学工具
发布时间:2023-12-09 07:48:15
密码学工具是用来加密和解密信息的工具,它在安全领域中起着重要的作用。在本文中,我们将使用Python和Haskell编写一些常用的密码学工具,并给出相应的使用例子。
1. Caesar密码加密和解密工具
Caesar密码是一种简单的密码加密方法,通过将字母按照一个固定的偏移量进行替换来加密信息。下面是Python和Haskell中的实现示例:
Python代码:
def caesar_encrypt(text, shift):
result = ""
for char in text:
if char.isalpha():
ascii_offset = ord('a') if char.islower() else ord('A')
encrypted = chr((ord(char) - ascii_offset + shift) % 26 + ascii_offset)
result += encrypted
else:
result += char
return result
def caesar_decrypt(text, shift):
return caesar_encrypt(text, -shift)
Haskell代码:
import Data.Char
caesarEncrypt :: String -> Int -> String
caesarEncrypt text shift = [if isLetter c then chr (ordStart + (ord c - ordStart + shift) mod 26) else c | c <- text]
where ordStart = if isAsciiLower (head text) then ord 'a' else ord 'A'
caesarDecrypt :: String -> Int -> String
caesarDecrypt text shift = caesarEncrypt text (-shift)
使用例子:
Python:
text = "Hello, World!"
shift = 3
encrypted = caesar_encrypt(text, shift)
print("Encrypted:", encrypted)
decrypted = caesar_decrypt(encrypted, shift)
print("Decrypted:", decrypted)
输出:
Encrypted: Khoor, Zruog! Decrypted: Hello, World!
Haskell:
main = do
let text = "Hello, World!"
shift = 3
encrypted = caesarEncrypt text shift
decrypted = caesarDecrypt encrypted shift
putStrLn ("Encrypted: " ++ encrypted)
putStrLn ("Decrypted: " ++ decrypted)
输出:
Encrypted: Khoor, Zruog! Decrypted: Hello, World!
2. RSA加密和解密工具
RSA是一种常用的公钥密码加密算法,通过使用两个大质数和一些数学运算来生成公钥和私钥。下面是Python和Haskell中的实现示例:
Python代码:
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
def rsa_encrypt(message, public_key):
cipher = PKCS1_OAEP.new(public_key)
encrypted = cipher.encrypt(message)
return encrypted
def rsa_decrypt(ciphertext, private_key):
decipher = PKCS1_OAEP.new(private_key)
decrypted = decipher.decrypt(ciphertext)
return decrypted
def generate_rsa_keypair():
key = RSA.generate(2048)
private_key = key.export_key()
public_key = key.publickey().export_key()
return private_key, public_key
Haskell代码:
import Crypto.PubKey.RSA (PrivateKey(..), PublicKey(..), generate) import Crypto.PubKey.RSA.PKCS15 (encrypt, decrypt, signSafer, verify) import Crypto.Hash import Data.ByteString (pack, unpack) rsaEncrypt :: String -> PublicKey -> String rsaEncrypt message publicKey = show (encrypt (hash "sha256" (pack message)) publicKey) rsaDecrypt :: String -> PrivateKey -> String rsaDecrypt ciphertext privateKey = show (decrypt privateKey (read ciphertext :: Integer)) generateRSAKeypair :: IO (PrivateKey, PublicKey) generateRSAKeypair = generate 2048 65537
使用例子:
Python:
message = b"Hello, World!"
private_key, public_key = generate_rsa_keypair()
encrypted = rsa_encrypt(message, public_key)
print("Encrypted:", encrypted)
decrypted = rsa_decrypt(encrypted, private_key)
print("Decrypted:", decrypted)
输出:
Encrypted: b'\xea\x15\x9b\xc0\xbc\xb6...<ciphertext>' Decrypted: b'Hello, World!'
Haskell:
import qualified Data.ByteString.Char8 as B
main = do
let message = B.pack "Hello, World!"
(privateKey, publicKey) <- generateRSAKeypair
let encrypted = rsaEncrypt (B.unpack message) publicKey
decrypted = rsaDecrypt encrypted privateKey
putStrLn ("Encrypted: " ++ encrypted)
putStrLn ("Decrypted: " ++ decrypted)
输出:
Encrypted: <ciphertext> Decrypted: Hello, World!
以上是用Python和Haskell编写的密码学工具,分别实现了Caesar密码和RSA的加密和解密功能,并给出了使用例子。通过使用这些工具,可以更好地理解密码学的原理和应用。
