AES-CTR encryption in Go and decryption in CryptoJS


Mark:

I'm having trouble decrypting text encrypted in Go lang using CryptoJS.

Here is the Go code : https://play.golang.org/p/xCbl48T_iN

package main

import (
    "crypto/aes"
    "crypto/cipher"
    "encoding/base64"
    "fmt"
)

func main() {
    key := []byte("1234567890123456")
    plaintext := []byte("text can be a random lenght")

    block, err := aes.NewCipher(key)
    if err != nil {
        panic(err)
    }

    // The IV needs to be unique, but not secure. Therefore it's common to
    // include it at the beginning of the ciphertext.
    // BTW (only for test purpose) I don't include it

    ciphertext := make([]byte, len(plaintext))

    iv := []byte{'\x0f','\x0f','\x0f','\x0f','\x0f','\x0f','\x0f','\x0f','\x0f','\x0f','\x0f','\x0f','\x0f','\x0f','\x0f','\x0f'}

    stream := cipher.NewCTR(block, iv)
    stream.XORKeyStream(ciphertext, plaintext) 

    // CTR mode is the same for both encryption and decryption, so we can
    // also decrypt that ciphertext with NewCTR.
        base := base64.StdEncoding.EncodeToString(ciphertext)
    fmt.Printf("encodedHEX: %x\n", ciphertext)
    fmt.Printf("encodedBASE: %s\n", base)

    plaintext2 := make([]byte, len(plaintext))
    stream = cipher.NewCTR(block, iv)
    stream.XORKeyStream(plaintext2, ciphertext)

    fmt.Printf("decoded: %s\n", plaintext2)
}

Here is the JS code : http://jsfiddle.net/Ltkxm64n/

var key = CryptoJS.enc.Hex.parse('31323334353637383930313233343536');
var iv = CryptoJS.enc.Hex.parse('0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f');
var encrypted = CryptoJS.AES.encrypt("text can be a random lenght", key, {
  mode: CryptoJS.mode.CTR,
  iv: iv
});

console.log(encrypted.ciphertext.toString());
console.log(encrypted.toString());

var decrypted = CryptoJS.AES.decrypt(encrypted, key, {
  mode: CryptoJS.mode.CTR,
  iv: iv
});
console.log(decrypted.toString(CryptoJS.enc.Utf8)); 
// text can be a random lenght

Both encrypt and decrypt fine, but when I copy the base64 ciphertext from GO to JS (and vice versa) it doesn't work. I also noticed that the first part of the js output is the same as the Go output, but there are more bytes in the js output than in Go.

My purpose is to encrypt some text in GO and then send the Base64 ciphertext to JS which can decrypt it.

thanks

I. Yegor:

OK, here's your problem:

  1. Add no-padding js to your sources list:http://crypto-js.googlecode.com/svn/tags/3.1/build/components/pad-nopadding.js

  2. When encrypting/decrypting, please specify parameters:padding: CryptoJS.pad.NoPadding

CTR mode does not require padding of plain text before encryption.
The keystream generated from multiple AES blocks is trimmed to match the plaintext length before XORing.
It looks like CryptoJS generates a keystream for xorit using the plaintext , but doesn't trim it, because the ciphertext that CryptoJS doesn't generate is padding: CryptoJS.pad.NoPaddingalways a multiple of 16 bytes long (which happens to be the AES block size).

var key = CryptoJS.enc.Hex.parse('31323334353637383930313233343536');
var iv = CryptoJS.enc.Hex.parse('0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f');
var encrypted = CryptoJS.AES.encrypt("text can be a random lenght", key, {
  mode: CryptoJS.mode.CTR,
  iv: iv,
  padding: CryptoJS.pad.NoPadding
});

document.getElementById("id").innerHTML = encrypted.ciphertext.toString();
document.getElementById("id2").innerHTML = encrypted.toString();

var decrypted = CryptoJS.AES.decrypt(encrypted, key, {
  mode: CryptoJS.mode.CTR,
  iv: iv,
  padding: CryptoJS.pad.NoPadding
});
document.getElementById("decrypt").innerHTML = decrypted.toString(CryptoJS.enc.Utf8); // text can be a random lenght
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/mode-ctr.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1/build/components/pad-nopadding.js"></script>
<p> Ciphertext in HEX: </p>
<p id="id"> </p>
<p> Ciphertext in BASE64: </p>
<p id="id2"> </p>
<p> PlainText: </p>
<p id="decrypt"></p>

