My Requirement is i need to convert base64 to encrypted value
encrypt value to url
url to Image visible
when i read the url the image should be display how to do it.
My Requirement is i need to convert base64 to encrypted value
encrypt value to url
url to Image visible
when i read the url the image should be display how to do it.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Vishal JoshiRank #377 · 6.7K reputationPosted Mar 15, 2023, 7:47 AM EDT
Hello Jaya
Please type code like the below sample.
public Image LoadImage() { //data:image/gif;base64, //this image is a single pixel (black) byte[] bytes = Convert.FromBase64String("R0lGODlhAQABAIAAAAAAAAAAACH5BAAAAAAALAAAAAABAAEAAAICTAEAOw=="); Image image; using (MemoryStream ms = new MemoryStream(bytes)) { image = Image.FromStream(ms); } return image; }Thanks
Tuhin PaulRank #7 · 71.9K reputationPosted Mar 15, 2023, 8:04 AM EDT
Hello Jaya,
To achieve this requirement in C#, you can try these steps:
1) Convert Base64 to encrypted value:
You can use a symmetric encryption algorithm such as AES to encrypt the Base64 string. Here is an example of how you can do this in C#:
string base64String = "your-base64-string"; byte[] base64Bytes = Convert.FromBase64String(base64String); // Set a secret key for encryption byte[] key = Encoding.UTF8.GetBytes("your-secret-key"); // Create an instance of the AES encryption algorithm Aes aes = Aes.Create(); aes.Key = key; // Encrypt the Base64 bytes using AES byte[] encryptedBytes = aes.CreateEncryptor().TransformFinalBlock(base64Bytes, 0, base64Bytes.Length); // Convert the encrypted bytes to a Base64 string string encryptedString = Convert.ToBase64String(encryptedBytes);2) Encrypt value to URL:
To encrypt the value to URL, you can use a URL-safe encryption algorithm such as Base64URL. Here is an example of how you can do this in C#:
string value = "your-value-to-encrypt"; byte[] valueBytes = Encoding.UTF8.GetBytes(value); // Encrypt the value using Base64URL string base64UrlString = Convert.ToBase64String(valueBytes) .Replace("+", "-") .Replace("/", "_") .Replace("=", ""); // The resulting string is a URL-safe encrypted value3) URL to Image visible:
To make the URL visible as an image, you need to generate an image file from the URL. You can do this by creating a new Bitmap object from the URL and then saving it to a file:
string url = "your-url"; Bitmap image = new Bitmap(url); image.Save("path-to-save-image");4) Read the URL and display the image:
To display the image, you can use a PictureBox control in a Windows Forms application or an Image control in a WPF application. Here is an example of how you can do this in a Windows Forms application:
string imageUrl = "your-image-url"; pictureBox1.Load(imageUrl);This will load the image from the URL and display it in the PictureBox control.
Tuhin PaulRank #7 · 71.9K reputationPosted Mar 16, 2023, 1:11 AM EDT
updated version of the code:
public Image LoadImage(string base64String) { try { // Decode the Base64 string into a byte array byte[] bytes = Convert.FromBase64String(base64String); // Load the image from the byte array using (MemoryStream ms = new MemoryStream(bytes)) { return Image.FromStream(ms); } } catch (FormatException ex) { // The input string is not a valid Base64-encoded image Console.WriteLine("Error: " + ex.Message); return null; } catch (Exception ex) { // Handle other exceptions that may occur Console.WriteLine("Error: " + ex.Message); return null; } }This code takes a Base64-encoded string as input and returns an Image object. It uses a try-catch block to handle exceptions and disposes of the MemoryStream object using the using statement.
Jignesh KumarRank #91 · 22.9K reputationPosted Mar 16, 2023, 1:47 AM EDT
Helle Jaya prakash,
To store encrypted Image,
protected void btnImageUpload_Click(object sender, EventArgs e) { //***Convert Image File to Base64 Encoded string***// //Read the uploaded file using BinaryReader and convert it to Byte Array. BinaryReader br = new BinaryReader(FileUpload1.PostedFile.InputStream); byte[] bytes = br.ReadBytes((int)FileUpload1.PostedFile.InputStream.Length); //Convert the Byte Array to Base64 Encoded string. string base64String = Convert.ToBase64String(bytes, 0, bytes.Length); //***Save Base64 Encoded string as Image File***// byte[] imageBytes = Convert.FromBase64String(base64String); //Save the Byte Array as Image File. string filePath = Server.MapPath("~/Files/" + Path.GetFileName(FileUpload1.PostedFile.FileName)); File.WriteAllBytes(filePath, imageBytes); }read this encrypted image from imagepath,
protected static string GetBase64StringForImage(string imgPath) { byte[] imageBytes = System.IO.File.ReadAllBytes(imgPath); string base64String = Convert.ToBase64String(imageBytes); return base64String; }