This repository has been archived by the owner on Aug 15, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 159
/
example-js.html
38 lines (37 loc) · 1.52 KB
/
example-js.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CryptoJS Aes PHP</title>
<script src="cryptojs-aes.min.js"></script>
<script src="cryptojs-aes-format.js"></script>
</head>
<body>
<div id="results">
</div>
<script>
(function () {
// encrypt value
let url = new URL(window.location.href)
let results = document.getElementById('results')
let valueToEncrypt = 'Coming from JS - foobar' // this could also be object/array/whatever
let password = '123456'
let encrypted = CryptoJSAesJson.encrypt(valueToEncrypt, password)
results.innerHTML += 'Encrypted:<br/><a href="example-php.php?encrypted=' + btoa(encrypted) + '" title="Pass to PHP testpage" target="_blank">' + encrypted + '</a><br/><br/>'
// something like: {"ct":"10MOxNzbZ7vqR3YEoOhKMg==","iv":"9700d78e12910b5cccd07304333102b7","s":"c6b0b7a3dc072248"}
})()
</script>
<script>
(function () {
let url = new URL(window.location.href)
let results = document.getElementById('results')
// decrypt value
let encrypted = url.searchParams.get('encrypted') ? atob(url.searchParams.get('encrypted')) : '{"ct":"hQDvpbAKTGp1mXgzSShR9g==","iv":"57fd85773d898d1f9f868c53b436e28f","s":"a2dac436512077c5"}'
let password = '123456'
let decrypted = CryptoJSAesJson.decrypt(encrypted, password)
results.innerHTML += 'Decrypted (From ' + encrypted.replace(/</g, '<').replace(/>/g, '>') + '):<br/>'
results.innerHTML += decrypted.replace(/</g, '<').replace(/>/g, '>')
})()
</script>
</body>
</html>