Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"CTR" mode is not same as "aes-256-ctr" in crypto library. #73

Open
Just4test opened this issue Oct 16, 2018 · 4 comments
Open

"CTR" mode is not same as "aes-256-ctr" in crypto library. #73

Just4test opened this issue Oct 16, 2018 · 4 comments

Comments

@Just4test
Copy link

Just4test commented Oct 16, 2018

I have some file which encrypted by aes-js CTR mode.
Now I want to decrypt them using library "crypto". I think ctr in crypto should be "aes-256-ctr", but I can't decrypt them.

Here 's my demo code:(I use node v8.11.2)

const aesjs = require('aes-js')
const crypto = require('crypto')
const key = new Buffer([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
	16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31])
const text = 'Text may be any length you wish, no padding is required.Now I want to decrypt them using library "crypto".'
const buffer = Buffer.from(text, 'utf8')

function byAesjs(bytes){
	let aesCtr = new aesjs.ModeOfOperation.ctr(key)
	let encryptedBytes = Buffer(aesCtr.encrypt(bytes))
	return encryptedBytes
}

function byCrypto(bytes){
	let cipher = crypto.createCipher('aes-256-ctr', key)
	let encryptedBytes = cipher.update(bytes)
	encryptedBytes = Buffer.concat([encryptedBytes, cipher.final()])
	return encryptedBytes
}

let temp = byAesjs(buffer)
temp = byAesjs(temp)
console.log('======================================')
console.log(temp.toString('utf8') == text)

temp = byCrypto(buffer)
temp = byCrypto(temp)
console.log('======================================')
console.log(temp.toString('utf8') == text)

temp = byAesjs(buffer)
temp = byCrypto(temp)
console.log('======================================')
console.log(temp.toString('utf8') == text) //return false
@mikegwhit
Copy link

I see that you are relying on the symmetric nature of the algorithm. Also, per the examples, sometimes a conversion to a hex string helped me when wrapping the Node API.

@Just4test
Copy link
Author

Just4test commented Dec 26, 2018

I see that you are relying on the symmetric nature of the algorithm. Also, per the examples, sometimes a conversion to a hex string helped me when wrapping the Node API.

So.... it is possable to decrypt it using crypto? What should I do?
I've tried replace "crypto.createCipher('aes-256-ctr', key)" to "crypto.createCipher('aes-256-ctr', key.toString('hex'))" But it doesn't work either.
Thank you.

@mikegwhit
Copy link


    encrypt(data) {
        const iv = this.initializationVector();
        const cipher = this.crypto.createCipheriv(this.algorithm, 
            this.key, iv);
        let encrypted = new Buffer(iv).toString('base64') + 
            cipher.update(data, 'utf8', 'base64') +
            cipher.final('base64');
        this.hmac = this.crypto.createHmac('sha512', this.key)
            .update(encrypted).digest('hex');

        return encrypted;
    }

That's the fn I use for crypto in my library. A simple symmetric wrapper around the native library.

Node's native lib is finicky. It took me some time to figure out.

I haven't yet built a bridge to the browser, but it's on my TODO list which is how I landed here. I'm interested in the same problem you're solving in other words :)

@mikegwhit
Copy link

IIRC base64 may be what Node prefers but I don't exactly recall. There may also be padding-related issues since the aes-js library seems to indicate fragility around fixed length keys.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants