Skip to content

Commit 869dc8d

Browse files
committed
successful decryption!
1 parent 4b66e7a commit 869dc8d

1 file changed

Lines changed: 14 additions & 12 deletions

File tree

packages/xforms-engine/src/lib/client-reactivity/instance-state/prepareInstancePayload.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,19 @@ class InstanceFile extends File implements ClientInstanceFile {
4444
}
4545

4646
const generateSymmetricKey = () => {
47-
return 'tÎ,2Z\x02¢à\x95\x16!íÒ|\x07ð«\x91yHRz1\x13µZï\x95³Iµ»';
48-
// return crypto.getRandomValues(new Uint8Array(32)).toString();
47+
// return 'tÎ,2Z\x02¢à\x95\x16!íÒ|\x07ð«\x91yHRz1\x13µZï\x95³Iµ»';
48+
const bytes = new Uint8Array(32);
49+
crypto.getRandomValues(bytes);
50+
return bytes;
4951
};
5052

51-
async function encryptContent(content:string, symmetricKey:string, seed:any): Promise<Uint8Array<ArrayBuffer>> {
52-
console.log('encrypted 7');
53+
async function encryptContent(content:string, symmetricKey:Uint8Array<ArrayBuffer>, seed:any): Promise<Uint8Array<ArrayBuffer>> {
54+
console.log('encrypted 8');
5355
const ivString = seed.getIncrementedSeedByteString();
5456
console.log('iv', ivString);
55-
const key = CryptoJS.enc.Latin1.parse(symmetricKey);
57+
// const key = CryptoJS.enc.Latin1.parse(symmetricKey);
5658
const iv = CryptoJS.enc.Latin1.parse(ivString);
57-
59+
const key = CryptoJS.lib.WordArray.create(symmetricKey);
5860
const encrypted = CryptoJS.AES.encrypt(content, key, {
5961
iv: iv,
6062
mode: CryptoJS.mode.CFB,
@@ -92,7 +94,7 @@ async function encryptContent(content:string, symmetricKey:Uint8Array<ArrayBuffe
9294
// https://github.com/enketo/enketo/blob/2aab5ce716effe038fcc66041e4f16dbb908f26d/packages/enketo-express/public/js/src/module/encryptor.js#L99
9395
// https://github.com/getodk/collect/blob/master/collect_app/src/main/java/org/odk/collect/android/utilities/EncryptionUtils.java
9496

95-
const encrypt = async (symmetricKey:string, data: string, seed:any) => {
97+
const encrypt = async (symmetricKey:Uint8Array<ArrayBuffer>, data: string, seed:any) => {
9698

9799

98100
try {
@@ -112,14 +114,13 @@ const encrypt = async (symmetricKey:string, data: string, seed:any) => {
112114
}
113115

114116
// Equivalent to "RSA/NONE/OAEPWithSHA256AndMGF1Padding"
115-
const rsaEncrypt = async (symmetricKey:string, publicKey:CryptoKey) => {
116-
var enc = new TextEncoder();
117+
const rsaEncrypt = async (symmetricKey:Uint8Array<ArrayBuffer>, publicKey:CryptoKey) => {
117118
const encrypted = await crypto.subtle.encrypt(
118119
{
119120
name: ASYMMETRIC_ALGORITHM,
120121
},
121122
publicKey,
122-
enc.encode(symmetricKey)
123+
symmetricKey
123124
);
124125
return btoa(String.fromCharCode(...new Uint8Array(encrypted)));
125126
// const decoder = new TextDecoder('utf-8');
@@ -153,11 +154,12 @@ function fromWordArray(wordArray: CryptoJS.lib.WordArray) {
153154
}
154155

155156
// TODO type the seed!
156-
function createSeed(instanceId:string, symmetricKey: string) {
157+
function createSeed(instanceId:string, symmetricKey: Uint8Array<ArrayBuffer>) {
157158
// iv is the 16-byte md5 hash of the instanceID and the symmetric key
159+
const key = CryptoJS.lib.WordArray.create(symmetricKey);
158160
const md = CryptoJS.algo.MD5.create();
159161
md.update(instanceId)
160-
md.update(CryptoJS.enc.Latin1.parse(symmetricKey));
162+
md.update(key);
161163
const ivSeedArray = fromWordArray(md.finalize());
162164
let ivCounter = 0;
163165

0 commit comments

Comments
 (0)