obscr 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/publish.yml +18 -0
- package/README.md +72 -0
- package/bin/index.js +157 -0
- package/bin/utils/crypto.js +90 -0
- package/bin/utils/mersenne-twister.js +187 -0
- package/bin/utils/steg.js +124 -0
- package/bin/utils/utils.js +124 -0
- package/package.json +32 -0
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
name: Publish Package to npmjs
|
|
2
|
+
on:
|
|
3
|
+
release:
|
|
4
|
+
types: [created]
|
|
5
|
+
jobs:
|
|
6
|
+
build:
|
|
7
|
+
runs-on: ubuntu-latest
|
|
8
|
+
steps:
|
|
9
|
+
- uses: actions/checkout@v3
|
|
10
|
+
# Setup .npmrc file to publish to npm
|
|
11
|
+
- uses: actions/setup-node@v3
|
|
12
|
+
with:
|
|
13
|
+
node-version: "16.x"
|
|
14
|
+
registry-url: "https://registry.npmjs.org"
|
|
15
|
+
- run: npm ci
|
|
16
|
+
- run: npm publish
|
|
17
|
+
env:
|
|
18
|
+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
|
package/README.md
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
|
|
3
|
+
</p>
|
|
4
|
+
<p align="center">
|
|
5
|
+
<img src="https://img.shields.io/badge/license-MIT-green">
|
|
6
|
+
<img src="https://img.shields.io/badge/build-passing-brightgreen">
|
|
7
|
+
<img src="https://img.shields.io/badge/version-0.1.1-orange">
|
|
8
|
+
<img src="https://img.shields.io/badge/npm-v8.3.1-blue">
|
|
9
|
+
<img src="https://img.shields.io/badge/node-v16.14.0-yellow">
|
|
10
|
+
</p>
|
|
11
|
+
<br>
|
|
12
|
+
<p align="center">A CLI to encrypt data inside images.</p>
|
|
13
|
+
<br>
|
|
14
|
+
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
## Description
|
|
18
|
+
|
|
19
|
+
CLI tool to encrypt secret messages with AES-256-GCM and a password.
|
|
20
|
+
Same password will then be used as a seed to generate a random order in which the encrypted message will be encoded inside the specified .png image.
|
|
21
|
+
|
|
22
|
+
## NPM package
|
|
23
|
+
|
|
24
|
+
```
|
|
25
|
+
npm install -g obscr
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Usage
|
|
29
|
+
|
|
30
|
+
```
|
|
31
|
+
~$ obscr --help
|
|
32
|
+
Usage: obscr <cmd> [options]
|
|
33
|
+
|
|
34
|
+
Commands:
|
|
35
|
+
obscr encrypt Encrypts and hides the message into an image.
|
|
36
|
+
obscr decrypt Decrypts message from image.
|
|
37
|
+
|
|
38
|
+
Options:
|
|
39
|
+
--version Show version number [boolean]
|
|
40
|
+
--help Show help [boolean]
|
|
41
|
+
|
|
42
|
+
copyright 2022
|
|
43
|
+
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
> :warning: **Only tested with png images**
|
|
47
|
+
|
|
48
|
+
<br>
|
|
49
|
+
|
|
50
|
+
## Installation
|
|
51
|
+
|
|
52
|
+
1. Clone the repository and then navigate to it.
|
|
53
|
+
2. Run `npm install` to install the dependencies.
|
|
54
|
+
3. Run `npm install -g .` to install the CLI. <br>
|
|
55
|
+
|
|
56
|
+
> :warning: **This might cause an error** which can be resolved easily by using `sudo` with the command, **however**, using `sudo` with `npm` is **not recommended** because it might cause permission issues later. So instead put the code below in your .bashrc file and then run the above command again.
|
|
57
|
+
|
|
58
|
+
```
|
|
59
|
+
npm set prefix ~/.npm
|
|
60
|
+
PATH="$HOME/.npm/bin:$PATH"
|
|
61
|
+
PATH="./node_modules/.bin:$PATH"
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
4. Now you are good to go and can use the CLI globally!
|
|
65
|
+
|
|
66
|
+
Type `obscr` or `obscr --help` to get started.
|
|
67
|
+
|
|
68
|
+
<br>
|
|
69
|
+
|
|
70
|
+
## License
|
|
71
|
+
|
|
72
|
+
MIT © **_Obscr_**
|
package/bin/index.js
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
#! /usr/bin/env node
|
|
2
|
+
const yargs = require("yargs");
|
|
3
|
+
const chalk = require("chalk");
|
|
4
|
+
const fs = require("fs");
|
|
5
|
+
|
|
6
|
+
const { prompt } = require("inquirer");
|
|
7
|
+
|
|
8
|
+
const { encrypt, decrypt } = require("./utils/crypto");
|
|
9
|
+
const {
|
|
10
|
+
encodeMessageToImage,
|
|
11
|
+
extractMessageFromImage,
|
|
12
|
+
} = require("./utils/steg");
|
|
13
|
+
|
|
14
|
+
//just to increase obfuscation for steg, not actually used for the AES encryption
|
|
15
|
+
const SECRET_KEY = "S3cReTK3Y";
|
|
16
|
+
|
|
17
|
+
const usage = chalk.keyword("violet")("\nUsage: obscr <cmd> [options]");
|
|
18
|
+
|
|
19
|
+
const options = yargs
|
|
20
|
+
.usage(usage)
|
|
21
|
+
|
|
22
|
+
// Create encrypt command
|
|
23
|
+
.command(
|
|
24
|
+
"encrypt",
|
|
25
|
+
"Encrypts and hides the message into an image.",
|
|
26
|
+
{
|
|
27
|
+
f: {
|
|
28
|
+
alias: "filename",
|
|
29
|
+
describe: "Name of the png image to hide the message in",
|
|
30
|
+
demandOption: true, // Required
|
|
31
|
+
type: "string",
|
|
32
|
+
},
|
|
33
|
+
c: {
|
|
34
|
+
alias: "compress",
|
|
35
|
+
describe: "Compress the secret message",
|
|
36
|
+
demandOption: false,
|
|
37
|
+
type: "string",
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
|
|
41
|
+
async (argv) => {
|
|
42
|
+
const { f: filename, c: compress } = argv;
|
|
43
|
+
|
|
44
|
+
const { password, confirmPassword, message } = await prompt([
|
|
45
|
+
{
|
|
46
|
+
type: "editor",
|
|
47
|
+
name: "message",
|
|
48
|
+
message: "Type the secret message",
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
type: "password",
|
|
52
|
+
name: "password",
|
|
53
|
+
message: "Enter Password:",
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
type: "password",
|
|
57
|
+
name: "confirmPassword",
|
|
58
|
+
message: "Re-type Password:",
|
|
59
|
+
},
|
|
60
|
+
]);
|
|
61
|
+
|
|
62
|
+
// config.set({ token });
|
|
63
|
+
if (password !== confirmPassword) {
|
|
64
|
+
return console.log(chalk.keyword("red")("Passwords don't match"));
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/*
|
|
68
|
+
#1. encrypt message with key using AES-256-GCM
|
|
69
|
+
*/
|
|
70
|
+
|
|
71
|
+
try {
|
|
72
|
+
const encrypted = encrypt(message, password);
|
|
73
|
+
console.log(encrypted);
|
|
74
|
+
await encodeMessageToImage(filename, encrypted, password + SECRET_KEY);
|
|
75
|
+
} catch (e) {
|
|
76
|
+
return console.log(chalk.keyword("red")(e.message));
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/*
|
|
80
|
+
#2. embed encrypted message scattered into image
|
|
81
|
+
*/
|
|
82
|
+
|
|
83
|
+
console.log(chalk.keyword("green")("Successful"));
|
|
84
|
+
}
|
|
85
|
+
)
|
|
86
|
+
//create decrypt command
|
|
87
|
+
.command(
|
|
88
|
+
"decrypt",
|
|
89
|
+
"Decrypts message from image.",
|
|
90
|
+
{
|
|
91
|
+
f: {
|
|
92
|
+
alias: "filename",
|
|
93
|
+
describe: "Name of the png image to hide the message in",
|
|
94
|
+
demandOption: true, // Required
|
|
95
|
+
type: "string",
|
|
96
|
+
},
|
|
97
|
+
o: {
|
|
98
|
+
alias: "output",
|
|
99
|
+
describe: "Output filename",
|
|
100
|
+
demandOption: false, // Required
|
|
101
|
+
type: "string",
|
|
102
|
+
},
|
|
103
|
+
},
|
|
104
|
+
|
|
105
|
+
async (argv) => {
|
|
106
|
+
const { f: filename, o: output } = argv;
|
|
107
|
+
|
|
108
|
+
const { password } = await prompt({
|
|
109
|
+
type: "password",
|
|
110
|
+
name: "password",
|
|
111
|
+
message: "Enter Password:",
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
/*
|
|
115
|
+
#2. extract encrypted message from image
|
|
116
|
+
*/
|
|
117
|
+
|
|
118
|
+
/*
|
|
119
|
+
#1. decrypt message with key using AES-256-GCM
|
|
120
|
+
*/
|
|
121
|
+
|
|
122
|
+
try {
|
|
123
|
+
const [succeeded, extracted] = await extractMessageFromImage(
|
|
124
|
+
filename,
|
|
125
|
+
password + SECRET_KEY
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
if (!succeeded) throw extracted;
|
|
129
|
+
const decrypted = decrypt(extracted, password);
|
|
130
|
+
|
|
131
|
+
console.log(decrypted);
|
|
132
|
+
|
|
133
|
+
if (output) fs.writeFileSync(output, decrypted);
|
|
134
|
+
} catch (e) {
|
|
135
|
+
console.log(e);
|
|
136
|
+
return console.log(chalk.keyword("red")("Could not decrypt"));
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
console.log(chalk.keyword("green")("Successful"));
|
|
140
|
+
}
|
|
141
|
+
)
|
|
142
|
+
|
|
143
|
+
.epilog("copyright 2022")
|
|
144
|
+
.help(true).argv;
|
|
145
|
+
|
|
146
|
+
if (yargs.argv._[0] == null) {
|
|
147
|
+
yargs.showHelp();
|
|
148
|
+
process.exit(0);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/*
|
|
152
|
+
TODO: error out if image is too small
|
|
153
|
+
TODO: implement compression
|
|
154
|
+
TODO: implement streaming
|
|
155
|
+
TODO: file encryption
|
|
156
|
+
|
|
157
|
+
*/
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
const crypto = require("crypto");
|
|
2
|
+
|
|
3
|
+
const cryptoConfig = {
|
|
4
|
+
cipherAlgorithm: "aes-256-gcm",
|
|
5
|
+
iterations: 65535,
|
|
6
|
+
keyLength: 32,
|
|
7
|
+
saltLength: 32,
|
|
8
|
+
nonceLength: 12,
|
|
9
|
+
tagLength: 16,
|
|
10
|
+
digest: "sha512",
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const encrypt = (message, password) => {
|
|
14
|
+
const salt = crypto.randomBytes(cryptoConfig.saltLength);
|
|
15
|
+
const key = crypto.pbkdf2Sync(
|
|
16
|
+
password,
|
|
17
|
+
salt,
|
|
18
|
+
cryptoConfig.iterations,
|
|
19
|
+
cryptoConfig.keyLength,
|
|
20
|
+
cryptoConfig.digest
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
const nonce = crypto.randomBytes(cryptoConfig.nonceLength);
|
|
24
|
+
const cipher = crypto.createCipheriv(
|
|
25
|
+
cryptoConfig.cipherAlgorithm,
|
|
26
|
+
key,
|
|
27
|
+
nonce
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
let encryptedBase64 = "";
|
|
31
|
+
cipher.setEncoding("base64");
|
|
32
|
+
cipher.on("data", (chunk) => (encryptedBase64 += chunk));
|
|
33
|
+
cipher.on("end", () => {
|
|
34
|
+
// do nothing console.log(encryptedBase64);
|
|
35
|
+
// Prints: some clear text data
|
|
36
|
+
});
|
|
37
|
+
cipher.write(message);
|
|
38
|
+
cipher.end();
|
|
39
|
+
|
|
40
|
+
const saltBase64 = base64Encoding(salt);
|
|
41
|
+
const nonceBase64 = base64Encoding(nonce);
|
|
42
|
+
const gcmTagBase64 = base64Encoding(cipher.getAuthTag());
|
|
43
|
+
return (
|
|
44
|
+
saltBase64 + ":" + nonceBase64 + ":" + encryptedBase64 + ":" + gcmTagBase64
|
|
45
|
+
);
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const decrypt = (encrypted, password) => {
|
|
49
|
+
const dataSplit = encrypted.split(":");
|
|
50
|
+
const salt = base64Decoding(dataSplit[0]);
|
|
51
|
+
const nonce = base64Decoding(dataSplit[1]);
|
|
52
|
+
const ciphertext = dataSplit[2];
|
|
53
|
+
const gcmTag = base64Decoding(dataSplit[3]);
|
|
54
|
+
const key = crypto.pbkdf2Sync(
|
|
55
|
+
password,
|
|
56
|
+
salt,
|
|
57
|
+
cryptoConfig.iterations,
|
|
58
|
+
cryptoConfig.keyLength,
|
|
59
|
+
cryptoConfig.digest
|
|
60
|
+
);
|
|
61
|
+
|
|
62
|
+
const decipher = crypto.createDecipheriv(
|
|
63
|
+
cryptoConfig.cipherAlgorithm,
|
|
64
|
+
key,
|
|
65
|
+
nonce
|
|
66
|
+
);
|
|
67
|
+
decipher.setAuthTag(gcmTag);
|
|
68
|
+
|
|
69
|
+
let decrypted = "";
|
|
70
|
+
decipher.on("readable", () => {
|
|
71
|
+
while (null !== (chunk = decipher.read())) {
|
|
72
|
+
decrypted += chunk.toString("utf8");
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
decipher.on("end", () => {
|
|
76
|
+
// do nothing console.log(decrypted);
|
|
77
|
+
});
|
|
78
|
+
decipher.on("error", (err) => {
|
|
79
|
+
throw err.message;
|
|
80
|
+
});
|
|
81
|
+
decipher.write(ciphertext, "base64");
|
|
82
|
+
decipher.end();
|
|
83
|
+
return decrypted;
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
const base64Encoding = (input) => input.toString("base64");
|
|
87
|
+
|
|
88
|
+
const base64Decoding = (input) => Buffer.from(input, "base64");
|
|
89
|
+
|
|
90
|
+
module.exports = { encrypt, decrypt };
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
// Code from https://gist.github.com/banksean/300494 for seeded rand.
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
I've wrapped Makoto Matsumoto and Takuji Nishimura's code in a namespace
|
|
5
|
+
so it's better encapsulated. Now you can have multiple random number generators
|
|
6
|
+
and they won't stomp all over eachother's state.
|
|
7
|
+
|
|
8
|
+
If you want to use this as a substitute for Math.random(), use the random()
|
|
9
|
+
method like so:
|
|
10
|
+
|
|
11
|
+
var m = new MersenneTwister();
|
|
12
|
+
var randomNumber = m.random();
|
|
13
|
+
|
|
14
|
+
You can also call the other genrand_{foo}() methods on the instance.
|
|
15
|
+
If you want to use a specific seed in order to get a repeatable random
|
|
16
|
+
sequence, pass an integer into the constructor:
|
|
17
|
+
var m = new MersenneTwister(123);
|
|
18
|
+
and that will always produce the same random sequence.
|
|
19
|
+
Sean McCullough (banksean@gmail.com)
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
/*
|
|
23
|
+
A C-program for MT19937, with initialization improved 2002/1/26.
|
|
24
|
+
Coded by Takuji Nishimura and Makoto Matsumoto.
|
|
25
|
+
|
|
26
|
+
Before using, initialize the state by using init_genrand(seed)
|
|
27
|
+
or init_by_array(init_key, key_length).
|
|
28
|
+
|
|
29
|
+
Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
|
|
30
|
+
All rights reserved.
|
|
31
|
+
|
|
32
|
+
Redistribution and use in source and binary forms, with or without
|
|
33
|
+
modification, are permitted provided that the following conditions
|
|
34
|
+
are met:
|
|
35
|
+
|
|
36
|
+
1. Redistributions of source code must retain the above copyright
|
|
37
|
+
notice, this list of conditions and the following disclaimer.
|
|
38
|
+
|
|
39
|
+
2. Redistributions in binary form must reproduce the above copyright
|
|
40
|
+
notice, this list of conditions and the following disclaimer in the
|
|
41
|
+
documentation and/or other materials provided with the distribution.
|
|
42
|
+
|
|
43
|
+
3. The names of its contributors may not be used to endorse or promote
|
|
44
|
+
products derived from this software without specific prior written
|
|
45
|
+
permission.
|
|
46
|
+
|
|
47
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
48
|
+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
49
|
+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
50
|
+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
51
|
+
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
52
|
+
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
53
|
+
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
54
|
+
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
55
|
+
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
56
|
+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
57
|
+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
Any feedback is very welcome.
|
|
61
|
+
http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
|
|
62
|
+
email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
|
|
63
|
+
*/
|
|
64
|
+
|
|
65
|
+
var MersenneTwister = function (seed) {
|
|
66
|
+
if (seed == undefined) {
|
|
67
|
+
seed = new Date().getTime();
|
|
68
|
+
}
|
|
69
|
+
/* Period parameters */
|
|
70
|
+
this.N = 624;
|
|
71
|
+
this.M = 397;
|
|
72
|
+
this.MATRIX_A = 0x9908b0df; /* constant vector a */
|
|
73
|
+
this.UPPER_MASK = 0x80000000; /* most significant w-r bits */
|
|
74
|
+
this.LOWER_MASK = 0x7fffffff; /* least significant r bits */
|
|
75
|
+
|
|
76
|
+
this.mt = new Array(this.N); /* the array for the state vector */
|
|
77
|
+
this.mti = this.N + 1; /* mti==N+1 means mt[N] is not initialized */
|
|
78
|
+
|
|
79
|
+
this.init_genrand(seed);
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
/* initializes mt[N] with a seed */
|
|
83
|
+
MersenneTwister.prototype.init_genrand = function (s) {
|
|
84
|
+
this.mt[0] = s >>> 0;
|
|
85
|
+
for (this.mti = 1; this.mti < this.N; this.mti++) {
|
|
86
|
+
var s = this.mt[this.mti - 1] ^ (this.mt[this.mti - 1] >>> 30);
|
|
87
|
+
this.mt[this.mti] =
|
|
88
|
+
((((s & 0xffff0000) >>> 16) * 1812433253) << 16) +
|
|
89
|
+
(s & 0x0000ffff) * 1812433253 +
|
|
90
|
+
this.mti;
|
|
91
|
+
/* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
|
|
92
|
+
/* In the previous versions, MSBs of the seed affect */
|
|
93
|
+
/* only MSBs of the array mt[]. */
|
|
94
|
+
/* 2002/01/09 modified by Makoto Matsumoto */
|
|
95
|
+
this.mt[this.mti] >>>= 0;
|
|
96
|
+
/* for >32 bit machines */
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
/* initialize by an array with array-length */
|
|
101
|
+
/* init_key is the array for initializing keys */
|
|
102
|
+
/* key_length is its length */
|
|
103
|
+
/* slight change for C++, 2004/2/26 */
|
|
104
|
+
MersenneTwister.prototype.init_by_array = function (init_key, key_length) {
|
|
105
|
+
var i, j, k;
|
|
106
|
+
this.init_genrand(19650218);
|
|
107
|
+
i = 1;
|
|
108
|
+
j = 0;
|
|
109
|
+
k = this.N > key_length ? this.N : key_length;
|
|
110
|
+
for (; k; k--) {
|
|
111
|
+
var s = this.mt[i - 1] ^ (this.mt[i - 1] >>> 30);
|
|
112
|
+
this.mt[i] =
|
|
113
|
+
(this.mt[i] ^
|
|
114
|
+
(((((s & 0xffff0000) >>> 16) * 1664525) << 16) +
|
|
115
|
+
(s & 0x0000ffff) * 1664525)) +
|
|
116
|
+
init_key[j] +
|
|
117
|
+
j; /* non linear */
|
|
118
|
+
this.mt[i] >>>= 0; /* for WORDSIZE > 32 machines */
|
|
119
|
+
i++;
|
|
120
|
+
j++;
|
|
121
|
+
if (i >= this.N) {
|
|
122
|
+
this.mt[0] = this.mt[this.N - 1];
|
|
123
|
+
i = 1;
|
|
124
|
+
}
|
|
125
|
+
if (j >= key_length) j = 0;
|
|
126
|
+
}
|
|
127
|
+
for (k = this.N - 1; k; k--) {
|
|
128
|
+
var s = this.mt[i - 1] ^ (this.mt[i - 1] >>> 30);
|
|
129
|
+
this.mt[i] =
|
|
130
|
+
(this.mt[i] ^
|
|
131
|
+
(((((s & 0xffff0000) >>> 16) * 1566083941) << 16) +
|
|
132
|
+
(s & 0x0000ffff) * 1566083941)) -
|
|
133
|
+
i; /* non linear */
|
|
134
|
+
this.mt[i] >>>= 0; /* for WORDSIZE > 32 machines */
|
|
135
|
+
i++;
|
|
136
|
+
if (i >= this.N) {
|
|
137
|
+
this.mt[0] = this.mt[this.N - 1];
|
|
138
|
+
i = 1;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
this.mt[0] = 0x80000000; /* MSB is 1; assuring non-zero initial array */
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
/* generates a random number on [0,0xffffffff]-interval */
|
|
146
|
+
MersenneTwister.prototype.genrand_int32 = function () {
|
|
147
|
+
var y;
|
|
148
|
+
var mag01 = new Array(0x0, this.MATRIX_A);
|
|
149
|
+
/* mag01[x] = x * MATRIX_A for x=0,1 */
|
|
150
|
+
|
|
151
|
+
if (this.mti >= this.N) {
|
|
152
|
+
/* generate N words at one time */
|
|
153
|
+
var kk;
|
|
154
|
+
|
|
155
|
+
if (this.mti == this.N + 1)
|
|
156
|
+
/* if init_genrand() has not been called, */
|
|
157
|
+
this.init_genrand(5489); /* a default initial seed is used */
|
|
158
|
+
|
|
159
|
+
for (kk = 0; kk < this.N - this.M; kk++) {
|
|
160
|
+
y = (this.mt[kk] & this.UPPER_MASK) | (this.mt[kk + 1] & this.LOWER_MASK);
|
|
161
|
+
this.mt[kk] = this.mt[kk + this.M] ^ (y >>> 1) ^ mag01[y & 0x1];
|
|
162
|
+
}
|
|
163
|
+
for (; kk < this.N - 1; kk++) {
|
|
164
|
+
y = (this.mt[kk] & this.UPPER_MASK) | (this.mt[kk + 1] & this.LOWER_MASK);
|
|
165
|
+
this.mt[kk] =
|
|
166
|
+
this.mt[kk + (this.M - this.N)] ^ (y >>> 1) ^ mag01[y & 0x1];
|
|
167
|
+
}
|
|
168
|
+
y =
|
|
169
|
+
(this.mt[this.N - 1] & this.UPPER_MASK) | (this.mt[0] & this.LOWER_MASK);
|
|
170
|
+
this.mt[this.N - 1] = this.mt[this.M - 1] ^ (y >>> 1) ^ mag01[y & 0x1];
|
|
171
|
+
|
|
172
|
+
this.mti = 0;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
y = this.mt[this.mti++];
|
|
176
|
+
|
|
177
|
+
/* Tempering */
|
|
178
|
+
y ^= y >>> 11;
|
|
179
|
+
y ^= (y << 7) & 0x9d2c5680;
|
|
180
|
+
y ^= (y << 15) & 0xefc60000;
|
|
181
|
+
y ^= y >>> 18;
|
|
182
|
+
|
|
183
|
+
return y >>> 0;
|
|
184
|
+
};
|
|
185
|
+
/* These real versions are due to Isaku Wada, 2002/01/09 added */
|
|
186
|
+
|
|
187
|
+
module.exports = { MersenneTwister };
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
const fs = require("fs");
|
|
2
|
+
const { loadImage, createCanvas } = require("canvas");
|
|
3
|
+
const { get_hashed_order, str_to_bits, bits_to_str } = require("./utils");
|
|
4
|
+
|
|
5
|
+
const prepare_write_data = (data_bits, enc_key, encode_len) => {
|
|
6
|
+
const data_bits_len = data_bits.length;
|
|
7
|
+
if (data_bits.length > encode_len) throw "Can not hold this much data!";
|
|
8
|
+
const result = Array(encode_len);
|
|
9
|
+
for (let i = 0; i < encode_len; i++) {
|
|
10
|
+
result[i] = Math.floor(Math.random() * 2); //obfuscation
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const order = get_hashed_order(enc_key, encode_len);
|
|
14
|
+
for (let i = 0; i < data_bits_len; i++) result[order[i]] = data_bits[i];
|
|
15
|
+
|
|
16
|
+
return result;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const prepare_read_data = (data_bits, enc_key) => {
|
|
20
|
+
const data_bits_len = data_bits.length;
|
|
21
|
+
const result = Array(data_bits_len);
|
|
22
|
+
const order = get_hashed_order(enc_key, data_bits_len);
|
|
23
|
+
|
|
24
|
+
for (let i = 0; i < data_bits_len; i++) result[i] = data_bits[order[i]];
|
|
25
|
+
|
|
26
|
+
return result;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const get_bits_lsb = (imgData) => {
|
|
30
|
+
const result = Array();
|
|
31
|
+
for (let i = 0; i < imgData.data.length; i += 4) {
|
|
32
|
+
result.push(imgData.data[i] % 2 == 1 ? 1 : 0);
|
|
33
|
+
result.push(imgData.data[i + 1] % 2 == 1 ? 1 : 0);
|
|
34
|
+
result.push(imgData.data[i + 2] % 2 == 1 ? 1 : 0);
|
|
35
|
+
}
|
|
36
|
+
return result;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const write_lsb = (imgData, setdata) => {
|
|
40
|
+
function unsetbit(k) {
|
|
41
|
+
return k % 2 == 1 ? k - 1 : k;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function setbit(k) {
|
|
45
|
+
return k % 2 == 1 ? k : k + 1;
|
|
46
|
+
}
|
|
47
|
+
let j = 0;
|
|
48
|
+
for (let i = 0; i < imgData.data.length; i += 4) {
|
|
49
|
+
imgData.data[i] = setdata[j]
|
|
50
|
+
? setbit(imgData.data[i])
|
|
51
|
+
: unsetbit(imgData.data[i]);
|
|
52
|
+
imgData.data[i + 1] = setdata[j + 1]
|
|
53
|
+
? setbit(imgData.data[i + 1])
|
|
54
|
+
: unsetbit(imgData.data[i + 1]);
|
|
55
|
+
imgData.data[i + 2] = setdata[j + 2]
|
|
56
|
+
? setbit(imgData.data[i + 2])
|
|
57
|
+
: unsetbit(imgData.data[i + 2]);
|
|
58
|
+
imgData.data[i + 3] = 255;
|
|
59
|
+
j += 3;
|
|
60
|
+
}
|
|
61
|
+
return imgData;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
exports.extractMessageFromImage = async (imagepath, encKey) => {
|
|
65
|
+
let c, ctx, imgData;
|
|
66
|
+
|
|
67
|
+
try {
|
|
68
|
+
const img = await loadImage(imagepath);
|
|
69
|
+
c = createCanvas(img.width, img.height);
|
|
70
|
+
ctx = c.getContext("2d");
|
|
71
|
+
ctx.drawImage(img, 0, 0, img.width, img.height);
|
|
72
|
+
imgData = ctx.getImageData(0, 0, c.width, c.height);
|
|
73
|
+
} catch (err) {
|
|
74
|
+
return [false, err];
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
try {
|
|
78
|
+
const bitsStream = get_bits_lsb(imgData);
|
|
79
|
+
const decryptedBitsStream = prepare_read_data(bitsStream, encKey);
|
|
80
|
+
const msg = bits_to_str(decryptedBitsStream, 1);
|
|
81
|
+
if (msg == null)
|
|
82
|
+
return [
|
|
83
|
+
false,
|
|
84
|
+
"Message does not decrypt. Maybe due to (1) wrong password / enc method. (2) corrupted file",
|
|
85
|
+
];
|
|
86
|
+
return [true, msg];
|
|
87
|
+
} catch (err) {
|
|
88
|
+
return [
|
|
89
|
+
false,
|
|
90
|
+
"Message does not decrypt. Maybe due to (1) wrong password / enc method. (2) corrupted file",
|
|
91
|
+
];
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
exports.encodeMessageToImage = async (imagepath, msg, encKey) => {
|
|
96
|
+
try {
|
|
97
|
+
// const imageBuffer = fs.readFileSync(imagepath);
|
|
98
|
+
|
|
99
|
+
const img = await loadImage(imagepath);
|
|
100
|
+
const c = createCanvas(img.width, img.height);
|
|
101
|
+
const ctx = c.getContext("2d");
|
|
102
|
+
ctx.drawImage(img, 0, 0, img.width, img.height);
|
|
103
|
+
// console.log(c.toDataURL);
|
|
104
|
+
const imgData = ctx.getImageData(0, 0, c.width, c.height);
|
|
105
|
+
const encode_len = Math.floor(imgData.data.length / 4) * 3;
|
|
106
|
+
|
|
107
|
+
// prepare data
|
|
108
|
+
const bitStream = str_to_bits(msg, 1);
|
|
109
|
+
const encryptedBitStream = prepare_write_data(
|
|
110
|
+
bitStream,
|
|
111
|
+
encKey,
|
|
112
|
+
encode_len
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
const encryptedImgData = write_lsb(imgData, encryptedBitStream);
|
|
116
|
+
ctx.putImageData(encryptedImgData, 0, 0);
|
|
117
|
+
const encodedImageBuffer = c.toBuffer("image/png");
|
|
118
|
+
fs.writeFileSync("encoded.png", encodedImageBuffer);
|
|
119
|
+
|
|
120
|
+
return true;
|
|
121
|
+
} catch (err) {
|
|
122
|
+
throw err;
|
|
123
|
+
}
|
|
124
|
+
};
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
const { MersenneTwister } = require("./mersenne-twister");
|
|
2
|
+
const SHA512 = require("crypto-js/sha512");
|
|
3
|
+
|
|
4
|
+
const get_hashed_order = (password, arr_len) => {
|
|
5
|
+
// O(arr_len) algorithm
|
|
6
|
+
const orders = Array.from(Array(arr_len).keys());
|
|
7
|
+
const result = [];
|
|
8
|
+
let loc;
|
|
9
|
+
const seed = SHA512(password).words.reduce(function (total, num) {
|
|
10
|
+
return total + Math.abs(num);
|
|
11
|
+
}, 0);
|
|
12
|
+
const rnd = new MersenneTwister(seed);
|
|
13
|
+
for (let i = arr_len; i > 0; i--) {
|
|
14
|
+
loc = rnd.genrand_int32() % i;
|
|
15
|
+
result.push(orders[loc]);
|
|
16
|
+
orders[loc] = orders[i - 1];
|
|
17
|
+
}
|
|
18
|
+
return result;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const utf8Decode = (bytes) => {
|
|
22
|
+
var chars = [],
|
|
23
|
+
offset = 0,
|
|
24
|
+
length = bytes.length,
|
|
25
|
+
c,
|
|
26
|
+
c2,
|
|
27
|
+
c3;
|
|
28
|
+
|
|
29
|
+
while (offset < length) {
|
|
30
|
+
c = bytes[offset];
|
|
31
|
+
c2 = bytes[offset + 1];
|
|
32
|
+
c3 = bytes[offset + 2];
|
|
33
|
+
|
|
34
|
+
if (128 > c) {
|
|
35
|
+
chars.push(String.fromCharCode(c));
|
|
36
|
+
offset += 1;
|
|
37
|
+
} else if (191 < c && c < 224) {
|
|
38
|
+
chars.push(String.fromCharCode(((c & 31) << 6) | (c2 & 63)));
|
|
39
|
+
offset += 2;
|
|
40
|
+
} else {
|
|
41
|
+
chars.push(
|
|
42
|
+
String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63))
|
|
43
|
+
);
|
|
44
|
+
offset += 3;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return chars.join("");
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const utf8Encode = (str) => {
|
|
52
|
+
var bytes = [],
|
|
53
|
+
offset = 0,
|
|
54
|
+
length,
|
|
55
|
+
char;
|
|
56
|
+
|
|
57
|
+
str = encodeURI(str);
|
|
58
|
+
length = str.length;
|
|
59
|
+
|
|
60
|
+
while (offset < length) {
|
|
61
|
+
char = str[offset];
|
|
62
|
+
offset += 1;
|
|
63
|
+
|
|
64
|
+
if ("%" !== char) {
|
|
65
|
+
bytes.push(char.charCodeAt(0));
|
|
66
|
+
} else {
|
|
67
|
+
char = str[offset] + str[offset + 1];
|
|
68
|
+
bytes.push(parseInt(char, 16));
|
|
69
|
+
offset += 2;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return bytes;
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
const bits_to_str = (bitarray, num_copy) => {
|
|
77
|
+
function merge_bits(bits) {
|
|
78
|
+
var bits_len = bits.length;
|
|
79
|
+
var bits_sum = 0;
|
|
80
|
+
for (var i = 0; i < bits_len; i++) bits_sum += bits[i];
|
|
81
|
+
return Math.round(bits_sum / bits_len);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
var msg_array = Array();
|
|
85
|
+
var data, tmp;
|
|
86
|
+
|
|
87
|
+
var msg_array_len = Math.floor(Math.floor(bitarray.length / num_copy) / 8);
|
|
88
|
+
for (var i = 0; i < msg_array_len; i++) {
|
|
89
|
+
data = 0;
|
|
90
|
+
tmp = 128;
|
|
91
|
+
for (var j = 0; j < 8; j++) {
|
|
92
|
+
data +=
|
|
93
|
+
merge_bits(
|
|
94
|
+
bitarray.slice((i * 8 + j) * num_copy, (i * 8 + j + 1) * num_copy)
|
|
95
|
+
) * tmp;
|
|
96
|
+
tmp = Math.floor(tmp / 2);
|
|
97
|
+
}
|
|
98
|
+
if (data == 255) break; //END NOTATION
|
|
99
|
+
msg_array.push(data);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return utf8Decode(msg_array);
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
const str_to_bits = (str, num_copy) => {
|
|
106
|
+
const utf8array = utf8Encode(str);
|
|
107
|
+
const result = Array();
|
|
108
|
+
const utf8strlen = utf8array.length;
|
|
109
|
+
for (let i = 0; i < utf8strlen; i++) {
|
|
110
|
+
for (let j = 128; j > 0; j = Math.floor(j / 2)) {
|
|
111
|
+
if (Math.floor(utf8array[i] / j)) {
|
|
112
|
+
for (let cp = 0; cp < num_copy; cp++) result.push(1);
|
|
113
|
+
utf8array[i] -= j;
|
|
114
|
+
} else for (let cp = 0; cp < num_copy; cp++) result.push(0);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
for (let j = 0; j < 24; j++)
|
|
118
|
+
for (let i = 0; i < num_copy; i++) {
|
|
119
|
+
result.push(1);
|
|
120
|
+
}
|
|
121
|
+
return result;
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
module.exports = { get_hashed_order, str_to_bits, bits_to_str };
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "obscr",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Encrypt and hide your secure data",
|
|
5
|
+
"main": "bin/index.js",
|
|
6
|
+
"bin": {
|
|
7
|
+
"obscr": "./bin/index.js"
|
|
8
|
+
},
|
|
9
|
+
"scripts": {
|
|
10
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
11
|
+
},
|
|
12
|
+
"publishConfig": {
|
|
13
|
+
"registry": "https://registry.npmjs.org"
|
|
14
|
+
},
|
|
15
|
+
"author": "Johannes Dragulanescu",
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"repository": "github:jdragulanescu/obscr",
|
|
18
|
+
"keywords": [
|
|
19
|
+
"steganography",
|
|
20
|
+
"security",
|
|
21
|
+
"encryption",
|
|
22
|
+
"cryptography",
|
|
23
|
+
"crypto"
|
|
24
|
+
],
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"canvas": "^2.9.3",
|
|
27
|
+
"chalk": "^4.1.2",
|
|
28
|
+
"crypto-js": "^4.1.1",
|
|
29
|
+
"inquirer": "^8.0.0",
|
|
30
|
+
"yargs": "^17.5.1"
|
|
31
|
+
}
|
|
32
|
+
}
|