-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Import keys with the decryption examples
- Loading branch information
1 parent
f2467e2
commit 21a21a7
Showing
2 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { crypto } from "k6/x/webcrypto"; | ||
import { b64decode } from "k6/encoding"; | ||
|
||
export default async function () { | ||
// transmitted data is the base64 of the initialization vector + encrypted data | ||
// that unusually transmitted over the network | ||
const transmittedData = base64Decode( | ||
"drCfxl4O+5FcrHe8Bs0CvKlw3gZpv+S5if3zn7c4BJzHJ35QDFV4sJB0pbDT" | ||
); | ||
|
||
// keyData is the key used to decrypt the data, that is usually stored in a secure location | ||
// for the purpose of this example, we are using a static key | ||
const jwkKeyData = { | ||
kty: "oct", | ||
ext: true, | ||
key_ops: ["decrypt", "encrypt"], | ||
alg: "A256GCM", | ||
k: "9Id_8iG6FkGOWmc1S203vGVnTExtpDGxdQN7v7OV9Uc", | ||
}; | ||
|
||
try { | ||
const result = await decrypt(jwkKeyData, transmittedData); | ||
|
||
// should output decrypted message | ||
// INFO[0000] result: 'my secret message' source=console | ||
console.log("result: '" + result + "'"); | ||
} catch (e) { | ||
console.log("Error: " + JSON.stringify(e)); | ||
} | ||
} | ||
|
||
const decrypt = async (keyData, transmittedData) => { | ||
const initializeVectorLength = 12; | ||
|
||
// the first 12 bytes are the initialization vector | ||
const iv = new Uint8Array( | ||
transmittedData.subarray(0, initializeVectorLength) | ||
); | ||
|
||
// the rest of the transmitted data is the encrypted data | ||
const encryptedData = new Uint8Array( | ||
transmittedData.subarray(initializeVectorLength) | ||
); | ||
|
||
const importedKey = await crypto.subtle.importKey( | ||
"jwk", | ||
keyData, | ||
{ name: "AES-GCM", length: 256 }, | ||
true, | ||
["encrypt", "decrypt"] | ||
); | ||
|
||
const plain = await crypto.subtle.decrypt( | ||
{ name: "AES-GCM", iv: iv }, | ||
importedKey, | ||
encryptedData | ||
); | ||
|
||
return arrayBufferToString(plain); | ||
}; | ||
|
||
const arrayBufferToString = (buffer) => { | ||
return String.fromCharCode.apply(null, new Uint8Array(buffer)); | ||
}; | ||
|
||
const base64Decode = (base64String) => { | ||
return new Uint8Array(b64decode(base64String)); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { crypto } from "k6/x/webcrypto"; | ||
import { b64decode } from "k6/encoding"; | ||
|
||
export default async function () { | ||
// transmitted data is the base64 of the initialization vector + encrypted data | ||
// that unusually transmitted over the network | ||
const transmittedData = base64Decode( | ||
"whzEN310mrlWIH/icf0dMquRZ2ENyfOzkvPuu92WR/9F8dbeFM8EGUVNIhaS" | ||
); | ||
|
||
// keyData is the key used to decrypt the data, that is usually stored in a secure location | ||
// for the purpose of this example, we are using a static key | ||
const keyData = new Uint8Array([ | ||
109, 151, 76, 33, 232, 253, 176, 90, 94, 40, 146, 227, 139, 208, 245, 139, | ||
69, 215, 55, 197, 43, 122, 160, 178, 228, 104, 4, 115, 138, 159, 119, 49, | ||
]); | ||
|
||
try { | ||
const result = await decrypt(keyData, transmittedData); | ||
|
||
// should output decrypted message | ||
// INFO[0000] result: 'my secret message' source=console | ||
console.log("result: '" + result + "'"); | ||
} catch (e) { | ||
console.log("Error: " + JSON.stringify(e)); | ||
} | ||
} | ||
|
||
const decrypt = async (keyData, transmittedData) => { | ||
const initializeVectorLength = 12; | ||
|
||
// the first 12 bytes are the initialization vector | ||
const iv = new Uint8Array( | ||
transmittedData.subarray(0, initializeVectorLength) | ||
); | ||
|
||
// the rest of the transmitted data is the encrypted data | ||
const encryptedData = new Uint8Array( | ||
transmittedData.subarray(initializeVectorLength) | ||
); | ||
|
||
const importedKey = await crypto.subtle.importKey( | ||
"raw", | ||
keyData, | ||
{ name: "AES-GCM", length: "256" }, | ||
true, | ||
["decrypt"] | ||
); | ||
|
||
const plain = await crypto.subtle.decrypt( | ||
{ name: "AES-GCM", iv: iv }, | ||
importedKey, | ||
encryptedData | ||
); | ||
|
||
return arrayBufferToString(plain); | ||
}; | ||
|
||
const arrayBufferToString = (buffer) => { | ||
return String.fromCharCode.apply(null, new Uint8Array(buffer)); | ||
}; | ||
|
||
const base64Decode = (base64String) => { | ||
return new Uint8Array(b64decode(base64String)); | ||
}; |