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

  • Home
  • SEARCH
  • 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 3951312
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:42:58+00:00 2026-05-20T01:42:58+00:00

hey there, so in a msSQL database that i’m saving and retrieving from using

  • 0

hey there, so in a msSQL database that i’m saving and retrieving from using stored procedures. i’m trying to encrypt some data in the c# windows form before being placed, and then of course decrypting it when i pull it back. all the encryption is being handled on the c# side. i’m using the sample code for encryption and decryption verbatim from microsoft’s tripleDESCryptoService Class (the memory version, 2nd example). The values get encrypted and sent to the database, but when retrieving it i get a "bad data" error.
a sample of the encryption call is…

TripleDESCryptoServiceProvider tDESalg = new TripleDESCryptoServiceProvider();
byte[] tempByte = new byte[100];
tempByte = encrypt(txt_Last_Name.Text, tDESalg.Key, tDESalg.IV);
txt_Last_Name.Text = System.Text.ASCIIEncoding.ASCII.GetString(tempByte);

the txt_Last_Name is then sent to the database, and i can see that there is something in the database. in the database, last name is of type varchar(20)

sample of the decryption call is…

TripleDESCryptoServiceProvider tDESalg = new TripleDESCryptoServiceProvider();
string lastName = dr.GetString(dr.GetOrdinal("Last Name"));
if (isEncrypted)
{
     byte[] toDecrypt = new ASCIIEncoding().GetBytes(lastName);
     lastName = decrypt(toDecrypt, tDESalg.Key, tDESalg.IV);                    
}
txt_Last_Name.Text = lastName;

it bombs in the decryption function at: "csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);" and i don’t understand why. i’m not sure if it’s not getting stored in the database correctly, or my conversions aren’t right.

if it means anything, the "Data" coming into the decrypt function is of size 16 and contains non-zero values, but the "byte[] fromEncrypt" is an array of size 16 containing all zeros.

thanks for any help!

  • 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-20T01:42:59+00:00Added an answer on May 20, 2026 at 1:42 am

    EDIT: Okay, we’ve got to the bottom of it… although the ASCII issue would have bitten too.

    Every time you create a new TripleDESCryptoServiceProvider and ask it for a key/IV, it will generate a new one. You need to store that securely somewhere, as it’s required to decrypt the data. Otherwise you don’t have any “secret” so it’s not really encryption…


    This is a terrible idea:

    txt_Last_Name.Text = System.Text.ASCIIEncoding.ASCII.GetString(tempByte);
    

    You’ve got arbitrary binary data in tempByte. Don’t assume it’s valid ASCII text. That’s almost certainly where you’re losing data.

    Use Convert.ToBase64String and Convert.FromBase64String to safely encode opaque binary data as text.

    Additionally, this is a bad idea:

    byte[] tempByte = new byte[100];
    tempByte = encrypt(txt_Last_Name.Text, tDESalg.Key, tDESalg.IV);
    

    What’s the point of creating a byte array if you’re then going to ignore it? Use

    byte[] tempByte = encrypt(txt_Last_Name.Text, tDESalg.Key, tDESalg.IV);
    

    instead. Oh, and try to follow .NET naming conventions 🙂

    Having said all of this, if your encryption is always returning a byte array of 16 zeroes, that’s a pretty sure sign that your encrypt method is broken. We can’t really help there until you post the code for that method.

    Finally, if your column is of type varchar(20) you should be aware that that may very well not hold all the data you need it to… particularly if you’re going to include a salt. Base64 will increase the size of the data somewhat, and encryption may do so too. As mentioned in another answer, storing this as binary in the database would be more sensible in many ways.

    (Note: even if you did want to use that code, I’d write it as txt_Last_Name.Text = Encoding.ASCII.GetString(tempByte);. Using directives are your friend, and ASCII is a property of Encoding, not ASCIIEncoding.)

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

Sidebar

Related Questions

Hey there, I'm doing some queries to a MySQL database that involves some user-input..
Hey there, I've got a block of HTML that I'm going to be using
hey there i want to change some css attributes from javascript but i cant
Hey, is there any built in functions or something like that in php that
Hey there, I'm at the moment trying to make a product management application. The
Hey there, I'm trying to make a site which have following: News, Products, About
Hey there, I keep hearing over and over again that I should ALWAYS use
Hey there. I'm using Git for windows (The msysgit project). And working with the
hey there i am having problem i have List<List<memoryCard>> that i want to show
Hey I was wondering if there were any way to upload images in ASP?

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.