Autor Beitrag
zocker_max
Hält's aus hier
Beiträge: 7



BeitragVerfasst: So 29.11.09 15:20 
Hallo ich habe ein Problem mit dem zugriff auf "public string encrypt (string text, string schlüssel)"
vom Hauptprogramm aus. Ich möchte den Klartext und den Schlüssel eingeben und die Chiffre erhalten.
Bitte helft mir!

ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace Caesar_Verschlüsselung
{
    public class Encrypt 
    {
        
        public string encrypt (string text, string schlüssel)
        {
                string encrypted = string.Empty;
                int keyindex = 0;
                foreach (char c in text)
                {
                    int asciinr = (int)c;
                    asciinr += (int)schlüssel[keyindex];
                    if (asciinr > 255)
                        asciinr -= 255;                    
                    encrypted += (char)asciinr;
                    keyindex++;
                    if (keyindex > schlüssel.Length - 1)
                        keyindex = 0;
                }
                return encrypted;            
        }
    }
    class Program
    {
        static void Main()
        {
            string schlüssel;
            string klartext;
            string chiffre;
            Console.WriteLine("Caesar-Verschlüsselung");
            Console.WriteLine("Klartext bitte jetzt eingeben:");
            klartext = Console.ReadLine();
            Console.WriteLine("Key bitte jetzt eingeben:");
            schlüssel = Console.ReadLine();
            Console.WriteLine("Klartext wird verschlüsselt.");
            Encrypt entschlüsseln = new Encrypt(klartext, schlüssel); //Hier kommt der Fehler (Constructor)
            Console.WriteLine("Die Chiffre lautet:");
            Console.WriteLine(chiffre);
            Console.ReadKey(); 

        }  
    }
}


Moderiert von user profile iconChristian S.: C#-Tags hinzugefügt
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: So 29.11.09 19:21 
:welcome:

Da hast du etwas falsch verstanden. Ein Konstruktor besitzt den gleichen Namen wie seine Klasse, es müsste also Encrypt heißen. Ich denke aber nicht, dass du die Methode überhaupt als Konstruktor willst, denn diese können logischerweise keinen Rückgabewert haben. Also nimmst du stattdessen den parameterlosen Standardkonstruktor und rufst "encrypt" auf dem zurückgegebenen Objekt auf:
ausblenden C#-Quelltext
1:
2:
Encrypt entschlüsseln = new Encrypt();
string chiffre = entschlüsseln.encrypt(klartext, schlüssel);

(Methoden bitte trotzdem groß schreiben, hier also am besten umbenennen).

Ansonsten könntest du "encrypt" auch als static kennzeichnen, dann sähe der Aufruf so aus:
ausblenden C#-Quelltext
1:
string chiffre = Encrypt.encrypt(klartext, schlüssel);					

_________________
>λ=
zocker_max Threadstarter
Hält's aus hier
Beiträge: 7



BeitragVerfasst: So 29.11.09 20:31 
Danke für deine Hilfe. ich nehme die Lösung:

Encrypt entschlüsseln = new Encrypt();
chiffre = entschlüsseln.encrypt(klartext, schlüssel);
zocker_max Threadstarter
Hält's aus hier
Beiträge: 7



BeitragVerfasst: So 29.11.09 20:58 
So sieht der fertige Quelltext für Caesar-Chiffre aus. Nochmal thx!!!

ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace Caesar_Verschlüsselung
{
    public class Encrypt 
    {

        public string encrypt(string klartext, string schlüssel)
        {
                string encrypted = string.Empty;
                int keyindex = 0;
                foreach (char c in klartext)
                {
                    int asciinr = (int)c;
                    asciinr += (int)schlüssel[keyindex];
                    if (asciinr > 255)
                        asciinr -= 255;                    
                    encrypted += (char)asciinr;
                    keyindex++;
                    if (keyindex > schlüssel.Length - 1)
                        keyindex = 0;
                }
                return encrypted;            
        }
    }




    public class Decrypt
    {

        public string decrypt(string chiffre, string schlüssel)
        {
           
                string decrypted = string.Empty;                
                int keyindex = 0;
                foreach (char c in chiffre)               
                {
                    int asciinr = (int)c;
                    asciinr -= (int)schlüssel[keyindex];
                    if (asciinr < 0)
                        asciinr = 255 + asciinr;
                    decrypted += (char)asciinr;
                    keyindex++;
                    if (keyindex > schlüssel.Length - 1)
                        keyindex = 0;
                }
                return decrypted;
            }
        }
    


    class Program
    {
        static void Main()
        {
            string schlüssel;
            string klartext;
            string chiffre;
            string s;
            Start:
            Console.WriteLine("Caesar-Verschlüsselung");
            Console.WriteLine("verschlüsseln (v)");
            Console.WriteLine("entschlüsseln (e)");
            s = Console.ReadLine();                      
            switch (s)
            {
                case "v":
                    {
                        Console.WriteLine("Klartext bitte jetzt eingeben:");
                        klartext = Console.ReadLine();
                        Console.WriteLine("Key bitte jetzt eingeben:");
                        schlüssel = Console.ReadLine();
                        Console.WriteLine("Klartext wird verschlüsselt.");
                        Encrypt entschlüsseln = new Encrypt();
                        chiffre = entschlüsseln.encrypt(klartext, schlüssel);
                        Console.WriteLine("Die Chiffre lautet:");
                        Console.WriteLine(chiffre);
                        Console.ReadKey();
                        break;
                    }
                case "e":
                    {
                        Console.WriteLine("Chiffre bitte jetzt eingeben:");
                        chiffre = Console.ReadLine();
                        Console.WriteLine("Key bitte jetzt eingeben:");
                        schlüssel = Console.ReadLine();
                        Console.WriteLine("Klartext wird entschlüsselt.");
                        Decrypt verschlüsseln = new Decrypt();
                        klartext = verschlüsseln.decrypt(chiffre, schlüssel);
                        Console.WriteLine("Der Klartext lautete:");
                        Console.WriteLine(klartext);
                        Console.ReadKey();
                        break;
                    }
                default:
                    goto Start;                    
            }
    
        }  
    }


Moderiert von user profile iconChristian S.: C#-Tags hinzugefügt
JasonDelife
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 81

Windows 7 Professional
C# (Visual Studio 2008 Professional), Java (NetBeans IDE 6.7)
BeitragVerfasst: So 29.11.09 21:17 
Sollte man hier nicht eine Klasse CaesarCryptor machen und dort Encrypt() und Decrypt unterbringen?

Grüße, JasonDelife.
zocker_max Threadstarter
Hält's aus hier
Beiträge: 7



BeitragVerfasst: Mo 30.11.09 10:29 
Ja das ist besser aber nicht wirklich nötig. Aber trotzdem thx.

[SOLVED]