Marcus Höglund
Marcus Höglund

Reputation: 16811

What security threats does btoa() in javascript help me with?

Let's say I have an authentication method where I do a http post to get a token from server:

$http({
     method: 'POST',
     url: '/Token',
     processData: false,
     contentType: 'application/x-www-form-urlencoded',
     data: "grant_type=password&username=" + UserName + "&password=" + Password
...

Here I send the username and password as clear text.

If I instead encrypt my username and password with the javascript function btoa() (which is well used and recommended to be used How can you encode a string to Base64 in JavaScript?) like this:

$http({
     method: 'POST',
     url: '/Token',
     processData: false,
     contentType: 'application/x-www-form-urlencoded',
     data: "grant_type=password&username=" + btoa(UserName) + "&password=" + btoa(Password)
...

What security threats does this really help me with? I mean, the fact that my javascript code is reachable for anyone on my website it's possible to find the script which calls btoa(). Then they can just decrypt the username and password with atob() and I'm back to square one.

Upvotes: 0

Views: 3362

Answers (1)

Igor Bukin
Igor Bukin

Reputation: 1006

The btoa() and atob() functions does not provide any security improvements, since they are not encrypt/decrypt but just encode/decode data to text representation and back to the original look. Usually the functions are used for binary data, like images, etc.

Having a string encoded with btoa() anyone can easily decode it with its opposite function. Doesn't sound secure, does it ;)

Upvotes: 5

Related Questions