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

Fix PKCS7Pad() error #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

whoisnian
Copy link

#7

@whoisnian
Copy link
Author

go run cookie.go | ruby cookie.rb to test.
JSON.stringify({cid:"1234567"}).length is 17, so it's ok.
JSON.stringify({cid:"123456"}).length is 16, and rails will throw error.

cookie.go

package main

import (
	"fmt"

	"github.com/mattetti/goRailsYourself/crypto"
)

type MySession struct {
	CID string `json:"cid"`
}

func main() {
	railsSecret := "this is a secret"
	encryptedCookieSalt := []byte("encrypted cookie")
	encryptedSignedCookieSalt := []byte("signed encrypted cookie")

	kg := crypto.KeyGenerator{Secret: railsSecret}
	secret := kg.CacheGenerate(encryptedCookieSalt, 32)
	signSecret := kg.CacheGenerate(encryptedSignedCookieSalt, 64)
	e := crypto.MessageEncryptor{Key: secret, SignKey: signSecret}

	session := MySession{CID: "1234567"}
	msg, err := e.EncryptAndSign(session)
	if err != nil {
		panic(err)
	}
	fmt.Print(msg)
}

cookie.rb

#!/usr/bin/env ruby
# frozen_string_literal: true

# https://blog.iany.me/zh/2017/09/rails-cookie-encryption/

require 'active_support'
require 'json'

SECRET_KEY_BASE   = 'this is a secret'
DEFAULT_SALT      = 'encrypted cookie'
DEFAULT_SIGN_SALT = 'signed encrypted cookie'

key_generator = ActiveSupport::KeyGenerator.new(SECRET_KEY_BASE, iterations: 1000)
encrypt_key = key_generator.generate_key(DEFAULT_SALT, 32)
sign_key = key_generator.generate_key(DEFAULT_SIGN_SALT, 64)

encryptor = ActiveSupport::MessageEncryptor.new(encrypt_key, sign_key, serializer: JSON)
inputs = gets
puts inputs

res = encryptor.decrypt_and_verify(CGI.unescape(inputs))
puts res.to_json.length
puts res.to_json

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

Successfully merging this pull request may close these issues.

None yet

1 participant