Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6164339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:58:35+00:00 2026-05-23T21:58:35+00:00

EDIT2: Seems Convert.FromBase64String is my savior. Does anyone know if a comma would every

  • 0

EDIT2:
Seems Convert.FromBase64String is my savior. Does anyone know if a comma would every be in such a string? I’m doing basic csv parsing. If it is I can always use quotes (but can quotes ever be in a string like this too I guess) but this is just a small project for myself so just easier to parse by splitting by ‘,’.

EDIT:
OK so it would seem my issue is when I convert the encrypted bytes to a string, then convert that string back to bytes to be decrypted. How could I do this because I want to actually store the encrypted string somewhere and be able to decrypt that string.

I’m trying to save the public key (and private just for testing) to an xml file so that I can read the same one in later, but getting “Bad data” error when trying to decrypt. Can anyone see what I’m doing wrong?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace EncryptionTest
{
class Program
{
    static void Main(string[] args)
    {
        UnicodeEncoding ByteConverter = new UnicodeEncoding();

        byte[] dataToEncrypt = ByteConverter.GetBytes("Test data");

        WriteRSAInfoToFile();

        string enc = Encrypt(dataToEncrypt);

        enc = Decrypt(enc);
    }

    static void WriteRSAInfoToFile()
    {
        RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
        TextWriter writer = new StreamWriter("C:\\publicKey.xml");
        string publicKey = RSA.ToXmlString(false);
        writer.Write(publicKey);
        writer.Close();

        writer = new StreamWriter("C:\\privateKey.xml");
        string privateKey = RSA.ToXmlString(true);
        writer.Write(privateKey);
        writer.Close();
    }

    static string Encrypt(byte[] data)
    {
        UnicodeEncoding ByteConverter = new UnicodeEncoding();
        RSACryptoServiceProvider encrypt = new RSACryptoServiceProvider();
        TextReader reader = new StreamReader("C:\\publicKey.xml");
        string publicKey = reader.ReadToEnd();
        reader.Close();

        encrypt.FromXmlString(publicKey);

        byte[] encryptedData = encrypt.Encrypt(data, false);

        return ByteConverter.GetString(encryptedData);
    }

    static string Decrypt(string data)
    {
        UnicodeEncoding ByteConverter = new UnicodeEncoding();
        RSACryptoServiceProvider decrypt = new RSACryptoServiceProvider();
        TextReader reader = new StreamReader("C:\\privateKey.xml");
        string privateKey = reader.ReadToEnd();
        reader.Close();

        decrypt.FromXmlString(privateKey);

        byte[] d = ByteConverter.GetBytes(data);
        byte[] decryptedData = decrypt.Decrypt(d, false);       // ERROR: bad data

        return ByteConverter.GetString(decryptedData);
    }
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T21:58:37+00:00Added an answer on May 23, 2026 at 9:58 pm

    This has nothing to do with your public/private key handling (I think that part looks OK, the file I/O can be done easier though).

    But your way of transporting the encrypted data as a string is not suitable for “round-trip” . Use a Base64 encoding instead.

    You should be able to see this in the debugger: make a note of the length and the first bytes of encryptedData and this should match d in the Decrypt method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

edit #2: Question solved halfways. Look below As a follow-up question, does anyone know
I'd like to convert this string into a datetime object: Wed Oct 20 16:35:44
Python 2.6 Using Python string.replace() seems not working for UTF-16-LE file. I think of
I need to convert a String like 2010-11-28 into Date type to be saved
Hello lovely computer people: I would like to convert the following CFStringRef into a
EDIT: It seems to be something with having the two queues in the same
Is the documentation for Rich Edit Controls really as bad (wrong?) as it seems
Could someone convert this line of code to be readable by HTML? echo '<h3>'.
Could somebody tell me what is the proper way to convert a NSString* to
In the codebehind you would add the TVP as a SqlDbType.Structured for a stored

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.