从Main()方法访问本地变量

时间:2014-06-08 16:07:15

标签: c#

我是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

    }
 }

2 个答案:

答案 0 :(得分:0)

使该功能返回所有键。也许是一组键(byte[][])。或者,如果您不喜欢双数组定义自己的包装类:

class Key { public byte[] Bytes; }

并返回Key[]

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

答案 1 :(得分:0)

例如,您可以返回包含静态方法的同一个类:

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_Derivation_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, ...);
        ...
    }
}

此外,您还可以返回元组,数组数组等。