Berroterán Berroterán - 6 months ago 77
HTML Question

How to properly use encryption with AES?

I have the following code:

<script type="text/javascript" src="jquery-1.12.3.min.js"></script>

<script type="text/javascript" src="core-min.js"></script>

<script type="text/javascript>" src="sha1.js">

</script>
<script type="text/javascript>" src="hmac-sha1.js"></script>
<script type="text/javascript>" src="aes.js"></script>

<div id="divContent"></div>

<script>
$( document ).ready(function() {

try{
var encrypted = CryptoJS.AES.encrypt("Message for Encrypt", "Pass");
var decrypted = CryptoJS.AES.decrypt(encrypted, "Pass");


document.getElementById("divContent").innerHTML = decrypted.toString(CryptoJS.enc.Utf8);
}catch (ex){
console.log(ex.message);
}
});
</script>


And the console gives me this error:


Cannot read property 'encrypt' of undefined


What is wrong in my code?

Answer

Your problem is having an extra >, which won't let read the src.

Instead of:

<script type="text/javascript>" src="sha1.js"></script>
<script type="text/javascript>" src="hmac-sha1.js"></script>
<script type="text/javascript>" src="aes.js"></script>

Should be:

<script type="text/javascript" src="sha1.js"></script>
<script type="text/javascript" src="hmac-sha1.js"></script>
<script type="text/javascript" src="aes.js"></script>

Note the colors from code color

Comments