Skip to content

Commit

Permalink
fix: decrypting posts
Browse files Browse the repository at this point in the history
  • Loading branch information
ImLunaHey committed Feb 12, 2025
1 parent aa7c5ef commit 2a4090d
Showing 1 changed file with 50 additions and 32 deletions.
82 changes: 50 additions & 32 deletions src/components/post-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,41 @@ const PostDropdownMenu = ({ post, setTranslatedText }: { post: BSkyPost; setTran
);
};

async function createEncryptedPost(content: string) {
// Generate encryption key and IV
const key = await crypto.subtle.generateKey({ name: 'AES-CBC', length: 256 }, true, ['encrypt', 'decrypt']);

const iv = crypto.getRandomValues(new Uint8Array(16));
const encoder = new TextEncoder();
const data = encoder.encode(content);

// Encrypt content
const encrypted = await crypto.subtle.encrypt({ name: 'AES-CBC', iv }, key, data);

// Export key material
const rawKey = await crypto.subtle.exportKey('raw', key);
const keyString = btoa(String.fromCharCode(...new Uint8Array(rawKey)));

// Combine IV and ciphertext
const combined = new Uint8Array(iv.byteLength + encrypted.byteLength);
combined.set(iv, 0);
combined.set(new Uint8Array(encrypted), iv.byteLength);

return {
record: {
text: '', // Empty public text
encryption: {
type: 'AES-CBC',
key: keyString,
},
encryptedText: btoa(String.fromCharCode(...combined)),
},
};
}

window.createEncryptedPost = createEncryptedPost;

async function decryptPrivatePost(post: BSkyPost) {
// If no encryption data, return the regular text
if (!post.record.encryption || !post.record.encryptedText) {
return post.record.text;
}
Expand All @@ -217,52 +250,37 @@ async function decryptPrivatePost(post: BSkyPost) {
const encryptedText = post.record.encryptedText;

try {
// Convert key string to array buffer
console.info('key:', key);
const keyData = Uint8Array.from(atob(key), (c) => c.charCodeAt(0));
// Decode base64 key
const keyData = new Uint8Array(Array.from(atob(key), (c) => c.charCodeAt(0)));

// Import the raw key
console.info('keyData:', keyData);
const cryptoKey = await window.crypto.subtle.importKey(
// Import key (without length parameter)
const cryptoKey = await crypto.subtle.importKey(
'raw',
keyData,
{ name: post.record.encryption.type, length: 256 },
{ name: post.record.encryption.type }, // Removed length
false,
['decrypt'],
);

// Decode base64 encrypted text
console.info('encryptedText:', encryptedText);
const encryptedData = Uint8Array.from(atob(encryptedText), (c) => c.charCodeAt(0));

// First 16 bytes should be IV
const iv = encryptedData.slice(0, 16);
const data = encryptedData.slice(16);
console.info('iv:', iv);
console.info('data:', data);

// Decrypt the data
const decryptedData = await window.crypto.subtle.decrypt(
{
name: post.record.encryption.type,
iv: iv,
},
cryptoKey,
data,
);
// Decode base64 payload
const encryptedData = new Uint8Array(Array.from(atob(encryptedText), (c) => c.charCodeAt(0)));

// Extract IV and ciphertext with proper buffer boundaries
const iv = new Uint8Array(encryptedData.buffer, 0, 16);
const data = new Uint8Array(encryptedData.buffer, 16);

console.info('decryptedData:', decryptedData);
// Decrypt
const decryptedData = await crypto.subtle.decrypt({ name: post.record.encryption.type, iv }, cryptoKey, data);

// Convert the decrypted array buffer back to text
const text = new TextDecoder().decode(decryptedData);
console.info('decrypted text:', text);
return text;
return new TextDecoder().decode(decryptedData);
} catch (error) {
console.error('Decryption failed:', error);
return 'decryption failed';
}
}

window.decryptPrivatePost = decryptPrivatePost;

type PostCardInnerProps = {
post: BSkyPost;
context?: string;
Expand Down

0 comments on commit 2a4090d

Please sign in to comment.