Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. byte[] to string

byte[] to string

Scheduled Pinned Locked Moved ASP.NET
helpregex
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    M Ambigai
    wrote on last edited by
    #1

    sir, Just to encrypt a string, i used this code. code: public string encryptText(string plaintext,string passpharse,string saltValue,string hashAlgorithm,int passwordIterations,string initvector,int keysize) { byte[] InitVectorBytes = Encoding.ASCII.GetBytes(initvector); byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue); byte[] plaintextbytes = Encoding.ASCII.GetBytes(plaintext); PasswordDeriveBytes password = new PasswordDeriveBytes(passpharse, saltValueBytes, hashAlgorithm, passwordIterations); byte[] keyBytes = password.GetBytes(keysize / 8); RijndaelManaged symmetricKey = new RijndaelManaged(); symmetricKey.Mode = CipherMode.CBC; ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, InitVectorBytes); MemoryStream memorystream = new MemoryStream(); CryptoStream cryptostream = new CryptoStream(memorystream, encryptor, CryptoStreamMode.Write); cryptostream.Write(plaintextbytes, 0, plaintextbytes.Length); cryptostream.FlushFinalBlock(); byte[] ciphertextbytes = memorystream.ToArray(); memorystream.Close(); cryptostream.Close(); string ciphertext = Convert.FromBase64String(ciphertextbytes); return ciphertext; } I've got the following errors: Error 1 The best overloaded method match for 'System.Convert.FromBase64String(string)' has some invalid arguments.54,29 Error 2 Argument '1': cannot convert from 'byte[]' to 'string'.54,54 Please help me to resolve it.

    S Y 2 Replies Last reply
    0
    • M M Ambigai

      sir, Just to encrypt a string, i used this code. code: public string encryptText(string plaintext,string passpharse,string saltValue,string hashAlgorithm,int passwordIterations,string initvector,int keysize) { byte[] InitVectorBytes = Encoding.ASCII.GetBytes(initvector); byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue); byte[] plaintextbytes = Encoding.ASCII.GetBytes(plaintext); PasswordDeriveBytes password = new PasswordDeriveBytes(passpharse, saltValueBytes, hashAlgorithm, passwordIterations); byte[] keyBytes = password.GetBytes(keysize / 8); RijndaelManaged symmetricKey = new RijndaelManaged(); symmetricKey.Mode = CipherMode.CBC; ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, InitVectorBytes); MemoryStream memorystream = new MemoryStream(); CryptoStream cryptostream = new CryptoStream(memorystream, encryptor, CryptoStreamMode.Write); cryptostream.Write(plaintextbytes, 0, plaintextbytes.Length); cryptostream.FlushFinalBlock(); byte[] ciphertextbytes = memorystream.ToArray(); memorystream.Close(); cryptostream.Close(); string ciphertext = Convert.FromBase64String(ciphertextbytes); return ciphertext; } I've got the following errors: Error 1 The best overloaded method match for 'System.Convert.FromBase64String(string)' has some invalid arguments.54,29 Error 2 Argument '1': cannot convert from 'byte[]' to 'string'.54,54 Please help me to resolve it.

      S Offline
      S Offline
      shawty_ds
      wrote on last edited by
      #2

      You've declared ciperTextBytes as a byte array, when it needs to be a string. ConvertFromBase64String(string) is telling you that you need to pass the parameter to it as a string data type. I'm not 100% sure with out testing it, but string ciphertext = Convert.FromBase64String(ciphertextbytes.ToString()); !!MIGHT!! work. Cheers Shawty

      M 1 Reply Last reply
      0
      • S shawty_ds

        You've declared ciperTextBytes as a byte array, when it needs to be a string. ConvertFromBase64String(string) is telling you that you need to pass the parameter to it as a string data type. I'm not 100% sure with out testing it, but string ciphertext = Convert.FromBase64String(ciphertextbytes.ToString()); !!MIGHT!! work. Cheers Shawty

        M Offline
        M Offline
        M Ambigai
        wrote on last edited by
        #3

        No sir . Its not working

        S 1 Reply Last reply
        0
        • M M Ambigai

          sir, Just to encrypt a string, i used this code. code: public string encryptText(string plaintext,string passpharse,string saltValue,string hashAlgorithm,int passwordIterations,string initvector,int keysize) { byte[] InitVectorBytes = Encoding.ASCII.GetBytes(initvector); byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue); byte[] plaintextbytes = Encoding.ASCII.GetBytes(plaintext); PasswordDeriveBytes password = new PasswordDeriveBytes(passpharse, saltValueBytes, hashAlgorithm, passwordIterations); byte[] keyBytes = password.GetBytes(keysize / 8); RijndaelManaged symmetricKey = new RijndaelManaged(); symmetricKey.Mode = CipherMode.CBC; ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, InitVectorBytes); MemoryStream memorystream = new MemoryStream(); CryptoStream cryptostream = new CryptoStream(memorystream, encryptor, CryptoStreamMode.Write); cryptostream.Write(plaintextbytes, 0, plaintextbytes.Length); cryptostream.FlushFinalBlock(); byte[] ciphertextbytes = memorystream.ToArray(); memorystream.Close(); cryptostream.Close(); string ciphertext = Convert.FromBase64String(ciphertextbytes); return ciphertext; } I've got the following errors: Error 1 The best overloaded method match for 'System.Convert.FromBase64String(string)' has some invalid arguments.54,29 Error 2 Argument '1': cannot convert from 'byte[]' to 'string'.54,54 Please help me to resolve it.

          Y Offline
          Y Offline
          Yusuf
          wrote on last edited by
          #4

          If you can write encryption code, I fail to understand why you have a problem with such trivial error. At least you tell me what your code is trying to do, and why you are impementing it this way. :suss:

          Yusuf Oh didn't you notice, analogous to square roots, they recently introduced rectangular, circular, and diamond roots to determine the size of the corresponding shapes when given the area. Luc Pattyn[^]

          1 Reply Last reply
          0
          • M M Ambigai

            No sir . Its not working

            S Offline
            S Offline
            shawty_ds
            wrote on last edited by
            #5

            You need to describe it better than that i'm afraid. the .ToString()??? the function??? the routine??? try to convert your byte array to a string before hand. It's fairly easy. All you need to do is iterate through the array collection and convert each byte to a char before adding it to a string. Cheers Shawty

            M 1 Reply Last reply
            0
            • S shawty_ds

              You need to describe it better than that i'm afraid. the .ToString()??? the function??? the routine??? try to convert your byte array to a string before hand. It's fairly easy. All you need to do is iterate through the array collection and convert each byte to a char before adding it to a string. Cheers Shawty

              M Offline
              M Offline
              M Ambigai
              wrote on last edited by
              #6

              Sir, please explain it in detail

              S 1 Reply Last reply
              0
              • M M Ambigai

                Sir, please explain it in detail

                S Offline
                S Offline
                shawty_ds
                wrote on last edited by
                #7

                There's not much to it. You just need to declare a string, make it empty : String [myvariablename] = "" then you just need to loop over your byte array : foreach(byte [mybytevariablename] in [mybytearrayname]) then in the loop just concatenate each element. [myvariablename] = [myvariablename] + [mybytevariablename] When your finished, then the string you defind should be a string of all the chars in your byte array which you can then pass to the function. replace the [..] above as appropriate for your variable names where you've defined them.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups