2017-02-21 20 views
-3

В настоящее время я пишу программу, которая просит пользователя ввести число, не более 4 цифр, затем зашифровать его, после чего попросит пользователя расшифровать число. Основная проблема, с которой я сталкиваюсь, заключается в том, что я понятия не имею, с чего начать с математической логики. Любые предложения или примеры, с которых начать, будут очень благодарны, спасибо.Шифровать и дешифровать целочисленные данные в C#

ответ

0

Написал в java. Думаю, это поможет вам.

public class randStr{ 
     private static final Random random = new SecureRandom(); 

     //public static final int pass_length; 
     public static int pass_length; 

     public static String genRanPass() 
     { 
      //int pass_length; 
      Scanner scan = new Scanner(System.in); 
      String letters = "[email protected]#$%^&*"; 

      String pw = ""; 
      System.out.println("length: "); 
      pass_length = scan.nextInt(); 
      try{ 
       for(int i = 0; i<pass_length; i++){ 
        int index = (int)(random.nextDouble()*letters.length()); 
        pw += letters.substring(index,index+1); 
       } 
        //System.out.println(pw); 
        //return pw; 
      } 
      catch(Exception e){ 
       System.out.println(e); 
      } 
      System.out.println(pw); 
      return pw; 
     } 
     public static void main(String args[]){ 
      genRanPass(); 
      //System.out.println(pw); 
     } 
    } 
0

Это один класс

using System; 
using System.Security.Cryptography; 

public class DataProtectionSample 
{ 
// Create byte array for additional entropy when using Protect method. 
static byte[] s_aditionalEntropy = { 9, 8, 7, 6, 5 }; 

public static byte[] Protect(byte[] data) 
{ 
    try 
    { 
     // Encrypt the data using DataProtectionScope.CurrentUser. The result can be decrypted 
     // only by the same current user. 
     return ProtectedData.Protect(data, s_aditionalEntropy, DataProtectionScope.CurrentUser); 
    } 
    catch (CryptographicException e) 
    { 
     Console.WriteLine("Data was not encrypted. An error occurred."); 
     Console.WriteLine(e.ToString()); 
     return null; 
    } 
} 

public static byte[] Unprotect(byte[] data) 
{ 
    try 
    { 
     //Decrypt the data using DataProtectionScope.CurrentUser. 
     return ProtectedData.Unprotect(data, s_aditionalEntropy, DataProtectionScope.CurrentUser); 
    } 
    catch (CryptographicException e) 
    { 
     Console.WriteLine("Data was not decrypted. An error occurred."); 
     Console.WriteLine(e.ToString()); 
     return null; 
    } 
} 

public static void PrintValues(Byte[] myArr) 
{ 
    foreach (Byte i in myArr) 
    { 
     Console.Write("\t{0}", i); 
    } 
    Console.WriteLine(); 
} 

} 

Главный класс

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Security.Cryptography; 
using System.Text; 
using System.Threading.Tasks; 

namespace ConsoleApplication3 
{ 
class Program 
{ 
    static void Main(string[] args) 
    { 
     Console.WriteLine("Enter a four digit Number"); 
     var number = Console.ReadLine(); 
     if (!(number.Length == 4)) 
     { 
      Console.WriteLine("Enter a four digit Number"); 
     } 
     int i = int.Parse(number); 
     var bytearr = BitConverter.GetBytes(i); 
     method(bytearr); 
     Console.ReadLine(); 

    } 
    public static void method(byte[] secret) 
    { 
     byte[] encryptedSecret = DataProtectionSample.Protect(secret); 
     Console.WriteLine("The encrypted byte array is:"); 
     DataProtectionSample.PrintValues(encryptedSecret); 

     // Decrypt the data and store in a byte array. 
     byte[] originalData =   DataProtectionSample.Unprotect(encryptedSecret); 
     Console.WriteLine("{0}The original data is:", Environment.NewLine); 
     DataProtectionSample.PrintValues(originalData); 
     Console.WriteLine(BitConverter.ToInt32(originalData, 0)); 

    } 

} 
} 

Отказ от ответственности: Это всего лишь пример. Вопрос не ясен. Надеюсь, эта помощь.