2014-06-08 52 views
0

我是C#的初学者。到目前为止,我知道一个public static变量可以被任何其他类访问,并且public static方法中的局部变量不能被其他类访问。所以,在这种情况下,我想要访问Main()方法中的所有密钥,并对它们进行操作。我怎样才能做到这一点?必须有一种方法。我想过使用return,但它只会返回一个我会选择的键值。有什么方法可以一次返回多个值吗?从Main()方法获取对局部变量的访问

这是代码生成的密钥

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 

    } 
} 

回答

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

您也可以返回元组,数组的数组等

+0

因此,返回类型是一个方法!哇之前从来没有见过这种类型的编码。谢谢 – Giliweed

0

使函数返回所有键。也许作为一组键(byte[][])。或者,如果你不喜欢双阵列自行定义包装类:

class Key { public byte[] Bytes; } 

,并返回一个Key[]

请勿过度使用静态变量。他们让程序感到困惑,因为很难追踪它们的写入和读取时间。

相关问题