Skip to content

Instantly share code, notes, and snippets.

@agungyuliaji
Created August 20, 2024 09:46
Show Gist options
  • Save agungyuliaji/3db108254e215c2b3712dbe47fa7a866 to your computer and use it in GitHub Desktop.
Save agungyuliaji/3db108254e215c2b3712dbe47fa7a866 to your computer and use it in GitHub Desktop.
Encryption Decryption Javascript and Ruby
// IDEA FROM: https://stackoverflow.com/questions/33929712/crypto-in-nodejs-and-ruby
var crypto = require('crypto'),
algorithm = 'aes-256-cbc',
key = 'SOME_RANDOM_KEY_32_CHR_123456789', // 32 Characters
iv = "0000000000000000"; // 16 Characters
function encrypt(text){
var cipher = crypto.createCipheriv(algorithm,key,iv)
var crypted = cipher.update(text,'utf-8',"base64")
crypted += cipher.final("base64");
return crypted;
}
function decrypt(text) {
var cipher = crypto.createDecipheriv(algorithm,key,iv)
var crypted = cipher.update(text, "base64", "utf-8")
crypted += cipher.final("utf-8");
return crypted;
}
console.log(encrypt("1")); // return KAZL9qez4T7DblhCESPyPA==
console.log(decrypt("KAZL9qez4T7DblhCESPyPA==")); // return 1
### IDEA FROM: https://stackoverflow.com/questions/33929712/crypto-in-nodejs-and-ruby
require 'openssl'
require 'base64'
$alg = 'aes-256-cbc'
$key = 'SOME_RANDOM_KEY_32_CHR_123456789' ## 32 Characters
$iv = '0000000000000000' ## 16 characters
def encrypt(des_text)
des = OpenSSL::Cipher::Cipher.new($alg)
des.encrypt
des.key = $key
des.iv = $iv
result = des.update(des_text)
result << des.final
return Base64.encode64 result
end
def decrypt(des_text)
des = OpenSSL::Cipher::Cipher.new($alg)
des.decrypt
des.key = $key
des.iv = $iv
result = des.update(Base64.decode64(des_text))
result << des.final
return result
end
p encrypt("1").strip # return KAZL9qez4T7DblhCESPyPA==
p decrypt("KAZL9qez4T7DblhCESPyPA==").strip # return 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment