How do I make it so that, if i have this class:
namespace RSA_Functions
{
internal class Crypto
{
internal Encrypt encrypt;
internal Decrypt decrypt;
private RSAParameters privateKey;
private RSAParameters publicKey;
private RSACryptoServiceProvider csp;
//Generates public and private keys
internal void GenerateKeys()
{
csp = new RSACryptoServiceProvider(2048);
//private key
privateKey = csp.ExportParameters(true);
//public key
publicKey = csp.ExportParameters(false);
csp = new RSACryptoServiceProvider();
}
internal class Encrypt
{
internal static string String(string plainTextData, bool returnString)
{
//Bla bla bla
}
internal static Byte[] String(string plainTextData)
{
//Bla bla bla
}
internal static string Byte(byte[] bytesToEncrypt, bool returnString)
{
//Bla bla bla
}
internal static Byte[] Byte(byte[] bytesToEncrypt)
{
//Bla bla bla
}
}
internal class Decrypt
{
internal static string Byte(byte[] bytesEncrypted, bool returnString)
{
//Bla bla bla
}
internal static byte[] Byte(byte[] bytesEncrypted)
{
//Bla bla bla
}
internal static string String(string stringEncrypted, bool returnString)
{
//Bla bla bla
}
internal static byte[] String(string stringEncrypted)
{
//Bla bla bla
}
}
}
}
How do I make it so that I can simply do Crypto crypto = new()
and be able to access the Ecrypt and Decrypt classes methods with a simple crypto.GenerateKeys(); crypto.Encrypt.String("a random string");
and not have to instantiate Encrypt and Decrypt in order to access the methods inside of them. Do I use something other than a class to contain them or?
CodePudding user response:
First of all you have to make they public :
public Encrypt encrypt;
public Decrypt decrypt;
and create a constructor for Crypto and instantiate both above properties inside constructor. Hope this helps
CodePudding user response:
As starting point I recommend using interfaces
or abstract
classes.
For example
interface Encrypt
{
void DoEncrypt();
}
interface Decrypt
{
void DoDecrypt();
}
public class MyClass: Encrypt, Decrypt
{
// declare interfaces methods
}
Of course this is not a tested example, it's just to show you what an starting point would be. Using interfaces will force you to code the interface methods inside the main class (implement interface methods)
Another way is using inheritance
class Encrypt{
public void DoSomeThing()
{
// ...
}
}
class MyClass:Encrypt
{
public MyClass()
{
this.DoSomeThing();
}
}
But the use of inheritance has a native limitation in C#. You will only be able to inherit for exactly one parent class. That's why I recommend interfaces. The use of abstract classes has the same limitation as the use of normal classes
CodePudding user response:
First of all, it probably makes no sense to add your Crypto
class initialization logic into separate method. Consider to remove method void GenerateKeys()
and place its code into Crypto
class constructor:
internal class Crypto
{
// private fields
//...
internal Encrypt encrypt;
internal Decrypt decrypt;
// constructor
internal Crypto()
{
// your GenerateKeys() code
}
// ...
}
Then, in your approach you can add this code to constructor, so your Encrypt
and Decrypt
instances are created along with the Crypto
instance:
encrypt = new Encrypt();
decrypt = new Decrypt();
Finally, remove modifier static
from your Encrypt
and Decrypt
methods definition to call these methods via class instances and to be able to use your non-static fields in its code.
Also consider to refactor method names in you Encrypt
and Decrypt
classes to match c# coding conventions:
internal class Encrypt
{
internal string GetString(string plainTextData)
{
}
internal Byte[] GetBytes(string plainTextData)
{
}
internal string GetString(byte[] bytesToEncrypt)
{
}
internal Byte[] GetBytes(byte[] bytesToEncrypt)
{
}
}
// same for Decrypt class