基于Python实现在线加密解密网站系统
from flask import Flask, render_template, request, jsonify
from cryptography.fernet import Fernet
app = Flask(__name__)
# 硬编码的密钥,务必在生产环境中避免这样做
key = b'9P5o8VfVP54FAtlC5tgDc7dY-nQP-romGLMDx0tM8wY='
# key = Fernet.generate_key() # 生成密钥
cipher_suite = Fernet(key) # 创建加密/解密套件
@app.route('/')
def index():
return render_template('index.html')
@app.route('/encrypt', methods=['POST'])
def encrypt():
data = request.json
text = data.get('text')
encrypted_text = cipher_suite.encrypt(text.encode()).decode()
return jsonify({"encrypted_text": encrypted_text})
@app.route('/decrypt', methods=['POST'])
def decrypt():
data = request.json
text = data.get('text')
try:
decrypted_text = cipher_suite.decrypt(text.encode()).decode()
except:
return jsonify({"error": "解密失败,请提供有效的加密数据"}), 400
return jsonify({"decrypted_text": decrypted_text})
if __name__ == "__main__":
app.run(debug=True)