cryptia
Version:
Cryptia is a simple JavaScript library for encrypting and decrypting text using a basic substitution cipher. It provides an easy-to-use interface for securing text data in client-side applications..
112 lines (102 loc) • 3.47 kB
JavaScript
import { program } from 'commander'
import fs from 'fs'
import path from 'path'
import readline from 'readline'
import Cryptia from './cryptia.js'
const cryptia = Cryptia({
logging: true,
obfuscationLevel: 8,
preserveWhitespace: true
})
program
.name('cryptia')
.description('CLI for Cryptia encryption/decryption')
.version('1.0.6')
program
.command('encrypt')
.description('Encrypt text or file')
.option('-t, --text <text>', 'Text to encrypt')
.option('-f, --file <path>', 'File to encrypt')
.option('-o, --output <path>', 'Output file path')
.option('-k, --key <key>', 'Encryption key')
.action(async (options) => {
try {
const key = options.key || await promptForKey('Enter encryption key: ')
if (options.text) {
// Encrypt text
const result = cryptia.encrypt(options.text, key)
if (options.output) {
fs.writeFileSync(options.output, result.data)
console.log(`Encrypted content saved to ${options.output}`)
} else {
console.log('Encrypted result:', result.data)
}
console.log(`Encryption completed in ${result.timeTaken.toFixed(4)}ms`)
}
else if (options.file) {
// Encrypt file
const outputPath = options.output || `${options.file}.encrypted`
const result = cryptia.encryptFile(options.file, key, null, outputPath)
console.log(`File encrypted to ${result.encryptedFilePath}`)
console.log(`Encryption completed in ${result.timeTaken.toFixed(4)}ms`)
}
else {
console.error('Please provide either text or file to encrypt')
process.exit(1)
}
} catch (error) {
console.error('Error:', error.message)
process.exit(1)
}
})
program
.command('decrypt')
.description('Decrypt text or file')
.option('-t, --text <text>', 'Text to decrypt')
.option('-f, --file <path>', 'File to decrypt')
.option('-o, --output <path>', 'Output file path')
.option('-k, --key <key>', 'Decryption key')
.action(async (options) => {
try {
const key = options.key || await promptForKey('Enter decryption key: ')
if (options.text) {
// Decrypt text
const result = cryptia.decrypt(options.text, key)
if (options.output) {
fs.writeFileSync(options.output, result.data)
console.log(`Decrypted content saved to ${options.output}`)
} else {
console.log('Decrypted result:', result.data)
}
console.log(`Decryption completed in ${result.timeTaken.toFixed(4)}ms`)
}
else if (options.file) {
// Decrypt file
const outputPath = options.output || `${options.file}.decrypted`
const result = cryptia.decryptFile(options.file, key, null, outputPath)
console.log(`File decrypted to ${result.decryptedFilePath}`)
console.log(`Decryption completed in ${result.timeTaken.toFixed(4)}ms`)
}
else {
console.error('Please provide either text or file to decrypt')
process.exit(1)
}
} catch (error) {
console.error('Error:', error.message)
process.exit(1)
}
})
async function promptForKey(prompt) {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
return new Promise((resolve) => {
rl.question(prompt, (key) => {
rl.close()
resolve(key)
})
})
}
program.parse()