Another C# Queston And Enums


Recommended Posts

ok I have my enum to hold Char for lookups but I also want to add other char like ~`@#$%^& and numbers. is there a way to do this..

here is one of my enums

private enum WheelOne : int

{

a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z,

A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z,

}

I would like something like this, but works..

private enum WheelOne : int

{

a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, 1, 2, 3, 4, 5, 6, 7, 8, 9

A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, !, @, , #, $, %, ^, &, *, (, ), _, +, =, -, [, ], }, {, ', ;, ", :, /, ., ,, ?, >, <

}

I have tried wraping them in " " and ' '

Link to post
Share on other sites
Not possible.

should I throw these in an array and use the array position and search?

at least not possible with enums means I was not just stupid, I was just wrong. :)

Link to post
Share on other sites

ok I have my encrypton class done for c#

here it is

to use it you pass a positive number as the offset to encrypt

to decrypt you pass a negative version of the offset

so say the offset is 25 for encryption

it would be -25 to decrypt

here you go.. pick at it and tell me what I can do better

thanks

// Enigma.CS a dll to allow simple encryption in the vain of the emigma machine form WWII.
// Copyright (C) 2006 William S. Huskey
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
using System;
using System.Collections.Generic;
using System.Text;

namespace Enigma
{
public class Cenigma
{

//Set Encryption arrays that take place of the enigma machines wheels

/// <summary>
/// private enum WheelOne is used as a ?? char lookup table the switch between alpha upper and lower case charaters
///
/// </summary>
private string[] wheelOne =
{
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u",",",".","/","<",">","?",
"v", "w", "x", "y", "z", "1","2","3","4","5","6","7","8","9","0","'","-","=","!","@","#","$","+","%","^","&",
"*","(",")", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R","=", "S",
"T", "U", "V", "W", "X", "Y", "Z","\"","`","~"," ","|"
};

public string[] WheelOne {
get {
return wheelOne;
}
}

/// <summary>
/// private enum WheelTwo is used as a 52 char lookup table the switch between alpha upper and lower case charaters
///
/// </summary>
private string [] WheelTwo =
{
"T", "U", "V", "W", "X", "Y", "Z","\""," ","|",
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u","=",
"*","(",")", "A", "B", "C", "D", "E", "F", "G", "H","`", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S",",",".","/","<",">","?",
"v", "w", "x", "y", "z", "1","2","3","4","5","6","7","8","9","~","0","'","-","=","!","@","#","$","%","^","&","+"

};

/// <summary>
///
/// </summary>
private string [] WheelThree =
{

"v", "w", "x", "y", "z"," ", "1","2","3","4","5","6","7","8","9","0","'","-","=","!","@","#","$","%","^","&",
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u","=",
"T", "U", "V", "W", "X", "Y", "Z","\"",",",".","/","<",">","?","+","~","`","|",
"*","(",")", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S"
};

private String redSideOut = "";

public string RedSideOut {
get {
return redSideOut;
}
set {
redSideOut = value;
}
}
private String blackSideOut = "";

public string BlackSideOut {
get {
return blackSideOut;
}
set {
blackSideOut = value;
}
}
private int wheelStartPosition;

public int WheelStartPosition {
get {
return wheelStartPosition;
}
set {
wheelStartPosition = value;
}
}
private int wheelNext;

public int WheelNext {
get {
return wheelNext;
}
set {
wheelNext = value;
}
}
/// <summary>
///
/// </summary>
/// <param name="WheelPosition"></param>
/// <param name="WheelsUsed"></param>

public Cenigma(int WheelPosition, int wHeelsNext)
{
WheelStartPosition = WheelPosition;
WheelNext = wHeelsNext;

BlackSideOut = "";

}





/// <summary>
///
/// </summary>
/// <param name="RedSideIn"></param>
/// <param name="wheel"></param>
/// <returns></returns>

