-1

The following piece of code returns "does not match!" error:

pub, priv := GenerateKeyPair(2048)
ct1 := EncryptWithPublicKey([]byte("abc"), pub)
err := stub.PutState("ct", ct1)
ct2 := stub.GetState("ct")
if string(ct1[:]) != string(ct2[:]) {
    return shim.Error("does not match!") //error returned
}
pt := DecryptWithPrivateKey(ct2, priv)

RSA library used: https://gist.github.com/miguelmota/3ea9286bd1d3c2a985b67cac4ba2130a

If the string comparison part is commented out, then the decryption function returns the error "crypto/rsa decryption error".

The following code works perfectly:

pub, priv := GenerateKeyPair(2048)
ct := EncryptWithPublicKey([]byte("abc"), pub)
pt := DecryptWithPrivateKey(ct, priv)
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62

1 Answers1

2

The way that Fabric transactions work mean that you cannot read (GetState) a value from the state database that was written (PutState) during your transaction. The read (GetState) will always return the value before the transaction started.

The following links on Transaction Flow and the sequence diagram will give you more background on transactions if you need it.

R Thatcher
  • 5,550
  • 1
  • 7
  • 15
  • Actually, I initially, found the error when the encryption and PutState was part of one transaction and GetState and subsequent decryption was part of another transaction. It returned "crypto/rsa decryption error". While trying to solve the issue, I ended up merging the functions! I will edit the question to revert back to my original issue. Thank you. – the_stack_over_flew Apr 16 '19 at 09:25