Related


AES-CTR encryption in Go and decryption in CryptoJS

Mark: I'm having trouble decrypting text encrypted in Go lang using CryptoJS. Here is the Go code : https://play.golang.org/p/xCbl48T_iN package main import ( "crypto/aes" "crypto/cipher" "encoding/base64" "fmt" ) func main() { key := []b

AES-CTR encryption in Go and decryption in CryptoJS

Mark: I'm having trouble decrypting text encrypted in Go lang using CryptoJS. Here is the Go code : https://play.golang.org/p/xCbl48T_iN package main import ( "crypto/aes" "crypto/cipher" "encoding/base64" "fmt" ) func main() { key := []b

AES-CTR encryption in Go and decryption in CryptoJS

Mark: I'm having trouble decrypting text encrypted in Go lang using CryptoJS. Here is the Go code : https://play.golang.org/p/xCbl48T_iN package main import ( "crypto/aes" "crypto/cipher" "encoding/base64" "fmt" ) func main() { key := []b

AES-CTR encryption in Go and decryption in CryptoJS

Mark: I'm having trouble decrypting text encrypted in Go lang using CryptoJS. Here is the Go code : https://play.golang.org/p/xCbl48T_iN package main import ( "crypto/aes" "crypto/cipher" "encoding/base64" "fmt" ) func main() { key := []b

AES-CTR encryption in Go and decryption in CryptoJS

Mark: I'm having trouble decrypting text encrypted in Go lang using CryptoJS. Here is the Go code : https://play.golang.org/p/xCbl48T_iN package main import ( "crypto/aes" "crypto/cipher" "encoding/base64" "fmt" ) func main() { key := []b

AES CTR encryption and decryption

Wajahat So I have this code that basically encrypts two plain text messages and then tries to decrypt them and print them. The problem is that the first message recovers just fine, but the second is garbage. I downloaded this code from this tutorial and modifi

AES CTR encryption and decryption

Wajahat So I have this code that basically encrypts two plain text messages and then tries to decrypt them and print them. The problem is that the first message recovers just fine, but the second is garbage. I downloaded this code from this tutorial and modifi

AES CTR encryption and decryption

Wajahat So I have this code that basically encrypts two plain text messages and then tries to decrypt them and print them. The problem is that the first message recovers just fine, but the second is garbage. I downloaded this code from this tutorial and modifi

AES CTR encryption and decryption

Wajahat So I have this code that basically encrypts two plain text messages and then tries to decrypt them and print them. The problem is that the first message recovers just fine, but the second is garbage. I downloaded this code from this tutorial and modifi

AES CTR encryption and decryption

Wajahat So I have this code that basically encrypts two plain text messages and then tries to decrypt them and print them. The problem is that the first message recovers just fine, but the second is garbage. I downloaded this code from this tutorial and modifi

AES CTR encryption and decryption

Wajahat So I have this code that basically encrypts two plain text messages and then tries to decrypt them and print them. The problem is that the first message recovers just fine, but the second is garbage. I downloaded this code from this tutorial and modifi

CryptoJS encryption Go decryption

Ivaylo Ivanov: I have the following Go code ciphertext := "Zff9c+F3gZu/lsARvPhpMau50KUkMAie4j8MYfb12HMWhkLqZreTk8RPbtRB7RDG3QFw7Y0FXJsCq/EBEAz//XoeSZmqZXoyq2Cx8ZV+/Rw=" decodedText, _ := base64.StdEncoding.DecodeString(ciphertext) decodedIv, _ := base64.StdEnc

CryptoJS encryption Go decryption

Ivaylo Ivanov: I have the following Go code ciphertext := "Zff9c+F3gZu/lsARvPhpMau50KUkMAie4j8MYfb12HMWhkLqZreTk8RPbtRB7RDG3QFw7Y0FXJsCq/EBEAz//XoeSZmqZXoyq2Cx8ZV+/Rw=" decodedText, _ := base64.StdEncoding.DecodeString(ciphertext) decodedIv, _ := base64.StdEnc

