Golang OpenSSL bad iv size - go

I'm trying to create the following:
cipher, err := openssl.GetCipherByName("aes-128-ecb")
decryptionTool, err := openssl.NewDecryptionCipherCtx(cipher, nil, byteKey, iv)
The byteKey and iv are both 16 bytes long.
When I build my code, I get the following error:
panic: bad IV size (16 bytes instead of 0)
I read the documentation, and checked the source code, but I still can't find a way to add the IV without getting an error. I am using spacemonkeygo's OpenSSL.
Does anyone know what's wrong?
And thanks in advance!
EDIT: I added the cipher type above, and a bit more details about biteKey and iv
EDIT 2: ECB has no IV! I totally forgot about that. Well I guess that solves it.

I used ECB the first time, and I wanted to change it to CBC. I forgot to change it to aes-cbc and so I got an error when I tried adding an IV.

Related

is there any way to check and change some file encoding type in the same time in golang?

i use golang.org/x/text/encoding/charmap package and below code for changing file encoding and it works well.
data, err := ioutil.ReadFile(*file)
checkError(err)
encode := charmap.Windows1256.NewDecoder()
transform, err := encode.Bytes(data)
checkError(err)
err = ioutil.WriteFile(string(*output), transform, 644)
checkError(err)
now i want to work with some other encoding type too but the problem is there is no way for working with or checking other encoding for changing the file encode or walking charmap package for check encoding unless using switch/case for checking every encoding type and there are a lot encoding type and i feel this is wrong way to do this, so is there any solution for make this coding process more productive?
thank you for help.

Ruby RSA public key encryption to Golang

I'm currently working on a project where I have to "convert" some code from Ruby(version 1.9.3p194) to Golang(version 1.7). There is this part where Ruby uses RSA public key encryption and I always get a consistent result every time it gets executed. This is the function used:
Edit: I overlooked that after the public key encryption, there is a base 64 encoding as well
public_key = OpenSSL::PKey::RSA.new(public_encryption_key)
public_encrypted_text = public_key.public_encrypt(text, OpenSSL::PKey::RSA::NO_PADDING)
base64_encrypted_text = Base64.encode64(public_encrypted_text).gsub("\n", "")
escaped_encrypted_text = URI.escape(encrypted_key, "/+=")
However in Golang, due to the rsa library I can't get a consistent result since the function to encrypt takes a random parameter to generate different result each time. I understand why it needs to be different every time, but i can't get anything remotely similar to what ruby generates. These are the functions used in Golang:
//keyBytes is the public key as []byte
block, _ := pem.Decode(keyBytes)
key, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return nil, err
}
pubKey, ok := key.(*rsa.PublicKey)
if !ok {
return nil, errors.New("Cannot convert to rsa.PublicKey")
}
result, err := rsa.EncryptPKCS1v15(cryptorand.Reader, pubKey, text)
encryptedText := base64.URLEncoding.EncodeToString(result)
encryptedText = strings.TrimRight(encryptedText, "=")
One of the problems is that ruby can encrypt the text with no problem, and in golang I'm getting an error that the key is too short to encrypt everything.
If I encrypt something else, like "Hello". When decrypting I get from ruby the error "padding check failed". The decryption is being handle like follows:
private_key.private_decrypt(Base64.decode64(text))
EDIT: Thanks to the answer of gusto2 I now know better what is going on since I didn't have much understanding of RSA.
Now in Golang I was able to encrypt the text using PKCS1 v1.5, and just to be sure I tried to decrypt that as well, also in Golang, with no problem.
However in Ruby I still wasn't able to decrypt using the private key. So I figured that the base64 encoding used in Golang was the issue. So I changed that part to this:
encryptedText := base64.StdEncoding.EncodeToString(result)
And also I removed the last line were the equal sign was being trimmed.
With that done it worked like a charm.
I am no knowledgeable about golang, however I may know something about RSA.
The difference seems to be in the padding.
For ruby - no padding is used
For golang - PKCS1v15 padding is used
In the rubyexample you use OpenSSL::PKey::RSA::NO_PADDING is used which is VERY VERY unsafe. It is called textbook RSA and is not inteded in real-life use as it has many weaknesses and dangerous traps. So the ruby example is very dangerously unsafe because of using the textbook RSA. As well it is limited to encrypting small messages (much smaller than the keyspace).
There are two padding types used with RSA:
PKCS1 v1 (commonly referred as PKCS1) - this is a deterministic padding (the output is always the same), many cryptographers consider this option obsolete as some weaknesses has been found when not used properly, but it is still in use and not considered broken.
PKCS1 v2 (commonly refered as OAEP or PSS) which is stochastic (randomized) padding. You can distinguish the last two as the output of OAEP is always different.
One of the problems is that ruby can encrypt the text with no problem, and in golang I'm getting an error that the key is too short to encrypt everything
You've provided only a small part of the golang example, so here I may only assume many things.
As you claim the golang example outputs randomized output and according to the parameters PKCS1 v1.5 is used, I'd assume the implementation is doing hybrid encryption which is good and much safer way to encrypt data with RSA (using symmetric encryption with random key and wrap/encrypt the key with RSA).

