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 7004241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:10:24+00:00 2026-05-27T21:10:24+00:00

This is a method, for encrypting a text and returning the cipher text: public

  • 0

This is a method, for encrypting a text and returning the cipher text:

public static string Encrypt(this string clearText, CryptologyMethod method)
{
    ICryptoTransform cryptoTransform = null;
    switch (method)
    {
        case CryptologyMethod.TripleDes:
            cryptoTransform = new TripleDESCryptoServiceProvider().CreateEncryptor(Config.TripleDesKey, Config.TripleDesIV);
            break;
        case CryptologyMethod.AES:
            cryptoTransform = new AesCryptoServiceProvider().CreateEncryptor(Config.AesKey, Config.AesIV);
            break;
    }
    using (MemoryStream memoryStream = new MemoryStream())
    {
        using (CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoTransform, CryptoStreamMode.Write))
        {
            using (StreamWriter streamWriter = new StreamWriter(cryptoStream))
            {
                streamWriter.Write(clearText);
                cryptoStream.FlushFinalBlock();
                return memoryStream.Text();
            }
        }
    }
}

public enum CryptologyMethod
{
    TripleDes,
    AES
}

Text() is an extension method, to read the content of a stream:

public static string Text(this Stream stream)
{
    return new StreamReader(stream).ReadToEnd();
}

Also, Config.TripleDesKey and other properties return array of bytes read from configuration file.

The problem is that when I use this extension method, the result is always an empty string:

string cipherText = “some clear text”.Encrypt(CryptologyMethod.TripleDes);
// cipherText is empty

  • 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-27T21:10:25+00:00Added an answer on May 27, 2026 at 9:10 pm

    I think I’ve had that issue before, the solution was pretty obvious once I figured it out and completely opaque before then.

    The problem was that ReadToEnd is relative, as in “from current location to end”. The problem being that “current position” already WAS “end” because I’d just finished writing to the end of the stream. So I would fix your problem like this:

    public static string Text(this Stream stream)
    {
        stream.Position = 0
        return new StreamReader(stream).ReadToEnd();
    }
    

    Worked for me with a similar issue, hope it helps.

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

Sidebar

Related Questions

Can this method work? public String sayHello(){ return Hello.jsp?name= + laala; } I am
I have a method in my static Encryption class that looks like this: public
I am encrypting a text string using the following block of code. This code
For example this encryption function could be called a Utility ? public static string
I have this method on a webpart: private IFilterData _filterData = null; [ConnectionConsumer(Filter Data
Currently i have this method: static boolean checkDecimalPlaces(double d, int decimalPlaces){ if (d==0) return
I have this method in my db class public function query($queryString) { if (!$this->_connected)
I'm encrypting a text (just a string) using OpenSSL's CMS library. I've implemented a
This method works as expected - it creates a JTree with a root node
With this method declaration (no overloads): void Method(double d) { // do something with

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.