tahamajs/IE / Projects /front /src /pages /EncryptionPage.tsx
tahamajs's picture
download
raw
14.6 kB
import React, { useState } from 'react';
import { motion, AnimatePresence } from 'framer-motion';
import { Lock, Unlock, Key, ShieldCheck, RefreshCw, Copy, Check, Plus, Trash2, ShieldAlert } from 'lucide-react';
import { toast } from 'react-toastify';
interface GpgKeyPair {
id: string;
name: string;
email: string;
keyId: string;
fingerprint: string;
created: string;
type: 'RSA' | 'ECDSA';
publicKey: string;
}
export default function EncryptionPage() {
const [keys, setKeys] = useState<GpgKeyPair[]>([
{
id: 'k-1',
name: 'Dr. Taha Majs (Scientific Signatures)',
email: 'taha@ie-platform.org',
keyId: '0x9E2A7B8C',
fingerprint: 'A5C3 E9F8 B2D4 C7E6 A1B2 C3D4 E5F6 7A8B 9C0D E1F2',
created: new Date(Date.now() - 60 * 24 * 3600 * 1000).toISOString(),
type: 'ECDSA',
publicKey: '-----BEGIN PGP PUBLIC KEY BLOCK-----\nmQINBF4... (Truncated)\n-----END PGP PUBLIC KEY BLOCK-----'
}
]);
const [showGenerateModal, setShowGenerateModal] = useState(false);
const [keyName, setKeyName] = useState('');
const [keyEmail, setKeyEmail] = useState('');
const [keyType, setKeyType] = useState<'RSA' | 'ECDSA'>('ECDSA');
const [isGenerating, setIsGenerating] = useState(false);
// Encryption sandbox state
const [plainText, setPlainText] = useState('');
const [cipherText, setCipherText] = useState('');
const [decryptInput, setDecryptInput] = useState('');
const [decryptedText, setDecryptedText] = useState('');
const [copiedId, setCopiedId] = useState<string | null>(null);
const handleGenerateKey = (e: React.FormEvent) => {
e.preventDefault();
if (!keyName.trim() || !keyEmail.trim()) {
toast.error('Name and email parameters are required');
return;
}
setIsGenerating(true);
toast.info('Generating cryptographic key pair...');
setTimeout(() => {
const generatedId = `0x${Math.random().toString(16).substring(2, 10).toUpperCase()}`;
const fingerprint = Array.from({ length: 10 }, () =>
Math.random().toString(16).substring(2, 6).toUpperCase()
).join(' ');
const newKey: GpgKeyPair = {
id: `k-${Date.now()}`,
name: keyName,
email: keyEmail,
keyId: generatedId,
fingerprint,
created: new Date().toISOString(),
type: keyType,
publicKey: `-----BEGIN PGP PUBLIC KEY BLOCK-----\nVersion: OpenPGP.js\nComment: Research Signature Key\n\nmQINBF4${Math.random().toString(36).substring(2, 24)}...`
};
setKeys([...keys, newKey]);
setIsGenerating(false);
setShowGenerateModal(false);
setKeyName('');
setKeyEmail('');
toast.success('Key pair generated successfully');
}, 2000);
};
const handleCopy = (text: string, id: string) => {
navigator.clipboard.writeText(text);
setCopiedId(id);
toast.success('Key copied to clipboard');
setTimeout(() => setCopiedId(null), 2000);
};
const handleDeleteKey = (id: string) => {
if (confirm('Are you sure you want to revoke and delete this key pair? Encrypted items referencing this key will be impossible to recover.')) {
setKeys(keys.filter(k => k.id !== id));
toast.info('Cryptographic key pair deleted');
}
};
const handleEncryptSandbox = () => {
if (!plainText.trim()) return;
const cipher = `-----BEGIN PGP MESSAGE-----\nVersion: OpenPGP.js\n\nhQIMA4c8B5q...${btoa(plainText).substring(0, 48)}\n-----END PGP MESSAGE-----`;
setCipherText(cipher);
toast.success('Message encrypted');
};
const handleDecryptSandbox = () => {
if (!decryptInput.trim()) return;
try {
if (decryptInput.includes('-----BEGIN PGP MESSAGE-----')) {
const lines = decryptInput.split('\n');
const b64 = lines[3] || '';
setDecryptedText(atob(b64.substring(0, 16)) || 'Sample Decrypted Text');
} else {
setDecryptedText(atob(decryptInput) || 'Sample Decrypted Text');
}
toast.success('Decryption complete');
} catch {
setDecryptedText('Decryption failure: Invalid cipher payload structure or signature match');
toast.error('Decryption failed');
}
};
return (
<div className="min-h-screen p-6 md:p-10 bg-slate-950 text-white">
<motion.div
initial={{ opacity: 0, y: 15 }}
animate={{ opacity: 1, y: 0 }}
className="max-w-6xl mx-auto space-y-8"
>
{/* Header */}
<div className="flex flex-col md:flex-row md:items-center justify-between gap-4 border-b border-white/10 pb-6">
<div>
<h1 className="text-3xl font-extrabold tracking-tight bg-gradient-to-r from-violet-400 via-indigo-200 to-cyan-400 bg-clip-text text-transparent">
Zero-Knowledge & Encryption
</h1>
<p className="text-slate-400 mt-1 text-sm md:text-base">
Secure your research submissions, verify metadata authorship via GPG signatures, and inspect cryptographic keys.
</p>
</div>
<button
onClick={() => setShowGenerateModal(true)}
className="flex items-center justify-center gap-2 px-5 py-2.5 bg-gradient-to-r from-violet-600 to-indigo-600 hover:from-violet-500 hover:to-indigo-500 rounded-xl font-medium shadow-lg transition active:scale-95"
>
<Plus className="w-5 h-5" /> Generate Key Pair
</button>
</div>
{/* Credentials Sandbox */}
<div className="grid grid-cols-1 lg:grid-cols-3 gap-8">
{/* Key pair lists (Col span 2) */}
<div className="lg:col-span-2 space-y-6">
<h2 className="text-xl font-bold flex items-center gap-2">
<Key className="w-5 h-5 text-indigo-400" /> Active Cryptographic Credentials
</h2>
<div className="space-y-4">
{keys.length === 0 ? (
<div className="glass p-10 text-center text-slate-500 rounded-2xl border border-white/10">
No cryptographic keys installed. Click "Generate Key Pair" to start.
</div>
) : (
keys.map(k => (
<div key={k.id} className="glass p-6 rounded-2xl border border-white/10 bg-slate-900/40 relative group overflow-hidden">
<div className="flex flex-col md:flex-row md:items-start justify-between gap-4">
<div className="space-y-2">
<div className="flex items-center gap-2.5">
<span className="font-bold text-slate-200">{k.name}</span>
<span className="text-[10px] bg-indigo-500/10 text-indigo-300 border border-indigo-500/20 px-2 py-0.5 rounded font-mono font-bold uppercase">{k.type}</span>
<span className="text-[10px] bg-slate-950 border border-white/5 text-slate-400 px-2 py-0.5 rounded font-mono">{k.keyId}</span>
</div>
<p className="text-xs text-slate-400 font-mono">Email: {k.email}</p>
<p className="text-[10px] text-slate-500 font-mono">Fingerprint: {k.fingerprint}</p>
<p className="text-[10px] text-slate-500 font-mono">Created: {new Date(k.created).toLocaleString()}</p>
</div>
<div className="flex items-center gap-2 self-start flex-shrink-0">
<button
onClick={() => handleCopy(k.publicKey, k.id)}
className="px-3 py-1.5 text-xs font-semibold bg-indigo-500/10 hover:bg-indigo-500/20 text-indigo-400 rounded-lg transition"
>
{copiedId === k.id ? <Check className="w-4 h-4" /> : 'Copy Public Key'}
</button>
<button
onClick={() => handleDeleteKey(k.id)}
className="p-2 bg-rose-500/10 hover:bg-rose-500/20 text-rose-400 rounded-lg transition"
>
<Trash2 className="w-4 h-4" />
</button>
</div>
</div>
</div>
))
)}
</div>
</div>
{/* Sandbox utilities (Col 1) */}
<div className="lg:col-span-1 space-y-6">
<div className="glass p-6 rounded-3xl border border-white/10 bg-slate-900/40 space-y-6">
<h2 className="text-lg font-bold flex items-center gap-2">
<Lock className="w-5 h-5 text-indigo-400" /> Encryption Sandbox
</h2>
{/* Encryption tab */}
<div className="space-y-4 text-sm">
<div className="space-y-2">
<label className="text-xs font-bold uppercase text-slate-400">Plaintext</label>
<textarea
rows={2}
value={plainText}
onChange={(e) => setPlainText(e.target.value)}
placeholder="Message to encrypt..."
className="w-full bg-slate-950 border border-white/10 rounded-xl p-3 text-xs focus:outline-none focus:ring-1 focus:ring-indigo-500 placeholder-slate-600 text-white resize-none"
/>
</div>
<button
onClick={handleEncryptSandbox}
className="w-full py-2 bg-indigo-600 hover:bg-indigo-500 rounded-xl font-semibold transition"
>
Encrypt Message
</button>
{cipherText && (
<div className="space-y-2 pt-2 border-t border-white/5">
<div className="flex justify-between items-center">
<label className="text-xs font-bold uppercase text-slate-400">Ciphertext</label>
<button onClick={() => handleCopy(cipherText, 'cipher')} className="text-[10px] text-indigo-400 hover:underline">Copy Cipher</button>
</div>
<pre className="bg-slate-950 border border-white/5 rounded-xl p-3 font-mono text-[9px] text-slate-400 max-h-24 overflow-y-auto whitespace-pre-wrap">
{cipherText}
</pre>
</div>
)}
</div>
</div>
</div>
</div>
</motion.div>
{/* Generation Modal */}
<AnimatePresence>
{showGenerateModal && (
<div className="fixed inset-0 z-50 flex items-center justify-center p-4 bg-slate-950/80 backdrop-blur-md">
<motion.div
initial={{ scale: 0.95, opacity: 0 }}
animate={{ scale: 1, opacity: 1 }}
exit={{ scale: 0.95, opacity: 0 }}
className="glass max-w-md w-full rounded-3xl border border-white/10 bg-slate-900 p-6 md:p-8 shadow-2xl relative"
>
<div className="flex items-center justify-between border-b border-white/10 pb-4 mb-6">
<h3 className="text-xl font-bold">Generate Signature Key</h3>
<button onClick={() => setShowGenerateModal(false)} className="text-slate-400 hover:text-white text-sm">Cancel</button>
</div>
<form onSubmit={handleGenerateKey} className="space-y-6">
<div className="space-y-2">
<label className="text-xs font-bold uppercase tracking-wider text-slate-400">Owner Name</label>
<input
type="text"
required
value={keyName}
onChange={(e) => setKeyName(e.target.value)}
placeholder="e.g. Dr. Taha Majs"
className="w-full bg-slate-950 border border-white/10 rounded-xl px-4 py-3 text-sm focus:outline-none focus:ring-2 focus:ring-indigo-500 placeholder-slate-600 text-white"
/>
</div>
<div className="space-y-2">
<label className="text-xs font-bold uppercase tracking-wider text-slate-400">Email Address</label>
<input
type="email"
required
value={keyEmail}
onChange={(e) => setKeyEmail(e.target.value)}
placeholder="e.g. taha@ie-platform.org"
className="w-full bg-slate-950 border border-white/10 rounded-xl px-4 py-3 text-sm focus:outline-none focus:ring-2 focus:ring-indigo-500 placeholder-slate-600 text-white"
/>
</div>
<div className="space-y-2">
<label className="text-xs font-bold uppercase tracking-wider text-slate-400">Algorithm</label>
<select
value={keyType}
onChange={(e) => setKeyType(e.target.value as any)}
className="w-full bg-slate-950 border border-white/10 rounded-xl px-4 py-3 text-sm focus:outline-none focus:ring-2 focus:ring-indigo-500 text-white"
>
<option value="ECDSA">ECDSA (Curve 25519) - Recommended</option>
<option value="RSA">RSA (4096-bit)</option>
</select>
</div>
{isGenerating ? (
<div className="flex items-center justify-center gap-2.5 py-3 text-indigo-400 font-semibold text-sm">
<RefreshCw className="w-5 h-5 animate-spin" /> Simulating entropy computations...
</div>
) : (
<div className="flex gap-4 pt-4 border-t border-white/10">
<button
type="button"
onClick={() => setShowGenerateModal(false)}
className="flex-1 px-4 py-2.5 border border-white/10 rounded-xl text-slate-300 hover:text-white font-medium transition"
>
Cancel
</button>
<button
type="submit"
className="flex-1 px-4 py-2.5 bg-gradient-to-r from-violet-600 to-indigo-600 hover:from-violet-500 hover:to-indigo-500 rounded-xl font-medium shadow-lg transition"
>
Generate Key
</button>
</div>
)}
</form>
</motion.div>
</div>
)}
</AnimatePresence>
</div>
);
}

Xet Storage Details

Size:
14.6 kB
·
Xet hash:
014a663372b48eff393dfb5421a5d2ccea4c8cd1e3e1c5c9acbee3c30a1ac77a

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.