go-ethereum: failed to retrieve account nonce

I create go bindings for my smart contract but have an issue when executing a transaction. It only works when I explicitly specify the txOpts.Nonce (see commented line). When I leave the line commented I get this error:
Failed to execute transaction: failed to retrieve account nonce: json: cannot unmarshal hex number with leading zero digits into Go value of type hexutil.Uint64`
Here is the relevant code:
txOpts := bind.NewKeyedTransactor(key)
//txOpts.Nonce = big.NewInt(<nonce>)
tx, err := token.MyContract(txOpts, big.NewInt(1))
if err != nil {
log.Fatalf("Failed to execute transaction: %v", err)
}
The documentation tells it would retrieve the pending nonce from txOpts.From when txOpts.Nonce is nil.
For anyone else wondering about this, I encountered this error while testing with the Truffle develop framework. For me, this problem was because the Truffle JSON-RPC server was returning hex values with leading zeros like "0x04", which violates the spec here:
https://github.com/ethereum/wiki/wiki/JSON-RPC#hex-value-encoding
When encoding QUANTITIES (integers, numbers): encode as hex, prefix with "0x", the most compact representation (slight exception: zero should be represented as "0x0").
WRONG: 0x0400 (no leading zeroes allowed)
That said, for Truffle, there's already a pull request here: https://github.com/trufflesuite/ganache-core/pull/32
If you're using another JSON-RPC server, you'd have to verify for yourself that it's actually following this spec.

Storing crypto/rand generated string issues

So I have the following go file(s) as part of my project to be used for hashing passwords, I also wrote some tests that to my knowledge have yet to fail.
Currently the issue is that I am trying to store the password and salt in some database as strings, and every time I retrieve them to be compared against a another string I keep getting the message in the picture from golang's bcrypt package. The tests I wrote are running fine and produce the appropriate effect. I would have supplied a go playground link but bcrypt package is part of the standard library.
I know the gibberish from crypto/rand is pretty much the same from the initial look but I am not sure if there is anything being changed on the database. I am using redis fyi.
Edit: based on the request of #3of3, I am including the DAO code from my project. Also the bcrypt only solution worked with this code but as I stated in the comments, I am aiming to stick to Mozilla's guide.
The salt does not roundtrip through the JSON encode / decode because the salt is not valid UTF8.
There are a few ways to fix the problem:
Hex or base64 encode / decode the salt in hasher.
Use the []byte type for salt throughout the code. The JSON encoder encodes []byte values using base64.
Use the gob encoder instead of the JSON encoder.
Mozilla recommends storing the extra salt separate from the bcrypted password. By storing the extra salt with the bcrypted password, the system is no more secure than using bcrypt alone.
To hex encode the salt, change
return string(p), string(salt), nil
to
return string(p), hex.EncodeToString(salt), nil
and change
s := []byte(salt)
to
s, err := hex.DecodeString(salt)
if err != nil {
return err
}
It seems you forgot that the generated hashes are hex encoded, thus when casting the []byte variable to a string you'll get something weird. Using the hex package you can create the actual string you want:
hex.EncodeToString(hash)

How to decrypt a string with known key and a IV in java?

I have a string that is encrypted in c#.net which I have to decrypt in java using a key and a IV provided by the client. Used algorithm is AES.
I have tried few things. The key looks something like
key = "QWEEqweASDreefERTfdf45fefdWERfsdf34fedfdwn5=" //length 44 bytes
iv = "nkfghER24dfdfdf56YUIgH==" // lenght=24 bytes
When I use this with Cipher class with algorith AES/CBC/PKCS5Padding
passing the above key to Secretkeyspec class it says invalid key lenth 44 bytes
I am not able makeout whats wrong with the key. Tried all suggested solutions for few days nothing works. Can some one help please? Thank you.
Use java native for C# code.
First write C# code for decrypt the key.
and calling the code in java using native.
for reference
http://www.codeproject.com/Articles/378826/How-to-wrap-a-Csharp-library-for-use-in-Java

Resources