Created
January 11, 2024 13:42
-
-
Save maikyguanaes/4e65b9757f0d94e89a8f78d4106a7cd8 to your computer and use it in GitHub Desktop.
Encryption Decryption Javascript and Ruby
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
// 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 |
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
### 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