0

I used this base64 and uriencode on a number

<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/core-min.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/enc-base64-min.js"></script>
<script>
    var num= '1418265869452';
    var base64num =  CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(salt)); 
    var encodeuri = encodeURIComponent(base64num);
</script>

base64 num gives me MTQxODI2NTg2OTQ1Mg==

But actually i also needs to uri encode it after base64 encode

when i tried to encodeURIComponent(base64num) it throwed me an error as follows:

 Exception thrown from JavaScript : Error: Malformed UTF-8 data 

How to achieve this

2
  • 1
    Where do you set salt? And what is num used for? Commented Dec 11, 2014 at 2:57
  • I changed salt to num and it works fine for me: jsfiddle.net/barmar/nbuapp07 Commented Dec 11, 2014 at 2:59

2 Answers 2

2

I've never seen encodeURIComponent throw JS errors. Its probably someplace in the third party library. Depending on your browser support requirements you might be able to use the built in base64 encoder/decoders

enter image description here

var base64num = btoa('1418265869452'),
    encodeuri = encodeURIComponent(base64num);
encodeuri; // "MTQxODI2NTg2OTQ1Mg%3D%3D"

This could also be happening when you try to decode it. First you have to decodeURIComponent before trying to base64 decode it.

Sign up to request clarification or add additional context in comments.

Comments

0

You are passing salt variable as a parameter, but I am not seeing any initialization of it. Instead you need to pass num, then it will start working, as I have used num in place of salt and is working perfectly fine at my end.

Here is the updated JSFiddle Link

Updated Code:

var num= '1418265869452';
var base64num =  CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(num)); 
var encodeuri = encodeURIComponent(base64num);
alert(encodeuri);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.