14 Replies - 464 Views - Last Post: 24 September 2013 - 02:13 PM Rate Topic: -----

#1 bkruep  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 28
  • Joined: 26-July 13

Encryption

Posted 24 September 2013 - 08:32 AM

I am new to C# and trying to figure out this encryption\decryption app but I am not doing this right. Can someone that is familiar with VB take a look at this? The reason is I know how to code this in VB but not sure about C#. I can provide the errors if you would like...


//Create an app that reads a four-digit integer entered by the user and encrypt it as follows
//Replace each digit with the result of adding 7 ot the digit and getting the remainder after dividing the new value by 10.
//Then swap the first digit with the third, and swap the second digit with the fourth
//Then display the encrpyted integer


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EnforcingPrivacy
{
    class Program
    {
        static void Main(string[] args)
        {
            int intDigit, strDigit, intDigits(3);
            string strInput;
            Console.Write("Enter a four digit integer to encrypt and decrypt: ");
            int num = Convert.ToInt32(Console.ReadLine());

            for (int i = 1; i <= 4; i++)
            {
                strDigit = Mid(strInput, i, 1);
                intDigit = Convert.ToInt32(strDigit + 7);
                intDigits(i - 1) = intDigit % 10;
            }
            string Encrypt = Convert.ToString(intDigits(2)) & Convert.ToString(intDigits(3)) & Convert.ToString(intDigits(0)) & Convert.ToString(intDigits(1));
        }
    }
}




Is This A Good Question/Topic? 0
  • +

Replies To: Encryption

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9425
  • View blog
  • Posts: 35,422
  • Joined: 12-June 08

Re: Encryption

Posted 24 September 2013 - 08:39 AM

Would you explain what is going wrong versus what it should be doing?
Was This Post Helpful? 0
  • +
  • -

#3 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4537
  • View blog
  • Posts: 7,916
  • Joined: 08-June 10

Re: Encryption

Posted 24 September 2013 - 09:13 AM

Quote

I can provide the errors if you would like


We always want the errors, and any other relevant information you have that helps us narrow down the problems you're experiencing. I suggest you read the links in my signature, they'll explain what info we need and why.

For the record, your problem is likely because & is not the string concatenation operator, like it is in VB.NET. + is used to join strings. Look at line 29.

In C#, & has a few different uses, but is mainly the "bitwise and" operator. It can also be used for non-short-circuit boolean comparisons.
Was This Post Helpful? 0
  • +
  • -

#4 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4537
  • View blog
  • Posts: 7,916
  • Joined: 08-June 10

Re: Encryption

Posted 24 September 2013 - 09:19 AM

Side note: I hope you're not planning on using this encryption for anything important (or anything other than a learning exercise, really). Security is the #1 thing that should be left to experts. If you do need to use encryption, use one of the built-in libraries. They're not hard to use, and they're significantly more secure than any home-grown algorithm.

If you're interested, I made a simple wrapper for simple AES encryption (as well as hashing):

https://github.com/c...nd/CryptoHelper

But feel free to use any modern crypto tutorial (how to tell: if it's telling you to use "DES" or "Triple-DES/3DES", it's out of date). Just don't use home-grown crypto code for anything remotely important.
Was This Post Helpful? 0
  • +
  • -

#5 bkruep  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 28
  • Joined: 26-July 13

Re: Encryption

Posted 24 September 2013 - 09:24 AM

One of my errors is "Mid does not exist in the current context". I know where this issue is in line 25 but not sure how to fix it. I created a method Mid and the error went away but feel something is not right with it still...


        private static int Mid(string strInput, int i, int p)
        {
            throw new NotImplementedException();
        }




Every place that intDigits shows up I am getting an error stating "intDigits is a variable but is used like a method"

Finally, when I declare intDigits as a variable I get an error stating Expected; or = (cannot specify constructor arguments in declaration)

Prefer anyone that is strong in VB to get back with me as they probably would have the best idea in what I am trying to do
Was This Post Helpful? 0
  • +
  • -

#6 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Encryption

Posted 24 September 2013 - 10:30 AM

You need to learn C# syntax first. Your method of declaring an array in line 18 shows you don't know how it's done. Your index variable in line 23 and the indexing in line 29 show you don't understand array sizes. Your attempt to use 'Mid' and then not even trying a search (Dealing with a string, maybe searching for 'String C# MSDN' and reading the documentation would help).

You really should start with the beginner tutorials you can find over there on the right side of your screen.

Just FYI, every error you are getting is because you don't understand C# syntax.

This post has been edited by Momerath: 24 September 2013 - 10:35 AM

Was This Post Helpful? 2
  • +
  • -

#7 Robin19  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 272
  • View blog
  • Posts: 552
  • Joined: 07-July 10

Re: Encryption

Posted 24 September 2013 - 10:34 AM

C# Arrays
Was This Post Helpful? 1
  • +
  • -