/// <summary>
///
/// </summary>
/// <param name="WheelPosition"></param>
/// <param name="CharIn"></param>
/// <returns></returns>
private string Wheel3(int WheelPosition, char CharIn)
{
string find =Convert.ToString(CharIn);
int ConvertInt = 0;
int CharInString = 0;
for (int x =0; x < WheelThree.Length; x++)
{
if (find == WheelThree[x])
{
CharInString = x;
}
}


if (WheelPosition + CharInString > WheelThree.Length -1)
ConvertInt = (WheelPosition + CharInString) - (WheelThree.Length -1);
else if (WheelPosition + CharInString < 0)
ConvertInt = (WheelPosition + CharInString) + (WheelThree.Length -1 );
else
ConvertInt = WheelPosition + CharInString;

// WheelOne letter = (WheelOne)ConvertInt;
return WheelThree[ConvertInt];
}
/// <summary>
///
/// </summary>
/// <param name="WheelPosition"></param>
/// <param name="CharIn"></param>
/// <returns></returns>
private string Wheel2(int WheelPosition, char CharIn)
{
string find =Convert.ToString(CharIn);
int ConvertInt = 0;
int CharInString = 0;
for (int x =0; x < WheelTwo.Length; x++)
{
if (find == WheelTwo[x])
{
CharInString = x;
}
}

//ensure the pattern loops
if (WheelPosition + CharInString > WheelTwo.Length -1)
ConvertInt = (WheelPosition + CharInString) - (WheelTwo.Length);
else if (WheelPosition + CharInString < 0)
ConvertInt = (WheelPosition + CharInString) + (WheelTwo.Length);
else
ConvertInt = WheelPosition + CharInString;

// WheelOne letter = (WheelOne)ConvertInt;
return WheelTwo[ConvertInt];
}
/// <summary>
///
/// </summary>
/// <param name="WheelPosition"></param>
/// <param name="CharIn"></param>
/// <returns></returns>
private string Wheel1(int WheelPosition, char CharIn)
{
string find =Convert.ToString(CharIn);
int ConvertInt = 0;
int CharInString = 0;
for (int x =0; x < WheelOne.Length; x++)
{
if (find == WheelOne[x])
{
CharInString = x;
}
}

//ensure the pattern loops
if (WheelPosition + CharInString > WheelOne.Length - 1)
ConvertInt = (WheelPosition + CharInString) - (WheelOne.Length );
else if (WheelPosition + CharInString < 0)
ConvertInt = (WheelPosition + CharInString) + (WheelOne.Length );
else
ConvertInt = WheelPosition + CharInString;

// WheelOne letter = (WheelOne)ConvertInt;
return wheelOne[ConvertInt];
}

public string encrypt(char RedSideIn, int WheelNumber)
{
if (WheelNumber == 1)
BlackSideOut = Wheel1(WheelStartPosition, RedSideIn);
else if (WheelNumber == 2)
BlackSideOut = Wheel2(WheelStartPosition, RedSideIn);
else if (WheelNumber == 3)
BlackSideOut = Wheel3(WheelStartPosition, RedSideIn);

return BlackSideOut;
}
/// <summary>
///
/// </summary>
/// <param name="BlackSideIn"></param>
/// <param name="WheelNumber"></param>
/// <returns></returns>
public string deCrypt(char BlackSideIn, int WheelNumber)
{
if (WheelNumber == 1)
RedSideOut = Wheel1(WheelStartPosition, BlackSideIn);
else if (WheelNumber == 2)
RedSideOut = Wheel2(WheelStartPosition, BlackSideIn);
else if (WheelNumber == 3)
RedSideOut = Wheel3(WheelStartPosition, BlackSideIn);

return RedSideOut;
}

}

}

Link to post
Share on other sites

in case anyone cares, I found some problems with the arrays

here is the fixed version

// Enigma.CS a dll to allow simple encryption in the vain of the emigma machine form WWII.
// Copyright (C) 2006 William S. Huskey
// inspired by Myke Predko’s Enigma page http://www.myke.com/enigma1.htm
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
using System;
using System.Collections.Generic;
using System.Text;

