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. General Programming
  3. C#
  4. I have a problem with Convert.FromBase64String

I have a problem with Convert.FromBase64String

Scheduled Pinned Locked Moved C#
debugginghelpquestion
7 Posts 5 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.
  • R Offline
    R Offline
    Roshanakak
    wrote on last edited by
    #1

    I have this code in my project: string str; byte[] enc = new byte[str.length]; enc = Convert.FromBase64String(str); when i trace it, in the 2nd line the enc is a byte with the length of str but when 3rd line executes the enc length becomes 1 and therefore my data is not valid at the end. does anybody know what should i do? Roshanak

    CPalliniC 1 J 3 Replies Last reply
    0
    • R Roshanakak

      I have this code in my project: string str; byte[] enc = new byte[str.length]; enc = Convert.FromBase64String(str); when i trace it, in the 2nd line the enc is a byte with the length of str but when 3rd line executes the enc length becomes 1 and therefore my data is not valid at the end. does anybody know what should i do? Roshanak

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      You should know that the base64 encoding is a bit verbose. I.e. Silly question, silly answer (i.e. please post input values, expected output, actual output if you want better help). :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      In testa che avete, signor di Ceprano?

      1 Reply Last reply
      0
      • R Roshanakak

        I have this code in my project: string str; byte[] enc = new byte[str.length]; enc = Convert.FromBase64String(str); when i trace it, in the 2nd line the enc is a byte with the length of str but when 3rd line executes the enc length becomes 1 and therefore my data is not valid at the end. does anybody know what should i do? Roshanak

        1 Offline
        1 Offline
        12Code
        wrote on last edited by
        #3

        You didn't assign any string to str. What do you expect enc should be?

        ;)*12Code

        1 Reply Last reply
        0
        • R Roshanakak

          I have this code in my project: string str; byte[] enc = new byte[str.length]; enc = Convert.FromBase64String(str); when i trace it, in the 2nd line the enc is a byte with the length of str but when 3rd line executes the enc length becomes 1 and therefore my data is not valid at the end. does anybody know what should i do? Roshanak

          J Offline
          J Offline
          J a a n s
          wrote on last edited by
          #4

          The assignment part of second line is not having any use. Your field will be having the return value from FromBase64String method. Also the length of encrypted string and decrypted string wont be same.

          R 1 Reply Last reply
          0
          • J J a a n s

            The assignment part of second line is not having any use. Your field will be having the return value from FromBase64String method. Also the length of encrypted string and decrypted string wont be same.

            R Offline
            R Offline
            Roshanakak
            wrote on last edited by
            #5

            I'm trying to encrypt sth, and those lines were just a part of my code. I'm sorry for not being clear but i wanted to be brief, this is the whole code: using System; using System.IO; using System.Text; using System.Security.Cryptography; namespace Cryptography { public sealed class Cryption { private RijndaelManaged Algorithm; private string m_key; private string m_iv; private byte[] key; private byte[] iv; public Cryption(string key_val, string iv_val) { key = new byte[32]; iv = new byte[32]; int i; m_key = key_val; m_iv = iv_val; for (i = 0; i < m_key.Length; i++) { key[i] = Convert.ToByte(m_key[i]); } for (i = 0; i < m_iv.Length; i++) { iv[i] = Convert.ToByte(m_iv[i]); } } public string Encrypt(string s) { string enc_str; byte[] enc_byte; Algorithm = new RijndaelManaged(); Algorithm.BlockSize = 256; Algorithm.KeySize = 256; Algorithm.Padding = PaddingMode.PKCS7; using (MemoryStream memStream = new MemoryStream()) { using (ICryptoTransform EncryptorDecryptor = Algorithm.CreateEncryptor(key, iv)) { using (CryptoStream crStream = new CryptoStream(memStream, EncryptorDecryptor, CryptoStreamMode.Write)) { UTF8Encoding utf8enc = new UTF8Encoding(); enc_byte = utf8enc.GetBytes(s); crStream.Write(enc_byte, 0, enc_byte.Length); crStream.FlushFinalBlock(); } } } enc_str = Convert.ToBase64String(enc_byte); return enc_str; } public string Decrypt(string s) { string dec_str; //byte[] dec_byte; byte[] decrepted_byte; Algorithm = new RijndaelManaged(); Algorithm.BlockSize = 256; Algorithm.KeySize = 256; Algorithm.Padding = PaddingMode.PKCS7; byte[] dec_byte = new byte[s.Length]; dec_byte = Convert.FromBase64String(s); decrepted_byte = new byte[dec_byte.Length]; us

            L J 2 Replies Last reply
            0
            • R Roshanakak

              I'm trying to encrypt sth, and those lines were just a part of my code. I'm sorry for not being clear but i wanted to be brief, this is the whole code: using System; using System.IO; using System.Text; using System.Security.Cryptography; namespace Cryptography { public sealed class Cryption { private RijndaelManaged Algorithm; private string m_key; private string m_iv; private byte[] key; private byte[] iv; public Cryption(string key_val, string iv_val) { key = new byte[32]; iv = new byte[32]; int i; m_key = key_val; m_iv = iv_val; for (i = 0; i < m_key.Length; i++) { key[i] = Convert.ToByte(m_key[i]); } for (i = 0; i < m_iv.Length; i++) { iv[i] = Convert.ToByte(m_iv[i]); } } public string Encrypt(string s) { string enc_str; byte[] enc_byte; Algorithm = new RijndaelManaged(); Algorithm.BlockSize = 256; Algorithm.KeySize = 256; Algorithm.Padding = PaddingMode.PKCS7; using (MemoryStream memStream = new MemoryStream()) { using (ICryptoTransform EncryptorDecryptor = Algorithm.CreateEncryptor(key, iv)) { using (CryptoStream crStream = new CryptoStream(memStream, EncryptorDecryptor, CryptoStreamMode.Write)) { UTF8Encoding utf8enc = new UTF8Encoding(); enc_byte = utf8enc.GetBytes(s); crStream.Write(enc_byte, 0, enc_byte.Length); crStream.FlushFinalBlock(); } } } enc_str = Convert.ToBase64String(enc_byte); return enc_str; } public string Decrypt(string s) { string dec_str; //byte[] dec_byte; byte[] decrepted_byte; Algorithm = new RijndaelManaged(); Algorithm.BlockSize = 256; Algorithm.KeySize = 256; Algorithm.Padding = PaddingMode.PKCS7; byte[] dec_byte = new byte[s.Length]; dec_byte = Convert.FromBase64String(s); decrepted_byte = new byte[dec_byte.Length]; us

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              Roshanakak wrote:

              none of the answers could help me!

              because you gave fictitious code to start with. :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


              1 Reply Last reply
              0
              • R Roshanakak

                I'm trying to encrypt sth, and those lines were just a part of my code. I'm sorry for not being clear but i wanted to be brief, this is the whole code: using System; using System.IO; using System.Text; using System.Security.Cryptography; namespace Cryptography { public sealed class Cryption { private RijndaelManaged Algorithm; private string m_key; private string m_iv; private byte[] key; private byte[] iv; public Cryption(string key_val, string iv_val) { key = new byte[32]; iv = new byte[32]; int i; m_key = key_val; m_iv = iv_val; for (i = 0; i < m_key.Length; i++) { key[i] = Convert.ToByte(m_key[i]); } for (i = 0; i < m_iv.Length; i++) { iv[i] = Convert.ToByte(m_iv[i]); } } public string Encrypt(string s) { string enc_str; byte[] enc_byte; Algorithm = new RijndaelManaged(); Algorithm.BlockSize = 256; Algorithm.KeySize = 256; Algorithm.Padding = PaddingMode.PKCS7; using (MemoryStream memStream = new MemoryStream()) { using (ICryptoTransform EncryptorDecryptor = Algorithm.CreateEncryptor(key, iv)) { using (CryptoStream crStream = new CryptoStream(memStream, EncryptorDecryptor, CryptoStreamMode.Write)) { UTF8Encoding utf8enc = new UTF8Encoding(); enc_byte = utf8enc.GetBytes(s); crStream.Write(enc_byte, 0, enc_byte.Length); crStream.FlushFinalBlock(); } } } enc_str = Convert.ToBase64String(enc_byte); return enc_str; } public string Decrypt(string s) { string dec_str; //byte[] dec_byte; byte[] decrepted_byte; Algorithm = new RijndaelManaged(); Algorithm.BlockSize = 256; Algorithm.KeySize = 256; Algorithm.Padding = PaddingMode.PKCS7; byte[] dec_byte = new byte[s.Length]; dec_byte = Convert.FromBase64String(s); decrepted_byte = new byte[dec_byte.Length]; us

                J Offline
                J Offline
                J a a n s
                wrote on last edited by
                #7

                Please check this link. http://www.obviex.com/samples/Code.aspx?Source=EncryptionCS&Title=Symmetric%20Key%20Encryption&Lang=C%23[^]

                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