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

javascript - How to simply encrypt and decrypt cookie string? - Stack Overflow

programmeradmin0浏览0评论

I am trying to encrypt a string when setting it as a cookie with javascript AES, it encrypts fine. but after I recuperate the string (within the cookie) to decrypt it, it doesn't e back as the original string.

class Auth{
    constructor(){
        this.pass = "iscae";
        this.cookies = new Cookies();
        this.email = this.cookies.get("email");
        this.connected = false;
        this.checkConnection();
        return this;
    }
    checkConnection(){
        if(this.email !== undefined){
            this.email = crypto.AES.decrypt(this.email,this.pass).toString();
            this.connected = true;
        }
        else{
            this.connected = false;
        }
        return {
            email : this.email,
            connected : this.connected
        };
    }
    connect(email){
        this.email = crypto.AES.encrypt(email,this.pass).toString();
        this.cookies.set("email",this.email);
        return this;
    }
    disconnect(){
        this.cookies.remove("email");
        return this;
    }
}


export default Auth;

I am trying to encrypt a string when setting it as a cookie with javascript AES, it encrypts fine. but after I recuperate the string (within the cookie) to decrypt it, it doesn't e back as the original string.

class Auth{
    constructor(){
        this.pass = "iscae";
        this.cookies = new Cookies();
        this.email = this.cookies.get("email");
        this.connected = false;
        this.checkConnection();
        return this;
    }
    checkConnection(){
        if(this.email !== undefined){
            this.email = crypto.AES.decrypt(this.email,this.pass).toString();
            this.connected = true;
        }
        else{
            this.connected = false;
        }
        return {
            email : this.email,
            connected : this.connected
        };
    }
    connect(email){
        this.email = crypto.AES.encrypt(email,this.pass).toString();
        this.cookies.set("email",this.email);
        return this;
    }
    disconnect(){
        this.cookies.remove("email");
        return this;
    }
}


export default Auth;
Share Improve this question edited Apr 9, 2020 at 8:57 Jonathan Hall 79.8k19 gold badges159 silver badges203 bronze badges asked Feb 27, 2020 at 11:08 Dems314Dems314 3473 silver badges15 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

The problem is in the line:

this.email = crypto.AES.decrypt(email,this.pass).toString();

Without a parameter in toString() you'll get a string of hexadecimal ASCII codes. For example, when the plain text is "[email protected]", you'll get:

7465737440656d61696c2e636f6d

in hexadecimal representation (74="t", 65="e",...).

To get the normal string representation, i.e.

[email protected]

you need to pass a parameter like this:

toString(CryptoJS.enc.Utf8);

toString behaves like this because the decryption function crypto.AES.decrypt()returns a byte array, as the content could be binary data as well (e.g. pictures or other data). toString() just converts each byte into it's hexadecimal string reprensentation.

By passing the parameter CryptoJS.enc.Utf8 you tell the toString() method explicitely to treat the bytes as UTF-8 encoded characters.

发布评论

评论列表(0)

  1. 暂无评论