namespace Enigma
{
public class Cenigma
{

//Set Encryption arrays that take place of the enigma machines wheels

/// <summary>
/// private enum WheelOne is used as a ?? char lookup table the switch between alpha upper and lower case charaters
///
/// </summary>
private string[] WheelOne =
{
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q",
"r", "s", "t", "u", ",", ".", "/", "<", ">", "?", "v", "w", "x", "y", "z", "1", "2",
"3", "4", "5", "6", "7", "8", "9", "0", "'", "-", "=", "!", "@", "#", "$", "+", "%",
"^", "&", "*", "(", ")", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L",
"M", "N", "O", "P", "Q", "R", "\\", "S", "T", "U", "V", "W", "X", "Y", "Z","\"", "`",
"~", " ", "|"
};




/// <summary>
/// private enum WheelTwo is used as a 52 char lookup table the switch between alpha upper and lower case charaters
///
/// </summary>
private string [] WheelTwo =
{
"T", "U", "V", "W", "X", "Y", "Z", "\""," ", "|", "a", "b", "c", "d", "e", "f", "g",
"h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "\\", "*","(",
")", "A", "B", "C", "D", "E", "F", "G", "H", "`", "I", "J", "K", "L", "M", "N", "O",
"P", "Q", "R", "S", ",", ".", "/", "<", ">", "?", "v", "w", "x", "y", "z", "1", "2",
"3", "4", "5", "6", "7", "8", "9", "~", "0", "'", "-", "=", "!", "@", "#", "$", "%",
"^", "&", "+"

};

/// <summary>
///
/// </summary>
private string [] WheelThree =
{
"3", "4", "5", "6", "7", "8", "9", "~", "0", "'", "-", "=", "!", "@", "#", "$", "%",
"h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "//", "*","(",
"T", "U", "V", "W", "X", "Y", "Z", "\""," ", "|", "a", "b", "c", "d", "e", "f", "g",
"P", "Q", "R", "S", ",", ".", "/", "<", ">", "?", "v", "w", "x", "y", "z", "1", "2",
")", "A", "B", "C", "D", "E", "F", "G", "H", "`", "I", "J", "K", "L", "M", "N", "O",
"^", "&", "+"

};

private String redSideOut = "";

public string RedSideOut {
get {
return redSideOut;
}
set {
redSideOut = value;
}
}
private String blackSideOut = "";

public string BlackSideOut {
get {
return blackSideOut;
}
set {
blackSideOut = value;
}
}
private int wheelStartPosition;

public int WheelStartPosition {
get {
return wheelStartPosition;
}
set {
wheelStartPosition = value;
}
}
private int wheelNext;

public int WheelNext {
get {
return wheelNext;
}
set {
wheelNext = value;
}
}
/// <summary>
///
/// </summary>
/// <param name="WheelPosition"></param>
/// <param name="WheelsUsed"></param>

public Cenigma(int WheelPosition, int wHeelsNext)
{
WheelStartPosition = WheelPosition;
WheelNext = wHeelsNext;

BlackSideOut = "";

}





/// <summary>
///
/// </summary>
/// <param name="RedSideIn"></param>
/// <param name="wheel"></param>
/// <returns></returns>

/// <summary>
///
/// </summary>
/// <param name="WheelPosition"></param>
/// <param name="CharIn"></param>
/// <returns></returns>
private string Wheel3(int WheelPosition, char CharIn)
{
string find =Convert.ToString(CharIn);
int ConvertInt = 0;
int CharInString = 0;
for (int x =0; x < WheelThree.Length; x++)
{
if (find == WheelThree[x])
{
CharInString = x;
}
}


if (WheelPosition + CharInString > WheelThree.Length -1)
ConvertInt = (WheelPosition + CharInString) - (WheelThree.Length -1);
else if (WheelPosition + CharInString < 0)
ConvertInt = (WheelPosition + CharInString) + (WheelThree.Length -1 );
else
ConvertInt = WheelPosition + CharInString;

// WheelOne letter = (WheelOne)ConvertInt;
return WheelThree[ConvertInt];
}
/// <summary>
///
/// </summary>
/// <param name="WheelPosition"></param>
/// <param name="CharIn"></param>
/// <returns></returns>
private string Wheel2(int WheelPosition, char CharIn)
{
string find =Convert.ToString(CharIn);
int ConvertInt = 0;
int CharInString = 0;
for (int x =0; x < WheelTwo.Length; x++)
{
if (find == WheelTwo[x])
{
CharInString = x;
}
}

//ensure the pattern loops
if (WheelPosition + CharInString > WheelTwo.Length -1)
ConvertInt = (WheelPosition + CharInString) - (WheelTwo.Length);
else if (WheelPosition + CharInString < 0)
ConvertInt = (WheelPosition + CharInString) + (WheelTwo.Length);
else
ConvertInt = WheelPosition + CharInString;

// WheelOne letter = (WheelOne)ConvertInt;
return WheelTwo[ConvertInt];
}
/// <summary>
///
/// </summary>
/// <param name="WheelPosition"></param>
/// <param name="CharIn"></param>
/// <returns></returns>
private string Wheel1(int WheelPosition, char CharIn)
{
string find =Convert.ToString(CharIn);
int ConvertInt = 0;
int CharInString = 0;
for (int x =0; x < WheelOne.Length; x++)
{
if (find == WheelOne[x])
{
CharInString = x;
}
}

//ensure the pattern loops
if (WheelPosition + CharInString > WheelOne.Length - 1)
ConvertInt = (WheelPosition + CharInString) - (WheelOne.Length );
else if (WheelPosition + CharInString < 0)
ConvertInt = (WheelPosition + CharInString) + (WheelOne.Length );
else
ConvertInt = WheelPosition + CharInString;

// WheelOne letter = (WheelOne)ConvertInt;
return WheelOne[ConvertInt];
}

public string encrypt(char RedSideIn, int WheelNumber)
{
if (WheelNumber == 1)
BlackSideOut = Wheel1(WheelStartPosition, RedSideIn);
else if (WheelNumber == 2)
BlackSideOut = Wheel2(WheelStartPosition, RedSideIn);
else if (WheelNumber == 3)
BlackSideOut = Wheel3(WheelStartPosition, RedSideIn);

return BlackSideOut;
}
/// <summary>
///
/// </summary>
/// <param name="BlackSideIn"></param>
/// <param name="WheelNumber"></param>
/// <returns></returns>
public string deCrypt(char BlackSideIn, int WheelNumber)
{
if (WheelNumber == 1)
RedSideOut = Wheel1(WheelStartPosition, BlackSideIn);
else if (WheelNumber == 2)
RedSideOut = Wheel2(WheelStartPosition, BlackSideIn);
else if (WheelNumber == 3)
RedSideOut = Wheel3(WheelStartPosition, BlackSideIn);

return RedSideOut;
}

}

}

Edited by iccaros
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...