2 Commits

2 changed files with 66 additions and 13 deletions
+4 -4
View File
@@ -10,8 +10,8 @@ an encryption tool!
## usage ## usage
```sh ```sh
ec-pass <input file> -p <password> [-o <out>] ec-pass <input file> -p <password> [-o <out>] # generates a password-protected velvet file
dec-pass <input file> -p <password> [-o <out>] dec-pass <input file> -p <password> [-o <out>] # decrypts password-protected velvet files
ec-key <input file> [-o <out>] ec-key <input file> [-o <out>] [-p <key path>] # generates a 32-byte ring and a velvet file
dec-key <input file> [-o <out>] dec-key <input file> [-o <out>] [-p <key path>] # decrypts a velvet file using a ring file. assumes that the ring file shares the same name as the input file, unless a key is supplied with -p
``` ```
+61 -8
View File
@@ -1,9 +1,20 @@
#include <cstddef>
#include <filesystem>
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <vector> #include <vector>
#include <string> #include <string>
#include <stdexcept> #include <stdexcept>
#include <algorithm>
#include "velvet_ring.h" #include "velvet_ring.h"
#include <unistd.h>
bool is_ascii(const std::string& str) {
return std::all_of(str.begin(), str.end(), [](unsigned char c) {
return std::isprint(c) || c == '\n' || c == '\r' || c == '\t';
});
}
using ByteVec = std::vector<unsigned char>; using ByteVec = std::vector<unsigned char>;
@@ -48,13 +59,18 @@ std::string get_opt(int argc, char* argv[], const std::string opt) {
std::string help = R"(love is a gentle thing(lx) by maki std::string help = R"(love is a gentle thing(lx) by maki
v1.0 v1.1
usage: usage:
ec-pass <input file> -p <password> [-o <out>] ec-pass <input file> -p <password> [-o <out>] # generates a password-protected velvet file
dec-pass <input file> -p <password> [-o <out>] dec-pass <input file> -p <password> [-o <out>] # decrypts password-protected velvet files
ec-key <input file> [-o <out>] ec-key <input file> [-o <out>] [-p <key path>] # generates a 32-byte ring and a velvet file
dec-key <input file> [-o <out>])"; dec-key <input file> [-o <out>] [-p <key path>] # decrypts a velvet file using a ring file. assumes that the ring file shares the same name as the input file, unless a key is supplied with -p
options:
-p provide a password or ring file
-o set the destination of processed files
-r print content if non-ascii file)";
void usage() { void usage() {
std::cout << help << "\n"; std::cout << help << "\n";
@@ -71,8 +87,30 @@ int main(int argc, char* argv[]) {
std::string mode = argv[1]; std::string mode = argv[1];
std::string in = argv[2]; std::string in = argv[2];
std::string out = get_opt(argc, argv, "-o"); std::string out = "";
std::string pass = get_opt(argc, argv, "-p"); std::string pass = "";
bool print_raw = false;
int opt;
while ((opt = getopt(argc, argv, "ro:p:")) != -1) {
switch (opt) {
case 'r':
print_raw = true;
break;
case 'o':
out = optarg;
break;
case 'p':
pass = optarg;
break;
default:
usage();
return 1;
}
}
try { try {
ByteVec input = read_file(in); ByteVec input = read_file(in);
@@ -81,6 +119,8 @@ int main(int argc, char* argv[]) {
if (mode == "ec-key") { if (mode == "ec-key") {
if (out.empty()) out = enc_out(in); if (out.empty()) out = enc_out(in);
std::string ring = ring_name(out); std::string ring = ring_name(out);
if (!pass.empty())
ring = pass;
ByteVec key = rb(32); ByteVec key = rb(32);
ByteVec enc = ec_wky( ByteVec enc = ec_wky(
@@ -98,6 +138,12 @@ int main(int argc, char* argv[]) {
else if (mode == "dec-key") { else if (mode == "dec-key") {
if (out.empty()) out = dec_out(in); if (out.empty()) out = dec_out(in);
std::string ring = ring_name(in); std::string ring = ring_name(in);
if (!std::filesystem::exists(ring)) {
if (!pass.empty())
ring = pass;
else
throw std::runtime_error("provide a ring file!");
}
ByteVec key = read_file(ring); ByteVec key = read_file(ring);
if (key.size() != 32) if (key.size() != 32)
@@ -106,6 +152,9 @@ int main(int argc, char* argv[]) {
std::string dec = dec_wky(input, key); std::string dec = dec_wky(input, key);
write_file(out, ByteVec(dec.begin(), dec.end())); write_file(out, ByteVec(dec.begin(), dec.end()));
if (is_ascii(dec) || print_raw)
std::cout << dec;
//std::cout << "Decrypted " << out << "\n"; //std::cout << "Decrypted " << out << "\n";
} }
@@ -134,6 +183,10 @@ int main(int argc, char* argv[]) {
std::string dec = dec_p(input, pass); std::string dec = dec_p(input, pass);
write_file(out, ByteVec(dec.begin(), dec.end())); write_file(out, ByteVec(dec.begin(), dec.end()));
if (is_ascii(dec) || print_raw) {
std::cout << dec;
}
//std::cout << "Decrypted " << out << "\n"; // i think this is redundnant now cuz duh it better be there //std::cout << "Decrypted " << out << "\n"; // i think this is redundnant now cuz duh it better be there
} }
@@ -142,7 +195,7 @@ int main(int argc, char* argv[]) {
return 1; return 1;
} }
} }
catch (const std::exception e) { catch (const std::exception& e) {
std::cerr << "error: " << e.what() << "\n"; std::cerr << "error: " << e.what() << "\n";
return 1; return 1;
} }