在C#中加密和解密整数数据



我目前正在编写一个程序,要求用户输入一个数字,不超过4位数字,然后对其进行加密,然后在它要求用户解密的数字后。我遇到的主要问题是我不知道从哪里开始数学逻辑。感谢任何建议或示例将不胜感激,谢谢。

在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 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNPOQRSTUVWXYZ1234567890!@#$%^&*";
            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);
        }
    }

这是一个类

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

免责声明:这只是一个例子。问题不清楚。我希望这有所帮助。

最新更新