从Main()方法获得对局部变量的访问权

本文关键字:局部变量 访问权 Main 方法 | 更新日期: 2023-09-27 18:07:20

我是c#的初学者。到目前为止,我知道public static变量可以被任何其他类访问,public static方法中的局部变量不能被其他类访问。所以,在这种情况下,我想从Main()方法中访问所有的键,并对它们做一些事情。我该怎么做呢?一定有办法的。我想使用return,但它将只返回一个键值,我将选择的一个。是否有任何方法一次返回多个值?

生成密钥

的代码
class keyCreation
{
    public static void Key_Derivation_Function(byte[] password)
    {
        string salt = "12345678";
        byte[] saltbyte = Encoding.UTF8.GetBytes(salt);
        Console.WriteLine("Password length: " + password.Length);
        Console.WriteLine("Saltbyte lenght: " + saltbyte.Length);
        Rfc2898DeriveBytes keyGenerate = new Rfc2898DeriveBytes(password, saltbyte, 1000);
        byte[] key1 = keyGenerate.GetBytes(16);
        byte[] key2 = keyGenerate.GetBytes(32);
        byte[] key3 = keyGenerate.GetBytes(16);
        byte[] key4 = keyGenerate.GetBytes(32);
        byte[] key5 = keyGenerate.GetBytes(16);
        byte[] key6 = keyGenerate.GetBytes(16);
        byte[] key7 = keyGenerate.GetBytes(32);
    }
}

这是主要方法,

class Program
{
    static void Main(string[] args)
    {
        //user giving input
        Console.WriteLine("Plaintext:  ");
        string plaintext = Console.ReadLine();
        byte[] text = Encoding.UTF8.GetBytes(plaintext);
        Console.WriteLine("Enter Password: ");
        string pass = Console.ReadLine();
        byte[] password = Encoding.UTF8.GetBytes(pass);
        keyCreation.Key_Derivation_Function(password); 
        // get the keys and do something with the keys
    }
 }

从Main()方法获得对局部变量的访问权

作为示例,您可以返回包含静态方法的相同类:

public class keyCreation
{
    public byte[] Key1;
    public byte[] Key2;
    public byte[] Key3;
    public byte[] Key4;
    public byte[] Key5;
    public byte[] Key6;
    public byte[] Key7;
    public static keyCreation Key_Derivation_Function(byte[] password)
    {
        string salt = "12345678";
        byte[] saltbyte = Encoding.UTF8.GetBytes(salt);
        Console.WriteLine("Password length: " + password.Length);
        Console.WriteLine("Saltbyte lenght: " + saltbyte.Length);
        Rfc2898DeriveBytes keyGenerate = new Rfc2898DeriveBytes(password, saltbyte, 1000);
        return new keyCreation()
        {
            Key1 = keyGenerate.GetBytes(16),
            Key2 = keyGenerate.GetBytes(32),
            Key3 = keyGenerate.GetBytes(16),
            Key4 = keyGenerate.GetBytes(32),
            Key5 = keyGenerate.GetBytes(16),
            Key6 = keyGenerate.GetBytes(16),
            Key7 = keyGenerate.GetBytes(32)
        };
    }
}
class Program
{
    static void Main(string[] args)
    {
        //user giving input
        Console.WriteLine("Plaintext:  ");
        string plaintext = Console.ReadLine();
        byte[] text = Encoding.UTF8.GetBytes(plaintext);
        Console.WriteLine("Enter Password: ");
        string pass = Console.ReadLine();
        byte[] password = Encoding.UTF8.GetBytes(pass);
        var result = keyCreation.Key_Derivation_Function(password);
        // get the keys and do something with the keys        
        var key1 = result.Key1;
        var key2 = result.Key2;
        ...
    }
}

在这种情况下,类keyCreation包含所有KeyN字段,并从key_派生_function方法返回其实例。

另一个方法是out/ref params:

public class keyCreation
{
    public static void Key_Derivation_Function(byte[] password, out byte[] key1, out byte[] key2, ...)
    {
        string salt = "12345678";
        byte[] saltbyte = Encoding.UTF8.GetBytes(salt);
        Console.WriteLine("Password length: " + password.Length);
        Console.WriteLine("Saltbyte lenght: " + saltbyte.Length);
        Rfc2898DeriveBytes keyGenerate = new Rfc2898DeriveBytes(password, saltbyte, 1000);
        key1 = keyGenerate.GetBytes(16);
        key2 = keyGenerate.GetBytes(32);
        ...
    }
}
class Program
{
    static void Main(string[] args)
    {
        ...
        byte[] key1, key2, ...;
        keyCreation.Key_Derivation_Function(password, out key1, out key2, ...);
        ...
    }
}

还可以返回元组、数组的数组等

使函数返回所有键。也许是一个键数组(byte[][])。或者,如果不喜欢双数组,可以定义一个包装器类:

class Key { public byte[] Bytes; }

,返回Key[]

不要过度使用静态变量。它们使程序混乱,因为很难跟踪它们何时被写入和何时被读取。