Getting variable encryption results with VB.Net and DES -
i'm working on semi-internal encryption process sensitive information. email addresses , like. i'm working few other developers @ sister companies on project, , requirements everyone's encryption can talk else's. use global password, encrypt , decrypt information onsite, , that's it.
my problem encryption procedure, while matching theirs, giving me variable results. i'm polling our sql server strings encrypted in question, iterating through array of results, , updating server encrypted strings.
the problem first string different subsequent strings, , isn't recognized valid testing software we're supposed basing our solution off of. second , subsequent strings come through fine.
example:
test@test.com - brpurplww7+vyrr5puj/jhxoip/mv5wr test@test.com - brpurplww79h+n4tgot0xrmm7sdwqqsy test@test.com - brpurplww79h+n4tgot0xrmm7sdwqqsy
i can't quite figure out what's going on, because can encrypt , decrypt , forth on own machine no issues. advice lovely.
my encryption function follows:
private tripledes new descryptoserviceprovider sub new(byval key string) dim ivhash(), keyhash() byte keyhash = system.text.encoding.utf8.getbytes(key) redim preserve keyhash(7) tripledes.key = keyhash ivhash = system.text.encoding.utf8.getbytes(string.empty) redim preserve ivhash(7) tripledes.iv = ivhash end sub public function encryptdata(byval plaintext string) string dim plaintextbytes() byte = system.text.encoding.utf8.getbytes(plaintext) dim ms new system.io.memorystream dim encstream new cryptostream(ms, tripledes.createencryptor(), system.security.cryptography.cryptostreammode.write) tripledes.mode = ciphermode.ecb encstream.write(plaintextbytes, 0, plaintextbytes.length) encstream.flushfinalblock() return convert.tobase64string(ms.toarray) end function public function decryptdata(byval encryptedtext string) string dim encryptedbytes() byte = convert.frombase64string(encryptedtext) dim ms new system.io.memorystream dim decstream new cryptostream(ms, tripledes.createdecryptor(), system.security.cryptography.cryptostreammode.write) tripledes.mode = ciphermode.ecb decstream.write(encryptedbytes, 0, encryptedbytes.length) decstream.flushfinalblock() return system.text.encoding.utf8.getstring(ms.toarray) end function
you setting tripledes.mode = ciphermode.ecb
after have called tripledes.createencryptor()
, first encryption using default value of ciphermode.cbc
. since tripledes
reused, after first call encryptdata
mode
set correctly.
move tripledes.mode = ciphermode.ecb
new
, should work consistently.
Comments
Post a Comment