#8 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3631
  • View blog
  • Posts: 11,325
  • Joined: 05-May 12

Re: Encryption

Posted 24 September 2013 - 11:08 AM

This topic seems to refer to this old VB.NET thread: http://www.dreaminco...his-encryption/
Was This Post Helpful? 0
  • +
  • -

#9 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4537
  • View blog
  • Posts: 7,916
  • Joined: 08-June 10

Re: Encryption

Posted 24 September 2013 - 11:09 AM

To Momerath's point, here's a really good comparison chart that shows VB/C# syntax differences:

http://www.harding.e...comparison.html

Please give it a once over for all the topics Momerath mentioned, because it explains pretty much all of them.

The only thing it won't show you is Mid, which doesn't exist in C#. You can either use String.Substring or since you only want one digit, you can just treat the string as if it were an array of characters.

Also remember that in C#, collections are zero-based. So you'll have to change the way your loop works, counting from zero (and remembering that the collection's Length is one greater than its last index).

Edit: for the record, Momerath may have been blunt, but he's not wrong in any way. If you come to experts for help, you shouldn't be upset when you get it. He's given a very detailed explanation of what's wrong, and the steps you need to resolve the problem. The issue is that your problem isn't what you think it is. You think your issue is with your code, but the real problem is with language basics. I do believe that you already know how to program, but you have to start small with languages you are unfamiliar with. Read some of our Learning C# series tutorials and your problems will be greatly reduced.

For instance, I've been interested in Node.js, but I didn't jump in and start writing a new business website, I did tutorials and examples. I'm still doing them, even though I'm an "expert" in C#. :P
Was This Post Helpful? 2
  • +
  • -

#10 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3642
  • View blog
  • Posts: 12,620
  • Joined: 12-December 12

Re: Encryption

Posted 24 September 2013 - 11:49 AM

To clarify, most VB.NET Collections are also zero-indexed:

MSDN said:

.NET Framework collections are zero-based for the purpose of standardization. The Visual Basic Collection class is one-based for the purpose of compatibility with previous versions.


An important point to note, though, is that when dimensioning an Array in VB.NET the argument(s) specify the upper-bound(s) for the array: the index of the last element. The first element is the zeroth element (0), and the length of the array is the upper-bound + 1.

Curious to note that the MSDN documentation is wrong:

Quote

You can define the size of an array several ways. You can supply the size when the array is declared, as the following example shows.

Dim cargoWeights(10) As Double

Or perhaps I'm just tired? I've stared at this for a while :online2long: as it is very rare for the documentation to be wrong (and on such a fundamental level).

This post has been edited by andrewsw: 24 September 2013 - 11:52 AM

Was This Post Helpful? 0
  • +
  • -

#11 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4537
  • View blog
  • Posts: 7,916
  • Joined: 08-June 10

Re: Encryption

Posted 24 September 2013 - 11:59 AM

Good point, I made an assumption based on his loop. I've never worked professionally with VB.NET.
Was This Post Helpful? 0
  • +
  • -

#12 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2268
  • View blog
  • Posts: 9,482
  • Joined: 29-May 08

Re: Encryption

Posted 24 September 2013 - 12:19 PM

In the VB.net version remove the imported namespace Microsoft.VisualBasic.

This will reveal all the vb6 style of coding, include the strange beast that is Mid$(s$,1,4)="ABCD" assignment operator. Yep that right assigning to a method!

If i remember correctly MID indexing begins at 1 not 0.

Learn the .net framework based approach it make your code a lot easier and simpler.

This post has been edited by AdamSpeight2008: 24 September 2013 - 12:27 PM

Was This Post Helpful? 0
  • +
  • -

#13 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3642
  • View blog
  • Posts: 12,620
  • Joined: 12-December 12

Re: Encryption

Posted 24 September 2013 - 12:22 PM

View PostCurtis Rutland, on 24 September 2013 - 06:59 PM, said:

Good point, I made an assumption based on his loop. I've never worked professionally with VB.NET.

No worries ;) but.., is the world losing its focus? The MSDN Documentation is wrong?! The world doesn't make sense any more. Tomorrow, I will go to work on a banana and eat my bicycle.
Was This Post Helpful? 0
  • +
  • -

#14 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2268
  • View blog
  • Posts: 9,482
  • Joined: 29-May 08

Re: Encryption

Posted 24 September 2013 - 12:30 PM

Curtis Rutland only programs in VB when he wants to feel dirty.
Was This Post Helpful? 1
  • +
  • -

#15 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4537
  • View blog
  • Posts: 7,916
  • Joined: 08-June 10

Re: Encryption

Posted 24 September 2013 - 02:13 PM

Yep, just seeing the code makes me want a hot shower.

Not really, I actually have a lot of respect for VB.NET as a language, and it has some features I'd like to see make their way into C# (like XML Literals and the With statement). I just prefer C-style grammar over VB's.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1