.net - MemoryStream: Encoding -


i'm trying encrypt long text using public , private keys of x509 certificate. specifically, i'm trying reproduce msdn code: https://msdn.microsoft.com/en-us/library/system.security.cryptography.x509certificates.publickey(v=vs.90).aspx

however, instead of encrypting file, want encrypt string. i'm trying adapt code use memorystreams.

the problem every time try read content of stream, has strange caracters. when try call webservice (asmx) , sending cipher text, crashes.

this code. have tried using utf32, utf8, utf7 , ascii.

private shared function encrypttext(byval token string, byval rsapublickey security.cryptography.rsacryptoserviceprovider) string     dim aesmanaged new security.cryptography.aesmanaged()     dim transform security.cryptography.icryptotransform = nothing     dim outstreamencrypted security.cryptography.cryptostream = nothing     dim outfsaux new io.memorystream()     dim outfs new io.streamwriter(outfsaux, text.encoding.ascii)     dim infs new io.memorystream(text.encoding.ascii.getbytes(token))     dim ciphertext string = string.empty       try         aesmanaged.keysize = 256         aesmanaged.blocksize = 128         aesmanaged.mode = security.cryptography.ciphermode.cbc          dim keyformatter new security.cryptography.rsapkcs1keyexchangeformatter(rsapublickey)         dim keyencrypted byte() = keyformatter.createkeyexchange(aesmanaged.key, aesmanaged.gettype())          dim lenk(3) byte         dim leniv(3) byte          dim lkey integer = keyencrypted.length         lenk = bitconverter.getbytes(lkey)         dim liv integer = aesmanaged.iv.length         leniv = bitconverter.getbytes(liv)          outfs.write(lenk)         outfs.write(leniv)         outfs.write(keyencrypted)         outfs.write(aesmanaged.iv)          transform = aesmanaged.createencryptor()          outstreamencrypted = new security.cryptography.cryptostream(outfs.basestream, transform, security.cryptography.cryptostreammode.write)         dim count integer = 0         dim offset integer = 0          dim blocksizebytes integer = aesmanaged.blocksize / 8         dim data(blocksizebytes) byte         dim bytesread integer = 0                      count = infs.read(data, 0, blocksizebytes)             offset += count             outstreamencrypted.write(data, 0, count)             bytesread += blocksizebytes         loop while count > 0          infs.flush()         outstreamencrypted.flushfinalblock()          outfsaux.position = 0         ciphertext = system.convert.tobase64string(outfsaux.toarray())             if infs isnot nothing infs.dispose()         if outfs isnot nothing outfs.dispose()         if outstreamencrypted isnot nothing outstreamencrypted.dispose()         if transform isnot nothing transform.dispose()     end try      return ciphertext end function 

upgrade:

thanks help. have updated above code too.

now can correctly encrypt text. however, still need help, adapt decrypt method. crashes when tries create variable keydecrypted

i know problem that, on encrypt method, loads info @ begining of cipher string, decrypt method cannot recover after base64 conversion.

  private shared function decrypttext(byval token string, byval rsaprivatekey security.cryptography.rsacryptoserviceprovider) string     dim aesmanaged new security.cryptography.aesmanaged()     dim infs io.memorystream = new io.memorystream(convert.frombase64string(token))     dim outfs new io.memorystream     dim outstreamdecrypted security.cryptography.cryptostream = nothing      dim deciphertext string = string.empty      aesmanaged.keysize = 256     aesmanaged.blocksize = 128     aesmanaged.mode = security.cryptography.ciphermode.cbc      dim lenk() byte = new byte(4 - 1) {}     dim leniv() byte = new byte(4 - 1) {}       infs.seek(0, io.seekorigin.begin)     infs.seek(0, io.seekorigin.begin)     infs.read(lenk, 0, 3)     infs.seek(4, io.seekorigin.begin)     infs.read(leniv, 0, 3)      dim lengthk integer = bitconverter.toint32(lenk, 0)     dim lengthiv integer = bitconverter.toint32(leniv, 0)      dim startc integer = lengthk + lengthiv + 8     dim lenc integer = (ctype(infs.length, integer) - startc)      dim keyencrypted() byte = new byte(lengthk - 1) {}     dim iv() byte = new byte(lengthiv - 1) {}      infs.seek(8, io.seekorigin.begin)     infs.read(keyencrypted, 0, lengthk)     infs.seek(8 + lengthk, io.seekorigin.begin)     infs.read(iv, 0, lengthiv)      dim keydecrypted byte() = rsaprivatekey.decrypt(keyencrypted, false)     dim transform security.cryptography.icryptotransform = aesmanaged.createdecryptor(keydecrypted, iv)       dim count integer = 0     dim offset integer = 0      dim blocksizebytes integer = aesmanaged.blocksize / 8     dim data(blocksizebytes) byte      infs.seek(startc, io.seekorigin.begin)      outstreamdecrypted = new security.cryptography.cryptostream(outfs, transform, security.cryptography.cryptostreammode.write)             count = infs.read(data, 0, blocksizebytes)         offset += count         outstreamdecrypted.write(data, 0, count)     loop while count > 0      outfs.position = 0     using rd new io.streamreader(outfs)         deciphertext = rd.readtoend()     end using      return deciphertext end function 

the cipher text contain unprintable characters use whole range 0 255 each byte. if want send cipher text string base64 encode cipher text first.

change

rd = new io.streamreader(outfsaux, text.encoding.ascii) ciphertext = rd.readtoend() 

to

ciphertext = system.convert.tobase64string(outfsaux.toarray()) 

the code syntax may little off don't program in vb.


Comments

Popular posts from this blog

java - Andrioid studio start fail: Fatal error initializing 'null' -

android - Gradle sync Error:Configuration with name 'default' not found -

StringGrid issue in Delphi XE8 firemonkey mobile app -