2017-02-21 5 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)); 

    } 

} 
} 

免責事項:これは単なる一例です。質問は明確ではありません。 私はこの助けを願っています。