AESDecrypt errore

Ciao a tutti,

sto effettuando della criptografia su delel stringe e poi le salvo a database. In seguito durante il caricamento le decripto.

La chiava è un dato dell’utente che inserisce lui. Mentre l’InitialVector viene generato e salvato e poi ricaricato in fase di decrypt.

Encrypt:


var chiavePublica as String = Crypto.SHA2_256(Configurazione.accessKey )

var segmentoFisso as String = Crypto.SHA2_256(App.SKAcc) 
var chiaveVettorePrivata as String  = Crypto.SHA2_256(Configurazione.vectorGenerated) 

Var datoCriptatoMB As MemoryBlock
datoCriptatoMB = Crypto.AESEncrypt(chiavePublica, data, Crypto.BlockModes.CBC, chiaveVettorePrivata)
Var datoCriptato as String
If datoCriptatoMB <> Nil Then
  datoCriptato = datoCriptatoMB.StringValue(0, data.Length)
end if

Decript:


var chiavePublica as String = Crypto.SHA2_256(Configurazione.accessKey )

var segmentoFisso as String = Crypto.SHA2_256(App.SKAcc) 
var chiaveVettorePrivata as String  = Crypto.SHA2_256(Configurazione.vectorGenerated) 

Var datoDecriptatoMB As MemoryBlock
datoDecriptatoMB = Crypto.AESDecrypt(chiavePublica, data, Crypto.BlockModes.CBC, chiaveVettorePrivata)

Var datoDecriptato as String
If datoDecriptatoMB <> Nil Then
  datoDecriptato = datoDecriptatoMB.StringValue(0, data.Length)
end if

Il problema che in fase di AESDecrypt mi restituisce un eccessione con questo messaggio:
StreamTransformationFilter: ciphertext length is not a multiple of block size

Non riesco a capire dove è l’errore.
Le chiavi corrispondono in tutte e due i metodi.

Qualcuno sa darmi qualche aiuto?

grazie

It looks like you are truncating the encrypting data, making it impossible to decrypt.

Replace the encryption code starting with Var datoCriptatoMB As MemoryBlock with just this:

Var datoCriptato as String = Crypto.AESEncrypt(chiavePublica, data, Crypto.BlockModes.CBC, chiaveVettorePrivata)

Likewise, replace the decryption code starting with Var datoDecriptatoMB As MemoryBlock with this:

Var datoDecriptato as String = Crypto.AESDecrypt(chiavePublica, data, Crypto.BlockModes.CBC, chiaveVettorePrivata)

Many thanks for solution.