I have a problem with Convert.FromBase64String
-
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 -
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? RoshanakYou 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] -
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 -
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 -
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.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
-
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
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
-
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