最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cipher a string using crypto-js with hex encoding to make it url friendly - Stack Overflow

programmeradmin3浏览0评论

I am using crypto-js by brix. I have this function below that handles the encryption of a plain text.

import CryptoJS from 'crypto-js'
import AES from 'crypto-js/aes'

const SECRET = 'I am batman'
const plainText = 'This is Sparta!'

export function enc(plainText){
    // returns something like this U2FsdGVkX184He5Rp991JYAiCSdTwfZs8T3kJUk3zAc=  
    // but with random `/` and I dont want that
    // I want it to be Hex but .toString(CryptoJs.enc.Hex) 
    // is not working, it just returns an '' empty string
    // it's a string, I checked using typeof
    return AES.encrypt(plainText, SECRET).toString();
}

How do I make the enc(string) to return a Hex value which is url friendly?

I am using crypto-js by brix. I have this function below that handles the encryption of a plain text.

import CryptoJS from 'crypto-js'
import AES from 'crypto-js/aes'

const SECRET = 'I am batman'
const plainText = 'This is Sparta!'

export function enc(plainText){
    // returns something like this U2FsdGVkX184He5Rp991JYAiCSdTwfZs8T3kJUk3zAc=  
    // but with random `/` and I dont want that
    // I want it to be Hex but .toString(CryptoJs.enc.Hex) 
    // is not working, it just returns an '' empty string
    // it's a string, I checked using typeof
    return AES.encrypt(plainText, SECRET).toString();
}

How do I make the enc(string) to return a Hex value which is url friendly?

Share Improve this question edited Jun 23, 2017 at 7:37 jofftiquez asked Jun 23, 2017 at 6:36 jofftiquezjofftiquez 7,70810 gold badges70 silver badges122 bronze badges 5
  • If you have trouble with / why don't you use the URL-safe Base64 encoding? – Artjom B. Commented Jun 23, 2017 at 18:32
  • Hi @ArtjomB. Yes but, .toString(CryptoJs.enc.Base64) isnt working on AES.encrypt() – jofftiquez Commented Jun 24, 2017 at 1:57
  • It's already Base64-encoded. You just need to replace some characters (see this). – Artjom B. Commented Jun 24, 2017 at 11:46
  • @ArtjomB. replacing characters will affect the data integrity. – jofftiquez Commented Jun 26, 2017 at 1:00
  • Well, you would replace them back when you want to use the ciphertext to decrypt it. – Artjom B. Commented Jun 26, 2017 at 5:12
Add a comment  | 

1 Answer 1

Reset to default 19

You would likely want to do:

export function dec(cipherText){
   var bytes = CryptoJS.AES.decrypt(cipherText, SECRET);
   var hex = bytes.toString(CryptoJS.enc.Hex);
   var plain = bytes.toString(CryptoJS.enc.Utf8);
   return [hex, plain];
}

This takes the encrypted base64 string and will return the decrypted plaintext and hexadecimal.

EDIT: In regards to your comment and edited question:

const SECRET = 'I am batman'

function enc(plainText){
    var b64 = CryptoJS.AES.encrypt(plainText, SECRET).toString();
    var e64 = CryptoJS.enc.Base64.parse(b64);
    var eHex = e64.toString(CryptoJS.enc.Hex);
    return eHex;
}

function dec(cipherText){
   var reb64 = CryptoJS.enc.Hex.parse(cipherText);
   var bytes = reb64.toString(CryptoJS.enc.Base64);
   var decrypt = CryptoJS.AES.decrypt(bytes, SECRET);
   var plain = decrypt.toString(CryptoJS.enc.Utf8);
   return plain;
}

The end result takes the base64 string, makes it hexadecimal and returns the decrypted string.

发布评论

评论列表(0)

  1. 暂无评论