2014-06-08 84 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[]

請勿過度使用靜態變量。他們讓程序感到困惑,因爲很難追蹤它們的寫入和讀取時間。

相關問題