.net - AES-functions always reply with empty results -
today i've got question current visual basic project i'm going for. intention serve 1 or more encrypted configuration-files program in same directory executable placed in. because (en/de)cryption processed twice (username , password), want (en/de)crypt directly string string. hope reduce hassles temporary files , might useful later on.
with of great site , tutorial came far, @ least compiler doesn't complain anymore. :) far, good...
my code following:
private function producekeyandiv(byval password string) object() dim bytearray(password.length) byte, key(31) byte, iv(15) byte bytearray = system.text.encoding.ascii.getbytes(password) dim sha_function new system.security.cryptography.sha512managed dim hashresult byte() = sha_function.computehash(bytearray) array.copy(hashresult, key, 32) array.copy(hashresult, 32, iv, 0, 16) dim obj(2) object obj(0) = key obj(1) = iv return obj end function private function generatestreamfromstring(byval mystring string) stream dim ret_stream memorystream = new memorystream() dim stream_writer streamwriter = new streamwriter(ret_stream, encoding.default) stream_writer.write(mystring) stream_writer.flush() ret_stream.position = 0 return ret_stream end function public function decryptstringreturnstring(byval encryptedstring string, byval key string) string dim cryptographyparameters object() = producekeyandiv(key) try dim bytebuffer(4096) byte dim memorystreamin stream = generatestreamfromstring(encryptedstring) dim memorystreamout memorystream = new memorystream() dim positioninstring long = 0 dim stringlength long = encryptedstring.length() dim bytesinblockprocessed integer = 0 dim cryptserviceprovrijn new system.security.cryptography.rijndaelmanaged using scryptostream = new cryptostream(memorystreamout, _ cryptserviceprovrijn.createdecryptor(cryptographyparameters(0), cryptographyparameters(1)), _ cryptostreammode.write) while positioninstring < stringlength bytesinblockprocessed = memorystreamin.read(bytebuffer, 0, 4096) scryptostream.write(bytebuffer, 0, bytesinblockprocessed) positioninstring = positioninstring + (bytesinblockprocessed) end while end using memorystreamin.close() memorystreamout.position = 0 dim stream_reader streamreader = new streamreader(memorystreamout) return stream_reader.readtoend() catch ex exception end try return nothing end function public function encryptstringreturnstring(byval decryptedstring string, byval key string) string dim cryptographyparameters object() = producekeyandiv(key) try dim bytebuffer(4096) byte dim memorystreamin stream = generatestreamfromstring(decryptedstring) dim memorystreamout memorystream = new memorystream() dim positioninstring long = 0 dim stringlength long = decryptedstring.length() dim bytesinblockprocessed integer = 0 dim cryptserviceprovrijn new system.security.cryptography.rijndaelmanaged using scryptostream = new cryptostream(memorystreamout, _ cryptserviceprovrijn.createencryptor(cryptographyparameters(0), cryptographyparameters(1)), _ cryptostreammode.write) while positioninstring < stringlength bytesinblockprocessed = memorystreamin.read(bytebuffer, 0, 4096) scryptostream.write(bytebuffer, 0, bytesinblockprocessed) positioninstring = positioninstring + (bytesinblockprocessed) end while memorystreamin.close() memorystreamout.position = 0 dim stream_reader streamreader = new streamreader(memorystreamout) return stream_reader.readtoend() end using catch ex exception end try return nothing end function
the problem if call function , let me print give results, empty. maybe else got idea why? :)
thanks in advance help...
useful resources:
there instruction missing. according this website flushfinalblock() needed, in order finalize changes made cryptostream. people on there recommend store data in byte-arrays instead of converted strings. will in further developement, in end, if configuration needs plain readable converted "readable" text.
heres "fixed" version of previous source code:
imports system.io imports system.security.cryptography imports system.text module sandbox private function producekeyandiv(byval password string) object() dim bytearray(password.length) byte, key(31) byte, iv(15) byte bytearray = system.text.encoding.ascii.getbytes(password) dim sha_function new system.security.cryptography.sha512managed dim hashresult byte() = sha_function.computehash(bytearray) array.copy(hashresult, key, 32) array.copy(hashresult, 32, iv, 0, 16) dim obj(2) object obj(0) = key obj(1) = iv return obj end function private function generatestreamfromstring(byval mystring string) stream dim ret_stream memorystream = new memorystream() dim stream_writer streamwriter = new streamwriter(ret_stream, encoding.default) stream_writer.write(mystring) stream_writer.flush() ret_stream.position = 0 return ret_stream end function public function decryptbytearrayreturnbytearray(byval encrypted byte(), byval key string) byte() dim cryptographyparameters object() = producekeyandiv(key) try dim bytebuffer(4096) byte dim memorystreamin stream = new memorystream(encrypted) dim memorystreamout memorystream = new memorystream() dim position long = 0 dim bytesinblockprocessed integer = 0 dim cryptserviceprovrijn new system.security.cryptography.rijndaelmanaged using scryptostream = new cryptostream(memorystreamout, _ cryptserviceprovrijn.createdecryptor(cryptographyparameters(0), cryptographyparameters(1)), _ cryptostreammode.write) while position < encrypted.count() bytesinblockprocessed = memorystreamin.read(bytebuffer, 0, 4096) scryptostream.write(bytebuffer, 0, bytesinblockprocessed) scryptostream.flush() position = position + (bytesinblockprocessed) end while memorystreamin.close() scryptostream.flushfinalblock() ' needed close cryptostream (a simple flush not enough) return memorystreamout.toarray ' independent actual streamposition end using catch ex exception end try return nothing end function public function encryptbytearrayreturnbytearray(byval decrypted byte(), byval key string) byte() dim cryptographyparameters object() = producekeyandiv(key) try dim bytebuffer(4096) byte dim memorystreamin stream = new memorystream(decrypted) dim memorystreamout memorystream = new memorystream() dim position long = 0 dim bytesinblockprocessed integer = 0 dim cryptserviceprovrijn new system.security.cryptography.rijndaelmanaged using scryptostream = new cryptostream(memorystreamout, _ cryptserviceprovrijn.createencryptor(cryptographyparameters(0), cryptographyparameters(1)), _ cryptostreammode.write) while position < decrypted.count() bytesinblockprocessed = memorystreamin.read(bytebuffer, 0, 4096) scryptostream.write(bytebuffer, 0, bytesinblockprocessed) scryptostream.flush() position = position + (bytesinblockprocessed) end while memorystreamin.close() scryptostream.flushfinalblock() ' needed close cryptostream (a simple flush not enough) return memorystreamout.toarray ' independent actual streamposition end using catch ex exception end try return nothing end function sub main() console.write("passwort: ") dim pwd = console.readline() dim x string = new string((system.text.encoding.default.getchars(decryptbytearrayreturnbytearray(encryptbytearrayreturnbytearray(system.text.encoding.default.getbytes(pwd), pwd), pwd)))) console.write(x) console.readline() end sub end module
if think there issues or flaws in code, please point them out me. i'd appreciate much. :)
Comments
Post a Comment