aes - How to make Rijndael CBC mode work in vb.net -
i'm trying make rijndael work in cbc mode. i'm not sure how should it. think problem in current code stream initialized every time in beginning of encryption, no avalanche effect occurs (same data encrypted twice , output of 2 encryption same should not be).
i tried initialize cryptostream once coded crashed because canwrite property of cryptostream goes false after first write cryptostream.
here code have now:
sub main() dim rij new rijndaelmanaged dim iv(15) byte dim key(15) byte dim secret() byte = {59, 60, 61} dim cs icryptotransform dim cstream cryptostream dim out() byte dim newrandom new rngcryptoserviceprovider() newrandom.getbytes(iv) newrandom.getbytes(key) rij = new rijndaelmanaged() rij.keysize = 128 rij.padding = paddingmode.pkcs7 rij.mode = ciphermode.cbc rij.iv = iv rij.key = key cs = rij.createencryptor() dim ms_in new memorystream cstream = new cryptostream(ms_in, cs, cryptostreammode.write) using cstream cstream.write(secret, 0, 3) end using out = ms_in.toarray console.writeline(arraytostring(out, out.length)) erase out ms_in = new memorystream cstream = new cryptostream(ms_in, cs, cryptostreammode.write) using cstream cstream.write(secret, 0, 3) end using out = ms_in.toarray console.writeline(arraytostring(out, out.length)) end sub
and conversion function convert array string
public function arraytostring(byval bytes() byte, byval length integer) string if bytes.length = 0 return string.empty dim sb new system.text.stringbuilder(length) dim k integer = length - 1 dim integer = 0 k sb.append(chr(bytes(i))) next return sb.tostring() end function
this need:
cs = rij.createencryptor() dim ms_in new memorystream cstream = new cryptostream(ms_in, cs, cryptostreammode.write) using cstream cstream.write(secret, 0, 3) 'encrypt end using out = ms_in.toarray console.writeline(arraytostring(out, out.length)) 'see encrypted message erase out using cstream cstream.write(secret, 0, 3) 'encrypt, crash here , problem i'm trying solve end using out = ms_in.toarray console.writeline(arraytostring(out, out.length)) 'see encrypted message should not same first 1
try this:
public sub run() dim key() byte = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15} dim plaintext1 byte() = {59, 60, 61} dim plaintext2 byte() = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, _ 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, _ 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, _ 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 _ } roundtrip(plaintext1, key) system.console.writeline() roundtrip(plaintext2, key) end sub public sub roundtrip(byref plaintext byte(), byref key byte()) dim rij new rijndaelmanaged dim iv(15) byte dim encryptor icryptotransform dim decryptor icryptotransform dim out() byte 'dim newrandom new rngcryptoserviceprovider() 'newrandom.getbytes(iv) 'newrandom.getbytes(key) console.writeline("original:") console.writeline(arraytostring(plaintext)) system.console.writeline() rij = new rijndaelmanaged() rij.keysize = key.length * 8 ' 16 byte key == 128 bits rij.padding = paddingmode.pkcs7 rij.mode = ciphermode.cbc rij.iv = iv rij.key = key encryptor = rij.createencryptor() using msin = new memorystream using cstream = new cryptostream(msin, encryptor, cryptostreammode.write) cstream.write(plaintext, 0, plaintext.length) end using out = msin.toarray console.writeline("encrypted:") console.writeline("{0}", arraytostring(out)) system.console.writeline() end using decryptor = rij.createdecryptor() using msin = new memorystream using cstream = new cryptostream(msin, decryptor, cryptostreammode.write) cstream.write(out, 0, out.length) end using out = msin.toarray console.writeline("decrypted: ") console.writeline("{0}", arraytostring(out)) system.console.writeline() end using end sub public shared function arraytostring(byval bytes byte()) string dim sb new system.text.stringbuilder() dim integer = 0 bytes.length-1 if (i <> 0 , mod 16 = 0) sb.append(environment.newline) end if sb.append(system.string.format("{0:x2} ", bytes(i))) next return sb.tostring().trim() end function
i made these basic changes work:
- create decryptor
- properly manage buffers , streams (see
using
clauses added)
i re-organized little, , modified code use constant iv (all zeros) , use constant key. can repeatable results 1 run next. in real app use randomized iv , use password-derived key. (see rfc2898derivebytes)
ok, allows code compile. think want see effect of chaining. not easy, maybe show want see:
integer = 1 2 using ms = new memorystream using cstream = new cryptostream(ms, encryptor, cryptostreammode.write) j integer = 1 cstream.write(plaintext, 0, plaintext.length) next j end using out = ms.toarray console.writeline("encrypted (cycle {0}):", i) console.writeline("{0}", arraytostring(out)) system.console.writeline() end using next
Comments
Post a Comment