encryption - vb.net AES decryption returns "data is incomplete block" -
i'm aware of other thread on issue (aes decryption error " input data not complete block." error vb.net), i'm either not implementing solutions offered there correctly, or particular variant of issue isn't covered solutions. in event i'm getting incomplete block error following code
private gd system.security.cryptography.aes = system.security.cryptography.aes.create private pdb new system.security.cryptography.rfc2898derivebytes(ek, new byte() {&h49, &h76, &h61, &h6e, &h20, &h4d, &h65, &h64, &h76, &h65, &h64, &h65, &h76}) public function decrypt(byval val string) string dim ret string = nothing dim ttb new system.text.utf8encoding try dim input() byte = ttb.getbytes(val) using ms new system.io.memorystream(input) using cs new system.security.cryptography.cryptostream(ms, gd.createdecryptor(pdb.getbytes(32), pdb.getbytes(16)), security.cryptography.cryptostreammode.read) using sr new system.io.streamreader(cs) ret = sr.readtoend() end using end using end using input = nothing catch ex exception el.adderr("encountered error while decrypting provided text " & fname & ". error details: " & ex.message, path) end try return ret end function ek key, i'll not including. it's string though, nothing special.
i've tried several other methods decrypt based on guidance on msdn site, dreamincode, etc. none worked, had different issues (typically returning blank string). seeing version of code closely mirrors encryption code, i'd stick (or @ least close can while still having functional code).
despite comments, still lack understanding of intentions. therefore, sample code below may not provide want, @ least should give idea how employ cryptographic functions. particularly, notable difference approach encryption key , initialization vector computed once , messages, rather reevaluated on each occasion, because latter prone synchronization errors — such when reuse single crypto object communicate multiple parties, or when messages lost in transmission.
public shared sub test() ' note: should not hard-code sensitive information in source files, ever! dim skeypreimage string = "mysuperpassword" dim omycrypto new mycrypto(skeypreimage) dim splaintext string = "my super secret data" dim sencrypted string = omycrypto.encrypttext(splaintext) dim sdecrypted string = omycrypto.decrypttext(sencrypted) console.out.writeline("plaintext: {0}", splaintext) ' "my super secret data" console.out.writeline("encrypted: {0}", sencrypted) ' "72062997872dc4b4d1bcbf48d5d30df0d498b20630cafa28d584ccc3030fc5f1" console.out.writeline("decrypted: {0}", sdecrypted) ' "my super secret data" end sub public class mycrypto private shared textencoding text.encoding = text.encoding.utf8 private cipherengine system.security.cryptography.symmetricalgorithm ' note: unlike in question, same key , iv reused messages. private cipherkey() byte private cipheriv() byte public sub new(byval skeypreimage string) dim abkeypreimage() byte = textencoding.getbytes(skeypreimage) dim abkeysalt() byte = textencoding.getbytes("ivan medvedev") const keyderivationrounds integer = 1 << 12 dim okeyderivationengine new system.security.cryptography.rfc2898derivebytes(abkeypreimage, abkeysalt, keyderivationrounds) me.cipherengine = system.security.cryptography.aes.create() me.cipherengine.padding = security.cryptography.paddingmode.pkcs7 me.cipherkey = okeyderivationengine.getbytes(me.cipherengine.keysize >> 3) me.cipheriv = okeyderivationengine.getbytes(me.cipherengine.blocksize >> 3) end sub public function encrypt(byval abplaintext() byte) byte() dim abciphertext() byte using hstreamsource new system.io.memorystream(abplaintext), hstreamcipher new system.security.cryptography.cryptostream( hstreamsource, me.cipherengine.createencryptor(me.cipherkey, me.cipheriv), security.cryptography.cryptostreammode.read), hstreamtarget new system.io.memorystream hstreamcipher.copyto(hstreamtarget) abciphertext = hstreamtarget.toarray() end using return abciphertext end function public function decrypt(byval abciphertext() byte) byte() dim abplaintext() byte using hstreamsource new system.io.memorystream(abciphertext), hstreamcipher new system.security.cryptography.cryptostream( hstreamsource, me.cipherengine.createdecryptor(me.cipherkey, me.cipheriv), security.cryptography.cryptostreammode.read), hstreamtarget new system.io.memorystream hstreamcipher.copyto(hstreamtarget) abplaintext = hstreamtarget.toarray() end using return abplaintext end function public function encrypttext(byval splaintext string) string dim abplaintext() byte = textencoding.getbytes(splaintext) dim abciphertext() byte = me.encrypt(abplaintext) dim sciphertext string = hex.format(abciphertext) return sciphertext end function public function decrypttext(byval sciphertext string) string dim abciphertext() byte = hex.parse(sciphertext) dim abplaintext() byte = me.decrypt(abciphertext) dim splaintext string = textencoding.getchars(abplaintext) return splaintext end function end class public class hex public shared function format(byval abvalue() byte) string dim aschars(0 abvalue.length * 2 - 1) char dim ndxchar integer = 0 ndxbyte integer = 0 abvalue.length - 1 dim bnibblehi byte = abvalue(ndxbyte) >> 4, bnibblelo byte = cbyte(abvalue(ndxbyte) , &hfus) aschars(ndxchar) = convert.tochar(if(bnibblehi <= 9, &h30us + bnibblehi, &h37us + bnibblehi)) : ndxchar += 1 aschars(ndxchar) = convert.tochar(if(bnibblelo <= 9, &h30us + bnibblelo, &h37us + bnibblelo)) : ndxchar += 1 next return new string(aschars) end function public shared function parse(byval svalue string) byte() if string.isnullorempty(svalue) return new byte() {} if (svalue.length mod 2) > 0 return nothing dim ndxtext integer = 0 dim ndxbytemax integer = (svalue.length \ 2) - 1 dim abvalue(0 ndxbytemax) byte try ndxbyte integer = 0 ndxbytemax abvalue(ndxbyte) = convert.tobyte(svalue.substring(ndxtext, 2), 16) ndxtext += 2 next catch ex exception return nothing end try return abvalue end function end class again, please, note example. not endorsing kind of protection techniques shown here, because task remains unknown. code above illustrates syntax , semantics — not how right.
Comments
Post a Comment