# Encrypting data using AES256 GCM and encoding for DPP - Old, to be removed

## Prerequisites

* node.js


## Encryption script

```javascript
const crypto = require('crypto');
const fs = require('fs');

// Constants for encryption
const ALGORITHM = 'aes-256-gcm';
const MARKER = 2;
const CHUNK_SIZE = 1024 * 1024; // 1MB chunk size
const MAX_CHUNKS = 20;
const KEY = crypto.randomBytes(32); // Replace with your 256-bit key

// Helper function to generate a unique nonce
function generateNonce() {
    return crypto.randomBytes(12);
}

// Function to encrypt a chunk
function encryptChunk(data, key) {
    const nonce = generateNonce();
    const cipher = crypto.createCipheriv(ALGORITHM, key, nonce);

    const encrypted = Buffer.concat([cipher.update(data), cipher.final()]);
    const authTag = cipher.getAuthTag();

    return { encrypted, nonce, authTag };
}

// Function to encrypt data and encode for DPP
function encryptAndEncodeDataForDPP(data, key) {
    // Split data into 1MB chunks
    const chunks = Array.from({ length: Math.ceil(data.length / CHUNK_SIZE) }, (_, i) =>
        data.slice(i * CHUNK_SIZE, (i + 1) * CHUNK_SIZE)
    );

    // Check if data exceeds the maximum number of allowed chunks
    if (chunks.length > MAX_CHUNKS) {
        throw new Error(`Data exceeds maximum allowed size of ${MAX_CHUNKS * CHUNK_SIZE} bytes`);
    }

    // Encrypt and encode each chunk
    const encodedChunks = chunks.map(chunkData => {
        const { encrypted, nonce, authTag } = encryptChunk(chunkData, key);

        // Construct the chunk encoding
        const chunkSize = Buffer.alloc(4);
        chunkSize.writeUInt32BE(encrypted.length + nonce.length + authTag.length);

        return Buffer.concat([chunkSize, nonce, encrypted, authTag]);
    });

    // Prepend the marker and concatenate all encoded chunks
    return Buffer.concat([Buffer.from([ENCRYPTION_INDICATOR]), ...encodedChunks]);
}

// Example usage
const data = Buffer.from('Your data to encrypt and encode here...'); // Replace with actual data
try {
    const encodedData = encryptAndEncodeDataForDPP(data, KEY);
    fs.writeFileSync('encoded_data.bin', encodedData);
    console.log('Data encrypted, encoded, and saved as encoded_data.bin');
} catch (error) {
    console.error('Encoding failed:', error.message);
}
```