CryptoJS encryption Go decryption

Ivaylo Ivanov: I have the following Go code ciphertext := "Zff9c+F3gZu/lsARvPhpMau50KUkMAie4j8MYfb12HMWhkLqZreTk8RPbtRB7RDG3QFw7Y0FXJsCq/EBEAz//XoeSZmqZXoyq2Cx8ZV+/Rw=" decodedText, _ := base64.StdEncoding.DecodeString(ciphertext) decodedIv, _ := base64.StdEnc

CryptoJS encryption Go decryption

Ivaylo Ivanov: I have the following Go code ciphertext := "Zff9c+F3gZu/lsARvPhpMau50KUkMAie4j8MYfb12HMWhkLqZreTk8RPbtRB7RDG3QFw7Y0FXJsCq/EBEAz//XoeSZmqZXoyq2Cx8ZV+/Rw=" decodedText, _ := base64.StdEncoding.DecodeString(ciphertext) decodedIv, _ := base64.StdEnc

CryptoJS encryption Go decryption

Ivaylo Ivanov: I have the following Go code ciphertext := "Zff9c+F3gZu/lsARvPhpMau50KUkMAie4j8MYfb12HMWhkLqZreTk8RPbtRB7RDG3QFw7Y0FXJsCq/EBEAz//XoeSZmqZXoyq2Cx8ZV+/Rw=" decodedText, _ := base64.StdEncoding.DecodeString(ciphertext) decodedIv, _ := base64.StdEnc

AES encryption with CryptoJS and decryption with CodeIgniter

fire brand I'm trying to encrypt a username (written in Codeigniter 3) sent to my server via a POST request, so I'm using CryptoJS to do it client-side like this: var user = $('.user').val(); var key = "<? echo($key);?>"; //$key is created on the server side v

AES encryption with CryptoJS and decryption with CodeIgniter

fire brand I'm trying to encrypt a username (written in Codeigniter 3) sent to my server via a POST request, so I'm using CryptoJS to do it client-side like this: var user = $('.user').val(); var key = "<? echo($key);?>"; //$key is created on the server side v

AES CBC: JavaScript/CryptoJS encryption -> Golang decryption

DjH Note: This is for personal use and learning only, I don't plan to use my encryption for public use. I need to AES256 encrypt the string, but my current attempt ends up with something like the server side string Salted__Vέ��|��l��ʼ8XCQlYwhen hex decoded . W

AES CBC: JavaScript/CryptoJS encryption -> Golang decryption

DjH Note: This is for personal use and learning only, I don't plan to use my encryption for public use. I need to AES256 encrypt the string, but my current attempt ends up with something like the server side string Salted__Vέ��|��l��ʼ8XCQlYwhen hex decoded . W

AES encryption in C# and decryption in CryptoJS

Pabitra Dash I want to perform AES encryption in C# and decryption in CryptoJS. Pabitra Dash After getting some references from the Google CryptoJS group ( https://groups.google.com/forum/#!msg/crypto-js/ysgzr2Wxt_k/_Wh8l_1rhQAJ ) it now works fine . Here is t

AES encryption/decryption in javascript using CryptoJS

Anthracene I am trying to send AES encrypted messages between javascript and php using a shared key. In Javascript, I am using the CryptoJS library. In php, I am using mycrypt. I am trying to construct an encrypted message in javascript and then decrypt it in

AES encryption/decryption in javascript using CryptoJS

Anthracene I am trying to send AES encrypted messages between javascript and php using a shared secret. In Javascript, I am using the CryptoJS library. In php, I am using mycrypt. I am trying to construct an encrypted message in javascript and then decrypt it

AES encryption/decryption in javascript using CryptoJS

Anthracene I am trying to send AES encrypted messages between javascript and php using a shared secret. In Javascript, I am using the CryptoJS library. In php, I am using mycrypt. I am trying to construct an encrypted message in javascript and then decrypt it

AES CBC: JavaScript/CryptoJS encryption -> Golang decryption

DjH Note: This is for personal use and learning only, I don't plan to use my encryption for public use. I need to AES256 encrypt the string, but Salted__Vέ��|��l��ʼ8XCQlYwhen hex decodes it , my current attempt ends up with something like the string on the ser