I'm trying to reproduce the same hmacsha1 hash and base64 encoding from .net membership provider in a javascript function. I've tried using crypto-js and am getting different results. The .net code will hash "test" into "W477AMlLwwJQeAGlPZKiEILr8TA="
Here's the .net code
string password = "test";
HMACSHA1 hash = new HMACSHA1();
hash.Key = Encoding.Unicode.GetBytes(password);
string encodedPassword = Convert.ToBase64String(hash.ComputeHash(Encoding.Unicode.GetBytes(password)));
And here's the javascript method I tried using crypto-js that does not produce the same output
var hash = CryptoJS.HmacSHA1("test", "");
var encodedPassword = CryptoJS.enc.Base64.stringify(hash);
How can I get my javascript hash to match the hash being generated from .net.
//not sure why crypt-js's utf16LE function doesn't give the same result
//words = CryptoJS.enc.Utf16LE.parse("test");
//utf16 = CryptoJS.enc.Utf16LE.stringify("test");
function str2rstr_utf16le(input) {
var output = [],
i = 0,
l = input.length;
for (; l > i; ++i) {
output[i] = String.fromCharCode(
input.charCodeAt(i) & 0xFF,
(input.charCodeAt(i) >>> 8) & 0xFF
);
}
return output.join('');
}
var pwd = str2rstr_utf16le("test");
var hash = CryptoJS.HmacSHA1(pwd, pwd);
var encodedPassword = CryptoJS.enc.Base64.stringify(hash);
alert(encodedPassword);
You don't specify a key in .NET:
var secretKey = "";
var password = "test";
var enc = Encoding.ASCII;
System.Security.Cryptography.HMACSHA1 hmac = new System.Security.Cryptography.HMACSHA1(enc.GetBytes(secretKey));
hmac.Initialize();
byte[] buffer = enc.GetBytes(password);
var encodedPassword = Convert.ToBase64String(hmac.ComputeHash(buffer));
Edit: as #Andreas mentioned, your problem is the encoding. So you just need to replace UTF by ANSI in your own code:
string password = "test";
System.Security.Cryptography.HMACSHA1 hash = new System.Security.Cryptography.HMACSHA1();
hash.Key = Encoding.ASCII.GetBytes("");
string encodedPassword = Convert.ToBase64String(hash.ComputeHash(Encoding.ASCII.GetBytes(password)));
Related
I have the following code for decryption in java, I want that to be implemented in nodejs but the i found many tuto for my problem but they use a salt and that don't work when i try to remove the salt system.
My olds functions from java
public void readLastLogin(File lastLogin) {
try {
final Cipher ciph = this.openCipher(Cipher.DECRYPT_MODE);
final DataInputStream dis = new DataInputStream(new CipherInputStream(new FileInputStream(lastLogin), ciph));
String user = dis.readUTF();
String token = dis.readUTF();
dis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void saveLastLogin(File lastLogin, String user, String token) {
try {
final Cipher ciph = this.openCipher(Cipher.ENCRYPT_MODE);
final DataOutputStream dos = new DataOutputStream(new CipherOutputStream(new FileOutputStream(lastLogin), ciph));
dos.writeUTF(user);
dos.writeUTF(token);
dos.close();
} catch (final Exception e) {
e.printStackTrace();
}
}
private Cipher openCipher(final int mode) throws Exception {
final Random rnd = new Random(43287234L);
final byte[] data = new byte[8];
rnd.nextBytes(data);
final PBEParameterSpec spec = new PBEParameterSpec(data, 5);
final SecretKey key = SecretKeyFactory
.getInstance("PBEWithMD5AndDES")
.generateSecret(new PBEKeySpec("mysecretpassword".toCharArray()));
final Cipher ret = Cipher.getInstance("PBEWithMD5AndDES");
ret.init(mode, key, spec);
return ret;
}
I try this but not working
KDF(password, iterations) {
var key = new Buffer(password,'utf-8');
var i;
for (i = 0; i < iterations; i+=1) {
key = crypto.createHash("md5").update(key).digest();
}
return key;
}
getKeyIV(password, iterations) {
var key = this.KDF(password, iterations);
var keybuf = new Buffer(key,'binary').slice(0,8);
var ivbuf = new Buffer(key,'binary').slice(8,16);
return [ keybuf, ivbuf ];
}
decrypt(message) {
var kiv = this.getKeyIV('password' ,5);
var decipher = crypto.createDecipheriv('des-ede', kiv[0], kiv[1]);
var result = decipher.update(message, 'hex', 'utf-8');
return result + decipher.final('utf-8');
}
Edit: The salt generated at runtime of the Java code is hex encoded: 0x0C9D4AE41E8315FC
final byte[] data = new byte[8];
data[0] = 12;
data[1] = -99;
data[2] = 74;
data[3] = -28;
data[4] = 30;
data[5] = -125;
data[6] = 21;
data[7] = -4;
There are three issues in the code:
The Java code uses writeUTF(). This is based on modified UTF-8, with two additional bytes prepended to contain the data size. A NodeJS library for modified UTF-8 is mutf-8.
Instead of des-ede (3DES/2TDEA in ECB mode), des-cbc (DES in CBC mode) must be applied. If DES is not available in the used NodeJS version, it can be mimicked with 3DES/2TDEA in CBC mode, des-ede-cbc, where the 8 bytes key has to be concatenated with itself to a 16 bytes key, reducing 3DES to DES. This workaround has the disadvantage of a performance loss.
The Java code applies a static 8 byte salt because Random() is instantiated with a static seed. This salt can be determined at runtime to (hex encoded): 0x0C9D4AE41E8315FC.
pbewithmd5anddes-js is a port of PBEWithMD5AndDES to NodeJS, from which the KDF(), getKeyIV() and decrypt() methods can be used, but must be adapted taking into account the above points. In addition, the functionality of readUTF() (the counterpart of writeUTF()) must be implemented. One possible solution is:
var crypto = require('crypto');
const { MUtf8Decoder } = require("mutf-8");
var pbewithmd5anddes = {
KDF: function(password,salt,iterations) {
var pwd = Buffer.from(password,'utf-8');
var key = Buffer.concat([pwd, salt]);
var i;
for (i = 0; i < iterations; i+=1) {
key = crypto.createHash('md5').update(key).digest();
}
return key;
},
getKeyIV: function(password,salt,iterations) {
var key = this.KDF(password,salt,iterations);
var keybuf = Buffer.from(key,'binary').subarray(0,8);
var ivbuf = Buffer.from(key,'binary').subarray(8,16);
return [ keybuf, ivbuf ];
},
decrypt: function(payload,password,salt,iterations) {
var encryptedBuffer = Buffer.from(payload,'base64');
var kiv = this.getKeyIV(password,salt,iterations);
//var decipher = crypto.createDecipheriv('des-cbc', kiv[0], kiv[1]); // Fix 1: If supported, apply DES-CBC with key K
var decipher = crypto.createDecipheriv('des-ede-cbc', Buffer.concat([kiv[0], kiv[0]]), kiv[1]); // otherwise DES-EDE-CBC with key K|K
var decryptedBuf = Buffer.concat([decipher.update(encryptedBuffer), decipher.final()])
var decrypted = this.readUTF(decryptedBuf) // Fix 2: apply writeUTF counterpart
return decrypted;
},
readUTF: function(decryptedBuf) {
var decoder = new MUtf8Decoder()
var decryptedData = []
var i = 0;
while (i < decryptedBuf.length){
var lengthObj = decryptedBuf.readInt16BE(i);
var bytesObj = decryptedBuf.subarray(i+2, i+2+lengthObj);
var strObj = decoder.decode(bytesObj)
decryptedData.push(strObj)
i += 2 + lengthObj;
}
return decryptedData;
}
};
Test:
On the Java side, the following is executed:
saveLastLogin(file, "This is the first plaintext with special characters like §, $ and €.", "This is the second plaintext with special characters like §, $ and €.");
The raw ciphertext written to file is Base64 encoded:
Ow8bdeNM0QpNFQaoDe7dhG3k9nWz/UZ6v3+wQVgrD5QvWR/4+sA+YvqtnQBsy35nQkwhwGRBv1h1eOa587NaFtnJUWVHsRsncLWZ05+dD2rYVpcZRA8s6P2iANK6yLr+GO/+UpZpSe0fA4fFqEK1nm3U7NXdyddfuOlZ3h/RiyxK5819LieUne4F8/TpMzT0RIWkxqagbVw=
This can be decrypted on the NodeJS side with:
var payload = 'Ow8bdeNM0QpNFQaoDe7dhG3k9nWz/UZ6v3+wQVgrD5QvWR/4+sA+YvqtnQBsy35nQkwhwGRBv1h1eOa587NaFtnJUWVHsRsncLWZ05+dD2rYVpcZRA8s6P2iANK6yLr+GO/+UpZpSe0fA4fFqEK1nm3U7NXdyddfuOlZ3h/RiyxK5819LieUne4F8/TpMzT0RIWkxqagbVw=';
var password = 'mysecretpassword';
var salt = Buffer.from('0C9D4AE41E8315FC', 'hex');
var iterations = 5;
var decrypted = pbewithmd5anddes.decrypt(payload,password,salt,iterations);
console.log(decrypted);
with the output:
[
'This is the first plaintext with special characters like §, $ and €.',
'This is the second plaintext with special characters like §, $ and €.'
]
Note that the code contains serious vulnerabilities:
PBEWithMD5AndDES uses a key derivation based on the broken MD5 digest (PBKDF1), and as encryption algorithm the deprecated DES (officially withdrawn almost 20 years ago).
Use of a static salt.
An iteration count of 5 is generally much too small.
Random() (unlike SecureRandom()) is not cryptographically strong. Also note that it is not a good idea to use a PRNG like Random() for deterministic key derivation, as the implementation may change, leading to different results even with the same seed.
I've got a set of functions for encrypting/decrypting on the server (C#), however I'd like to port the decryption piece to Javascript. Here's the C# decryption functions I've used previously in C# with a sample encrypted string, password and IV
I'm having trouble with the JavaScript decryption piece using Cryptojs. The result that comes back is unexpectedly an empty string.
var ciphertext = "0MuDwNoWBFjN/1anszbl0Cxkrwh9ahRwE3c61t7io2c=";
var key = "8beee7ac-42d1-4294-91b8-68cd032cf1e1";
var iv = "9bC_#$/-+%#Kli%1Az=-#qT";
var ciphertextWA = CryptoJS.enc.Hex.parse(ciphertext);
var keyWA = CryptoJS.enc.Utf8.parse(key);
var ivWA = CryptoJS.enc.Utf8.parse(iv);
var ciphertextCP = { ciphertext: ciphertextWA };
var decrypted = CryptoJS.AES.decrypt(
ciphertextCP,
keyWA,
{ iv: ivWA }
);
console.log(decrypted.toString(CryptoJS.enc.Utf8));
The bugs are in the determination of IV and key. Both are derived from passwords using SHA512. In the case of the key the first 32 bytes (bytes 0-31) are used, in the case of the IV the 16 bytes following the first 32 bytes (bytes 32-47).
The fixed code is:
var ciphertext = "0MuDwNoWBFjN/1anszbl0Cxkrwh9ahRwE3c61t7io2c=";
var key = "8beee7ac-42d1-4294-91b8-68cd032cf1e1";
var iv = "9bC_#$/-+%#Kli%1Az=-#qT";
var ciphertextWA = CryptoJS.enc.Base64.parse(ciphertext);
var ciphertextCP = { ciphertext: ciphertextWA };
var keyHashWA = CryptoJS.SHA512(key);
var keyWA = CryptoJS.lib.WordArray.create(keyHashWA.words.slice(0, 32/4));
var ivHashWA = CryptoJS.SHA512(iv);
var ivWA = CryptoJS.lib.WordArray.create(ivHashWA.words.slice(32/4, 48/4));
var decryptedWA = CryptoJS.AES.decrypt(
ciphertextCP,
keyWA,
{ iv: ivWA }
);
console.log(decryptedWA.toString(CryptoJS.enc.Utf8));
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js"></script>
Edit:
As you correctly guessed, the problem is caused by the different encodings (Windows-1252 in the C# code and UTF-8 in the JavaScript code). The fix is to decode in the JavaScript code with Windows-1252.
CryptoJS supports Latin1 (aka ISO-8859-1) but not Windows-1252 (aka Cp1252), s. encoders. The differences between the two encodings are minor, ranging from 0x80 to 0x9F. Just like UTF-8, both correspond to ASCII encoding in the range between 0x00 and 0x7F. Since the original plaintext contained only ASCII characters, there were no problems.
However, the new plaintext now contains byte sequences that are not compatible with UTF-8 (hence the error) or whose characters differ from Latin1 (hence the mismatches).
Since CryptoJS does not support Windows-1252, the decoding must be done with JavaScript on-board means or another library. One possibility is to convert the CryptoJS type WordArray to the more general JavaScript type Uint8Array. Then, using TextDecoder(), which supports a large repertoire of encodings, decoding can be performed using Windows-1252:
var ciphertext = `t1LqNm8PMmfX92KSgiwPdxC6ueGCIaNxN2+gUZYewe34XswHHOge0fdNAttbC+5GWxZxDh+DFBnTTlwnKKGhRvlSdN8wOInec5kXA43XLLaGBwDikSO3WWyz4kjk4mNVRkJfVCwDCW39ZHkDEvroHab3Ye32GZHxEtbXwTh6+3hJ1TudOrSiIwJBUd+brUUk752GG7stLk/HdpTIWcZgVB0UR8VQe93vBkP8SuV7ltl4rTA5MHciomPYgRtJI/yc1pz3v/p6fSh5Mt6hWe7ObIfbsF7KHNrgAyw2VhQ/7lUagJA0XsJ4RR2jwkK1FQhRMUODd7TWoFEVr8wjEA8o3B5OgIfYziKPIWvOyoxX+QntRohZJ7IB7hsHJAVvyFL5NSG+DAuZWHq7vvuOnc9kYWZTgDdKwEElYXWRIxBAyKxdiTE5xxYZg1D/LW4bc1/y/Yl0unQA3C8fLlPElzVdLSVXLpCOOl+7lZjQAb5VPdhO9kbxT9oqmB3bzn9vKq5UcWJYXD+8nt1zRPQl3bQ3FYg1/WMkG08b9X8C6O/VvITyeWasS09pPNhL5ROw4XFrUVdkkfnXzRohbtrWpLZ3R0ZeCg5RWJbykHDh7hBWoNTw9Jw97snLs4bmokm75RrQeEHQcLw6iHnzbjYzGWqjGmQmSthsjBXMVXYku1VddNnoebWLWjaTw+hYertnwXLcvnCDq3lNJ7zOkvFF+vZ1SuoCjNgXdopKIDtmRhugcWWei7jdCWg4K/rD7reApwU1eAO8YxEe/quMQO4YHHV+zUJ5MLwTjDYsDnV4cxqP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kT2oPNiBAtJPy3lcbe8agWrulOfjm/B2kOMtxWAx9ONReIc2uElOliSSseB25RGxuhwgQGPk1Zzz+DGcmRXISFRvp/wRzRXAiDE54rjjykoO/DgKoo1WM+Zf7liezDIcQMFY5uJKrl`;
var key = "55790c03-4a4b-49f7-b7fe-d178454d88f0";
var iv = "9bC_#$/-+%#Kli%1Az=-#qT";
var ciphertextWA = CryptoJS.enc.Base64.parse(ciphertext);
var ciphertextCP = { ciphertext: ciphertextWA };
var keyHashWA = CryptoJS.SHA512(key);
var keyWA = CryptoJS.lib.WordArray.create(keyHashWA.words.slice(0, 32/4));
var ivHashWA = CryptoJS.SHA512(iv);
var ivWA = CryptoJS.lib.WordArray.create(ivHashWA.words.slice(32/4, 48/4));
var decryptedWA = CryptoJS.AES.decrypt(
ciphertextCP,
keyWA,
{ iv: ivWA }
);
function convertWordArrayToUint8Array(wordArray) {
var arrayOfWords = wordArray.hasOwnProperty("words") ? wordArray.words : [];
var length = wordArray.hasOwnProperty("sigBytes") ? wordArray.sigBytes : arrayOfWords.length * 4;
var uInt8Array = new Uint8Array(length), index=0, word, i;
for (i=0; i<length; i++) {
word = arrayOfWords[i];
uInt8Array[index++] = word >> 24;
uInt8Array[index++] = (word >> 16) & 0xff;
uInt8Array[index++] = (word >> 8) & 0xff;
uInt8Array[index++] = word & 0xff;
}
return uInt8Array;
}
var decryptedUint8Array = convertWordArrayToUint8Array(decryptedWA);
var decrypted = new TextDecoder('windows-1252').decode(decryptedUint8Array);
console.log(decrypted);
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js"></script>
I'm trying to generate a UserID out of userAgent and Date Function. I also wanted to understand callback function (which I still didn't get (JS Noob)). Therefore I built the following example:
var storage = window.localStorage;
var storageUserId = storage.getItem("userID");
var text = "";
function userID(callback){
var agent = window.navigator.userAgent;
var now = new Date();
try{
callback(agent, now);
}catch(e){}
}
function hasher(agent,now){
var hash = 0;
var arr = [];
for(var i = 0; i < arguments.length; i++){
var pars = arguments[i];
for(var j = 0; j < pars.length; j++){
var charI = pars.charCodeAt(j);
hash = ((hash<<5)-hash)+charI;
hash = hash & hash; // Convert to 32bit integer
hash = hash.toString();
}
}
console.log(hash + "|" + hash);
}
userID(hasher);
The result should look like this "9834917834|8293479273" (example numbers to show format). First number hashed agent second number hashed date. I got the hash logic form here: http://mediocredeveloper.com/wp/?p=55
Maybe there is a better way to do this :)
I really appreciate your help!
Thanks a lot!
Best,
Anton
You should extract the hashing loop into a new function:
function hash(str){
var hash = 0;
for(const char of str){
const charCode = char.charCodeAt(0);
hash = ((hash<<5)-hash)+charCode;
}
hash = hash & hash; // Convert to 32bit integer
return hash.toString();
}
So to get the hash you want to you just need to call it twice:
function getUserID(){
return hash(window.navigator.userAgent) + "|" + hash("" + new Date);
}
(PS: you know that new Date will change every millisecond?)
I would like to convert byte.Parse in c# to javascript .. but i am not sure how.
byte[] binarySaltValue = new byte[SaltValueSize];
binarySaltValue[0] = byte.Parse(saltValue.Substring(0, 2), System.Globalization.NumberStyles.HexNumber, CultureInfo.InvariantCulture.NumberFormat);
I need to get the ascII code of the substring(0,2) ... did i am right ?
EDIT QUESTION
The real think i try to do is to convert into Javascript the HashPassword method from Microsoft.
private static string HashPassword(string clearData, string saltValue, HashAlgorithm hash)
{
UnicodeEncoding encoding = new UnicodeEncoding();
if (clearData != null && hash != null && encoding != null)
{
// If the salt string is null or the length is invalid then
// create a new valid salt value.
// Convert the salt string and the password string to a single
// array of bytes. Note that the password string is Unicode and
// therefore may or may not have a zero in every other byte.
byte[] binarySaltValue = new byte[SaltValueSize];
binarySaltValue[0] = byte.Parse(saltValue.Substring(0, 2), System.Globalization.NumberStyles.HexNumber, CultureInfo.InvariantCulture.NumberFormat);
binarySaltValue[1] = byte.Parse(saltValue.Substring(2, 2), System.Globalization.NumberStyles.HexNumber, CultureInfo.InvariantCulture.NumberFormat);
binarySaltValue[2] = byte.Parse(saltValue.Substring(4, 2), System.Globalization.NumberStyles.HexNumber, CultureInfo.InvariantCulture.NumberFormat);
binarySaltValue[3] = byte.Parse(saltValue.Substring(6, 2), System.Globalization.NumberStyles.HexNumber, CultureInfo.InvariantCulture.NumberFormat);
byte[] valueToHash = new byte[SaltValueSize + encoding.GetByteCount(clearData)];
byte[] binaryPassword = encoding.GetBytes(clearData);
// Copy the salt value and the password to the hash buffer.
binarySaltValue.CopyTo(valueToHash, 0);
binaryPassword.CopyTo(valueToHash, SaltValueSize);
byte[] hashValue = hash.ComputeHash(valueToHash);
// The hashed password is the salt plus the hash value (as a string).
string hashedPassword = saltValue;
foreach (byte hexdigit in hashValue)
{
hashedPassword += hexdigit.ToString("X2", CultureInfo.InvariantCulture.NumberFormat);
}
// Return the hashed password as a string.
return hashedPassword;
}
return null;
}
Ok so there is my solution, maybe it's not optimized but working fine!
function Salt(plainText, salt) {
if (plainText != undefined && plainText != "" && salt != null) {
// If the salt string is null or the length is invalid then
// create a new valid salt value.
// Convert the salt string and the password string to a single
// array of bytes. Note that the password string is Unicode and
// therefore may or may not have a zero in every other byte.
var binarySaltValue = new Array();
binarySaltValue[0] = parseInt(salt.substring(0, 2), 16);
binarySaltValue[1] = parseInt(salt.substring(2, 4), 16);
binarySaltValue[2] = parseInt(salt.substring(4, 6), 16);
binarySaltValue[3] = parseInt(salt.substring(6, 8), 16);
var binaryPassword = cryptoHelper.stringToBytes(plainText);
var valueToHash = new Array();
// Copy the salt value and the password to the hash buffer.
// binarySaltValue.concat(valueToHash);
// binarySaltValue.CopyTo(valueToHash, 0);
// binaryPassword.CopyTo(valueToHash, SaltValueSize);
valueToHash = valueToHash.concat(binarySaltValue);
// Détermine la longeur utilisé par le array - 2048
var newLengthArray = 2048 - valueToHash.length;
var tampon = new Array(newLengthArray);
for (var i = 0; i < tampon.length; i++) {
tampon[i] = 0;
}
valueToHash = valueToHash.concat(tampon);
valueToHash = valueToHash.concat(binaryPassword);
var hashValue = CryptoJS.MD5(CryptoJS.lib.Base.ByteArray(valueToHash));
var arr = wordArrayToByteArray(hashValue);
var hashedPassword = salt;
for (var i = 0; i < arr.length; i++) {
hexdigit = arr[i];
var hexValue = hexdigit.toString(16);
if (hexValue.length < 2) hexValue = "0" + hexValue;
hashedPassword += hexValue;
}
return hashedPassword;
}
return null;
}
I have 2 strings with some chars. One of them is with "mashed" characters, and the other one is with ordered characters which have some sense. For example:
wvEr2JmJUs2JRr:7Fob9WIB8mSOA?w0s2E:7-f/-G/N-.f7jN:Mi:.CDfGX7tn!
Identification: zpE?bkHlfYS-hIDate: 07/08/2057 12:34:56.789 CGT
So as you may see - the first one have equivalent of symbols which are the same for the equal symbol in the second one.
And the task is - to create somehow kind of alphabet from them, because I have third one string wich have to be "decoded". (wvEr2JmJUs2JRr:7a1AJvvHvAmRRWsxWsFAvJvAJAaoE88A2?s2AxJ1?290s2E:7-f/-G/N-.f7jN:MC:ifDCGN7tn!).
And the tricky part here is - that if I'm pretty sure for the first two strings that they're absolutely equal like a number of chars, so about the new one - is completely different number of symbols, but they consisting in the "alphabet"
And here is my current code for creation of the "alphabet":
var enc = "wvEr2JmJUs2JRr:7Fob9WIB8mSOA?w0s2E:7-f/-G/N-.f7jN:Mi:.CDfGX7tn!";
var dec = "Identification: zpE?bkHlfYS-hIDate: 07/08/2057 12:34:56.789 CGT";
var newenc = "wvEr2JmJUs2JRr:7a1AJvvHvAmRRWsxWsFAvJvAJAaoE88A2?s2AxJ1?290s2E:7-f/-G/N-.f7jN:MC:ifDCGN7tn!";
var myenc = {};
var mynewenc = {};
for (i = 0; i < enc.length; i+=1) {
var encoded = new Array(enc[i]);
var decoded = new Array(dec[i]);
myenc[enc[i]] = dec[i];
};
console.log(myenc);
And now - how I have to decode, the new one string, using this "alphabet"?
var enc = "wvEr2JmJUs2JRr:7Fob9WIB8mSOA?w0s2E:7-f/-G/N-.f7jN:Mi:.CDfGX7tn!";
var dec = "Identification: zpE?bkHlfYS-hIDate: 07/08/2057 12:34:56.789 CGT";
var newenc = "wvEr2JmJUs2JRr:7a1AJvvHvAmRRWsxWsFAvJvAJAaoE88A2?s2AxJ1?290s2E:7-f/-G/N-.f7jN:MC:ifDCGN7tn!";
function make_dictionary(enc, dec){
o = new Object();
if(enc.length == dec.length){
for(i=0; i<enc.length; i++){
o[enc[i]] = dec[i];
}
}
else{
console.log('error');
}
return o;
}
function translate(newenc, dictionary, fill){
var newstring = '';
for(i=0; i<newenc.length; i++){
if(typeof dictionary[newenc[i]] !== 'undefined'){
newstring += dictionary[newenc[i]];
}
else{
newstring += fill;
}
}
return newstring;
}
var d = make_dictionary(enc, dec);
console.log(d);
var string = translate(enc, d, '_');
console.log(string);
var string = translate(newenc, d, '_');
console.log(string);
Here's one way that you can approach it (as I understand the question):
// Create your dictionary
var dict = {};
var enc = "wvEr2JmJUs2JRr:7Fob9WIB8mSOA?w0s2E:7-f/-G/N-.f7jN:Mi:.CDfGX7tn!".split('');
var dec = "Identification: zpE?bkHlfYS-hIDate: 07/08/2057 12:34:56.789 CGT".split('');
// Populate your dictionary
for (var i = 0; i < enc.length; i++) {
dict[enc[i]] = dec[i];
}
// You can use your dictionary like this
var newenc = "wvEr2JmJUs2JRr:7a1AJvvHvAmRRWsxWsFAvJvAJAaoE88A2?s2AxJ1?290s2E:7-f/-G/N-.f7jN:MC:ifDCGN7tn!".split('');
// Returns your translated string
newenc.map(function(e) {
return dict[e];
}).join('');
However for this method you'll have to deal with characters that are defined in newenc that are not defined in your enc (such as T). I tried to do the best I could given the situation and rules that you've described, hope this helps!
If I understand well, you can try using this code.
It is finding the appropriate encoded letter in your enc variable and if the letter is found, it is replacing it with the corresponding letter from your dec variable.
var enc = "wvEr2JmJUs2JRr:7Fob9WIB8mSOA?w0s2E:7-f/-G/N-.f7jN:Mi:.CDfGX7tn!";
var dec = "Identification: zpE?bkHlfYS-hIDate: 07/08/2057 12:34:56.789 CGT";
var newenc = "wvEr2JmJUs2JRr:7a1AJvvHvAmRRWsxWsFAvJvAJAaoE88A2?s2AxJ1?290s2E:7-f/-G/N-.f7jN:MC:ifDCGN7tn!";
for (i = 0; i < newenc.length; i++) {
for (j = 0; j < enc.length; j++) {
if (enc[j] == newenc[i])
newenc[i] = dec[j];
}
};
console.log(newenc);
At the end your variable newenc may contain your decoded string.