Skip to content

Instantly share code, notes, and snippets.

@thEpisode
Last active June 13, 2021 10:25
Show Gist options
  • Save thEpisode/9ae0f4f59eb26649104009aebe5651ff to your computer and use it in GitHub Desktop.
Save thEpisode/9ae0f4f59eb26649104009aebe5651ff to your computer and use it in GitHub Desktop.
Basic usage of AES with Output Feedback
/// Use public CDN in your frontend <script type="text/javascript" src="https://cdn.rawgit.com/ricmoo/aes-js/e27b99df/index.js"></script>
// Convert it to base 36 (numbers + letters), and grab the first 9 characters
// after the decimal.
var randomStringGenerator = function (length, prefix) {
return (prefix == undefined ? 'key-' : prefix) + Math.random().toString(36).substr(2, (length == undefined ? 5 : length));
}
// Get ASCII numbers from a string as array
var stringToAscii = function(input){
var result = [];
for (var key in input) {
if (input.hasOwnProperty(key)) {
result.push(input[key].charCodeAt());
}
}
return result;
}
var keyGenerator = function(){
return stringToAscii(randomStringGenerator(12)).join(':');
}
var ivGenerator = function(){
return stringToAscii(randomStringGenerator(10, 'keyiv-')).join(':');
}
var encrypt = function (cipherText){
var textBytes = aesjs.utils.utf8.toBytes(cipherText);
var aesOfb = new aesjs.ModeOfOperation.ofb(key, iv);
var encryptedBytes = aesOfb.encrypt(textBytes);
return aesjs.utils.hex.fromBytes(encryptedBytes);
}
var decrypt = function(mesage){
var encryptedBytes = aesjs.utils.hex.toBytes(message);
var aesOfb = new aesjs.ModeOfOperation.ofb(key, iv);
var decryptedBytes = aesOfb.decrypt(encryptedBytes);
return aesjs.utils.utf8.fromBytes(decryptedBytes);
}
var key = '';
var iv = '';
$(document).ready(function () {
key = keyGenerator();
iv = ivGenerator();
console.log(`Your Key: ${key}`);
console.log(`Your IV: ${iv}`);
do {
key = prompt('Insert your key:');
} while (key == null);
do {
iv = prompt('Insert your IV:');
} while (iv == null);
// Get an array from given strings
key = key.split(':').map(function (value) { return parseInt(value) });
iv = iv.split(':').map(function (value) { return parseInt(value) });
});
@crux
Copy link

crux commented Jun 13, 2021

why did you miss the 's' in line 35? That decrypt function would not work.

35: var decrypt = function(mes age){

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment