-3
我目前正在編寫一個程序,要求用戶輸入一個不超過4位數的數字,然後加密它,然後它會要求用戶輸入一個數字進行解密。我遇到的主要問題是,我不知道從數學邏輯開始。任何建議或例子開始將非常感謝,謝謝。加密和解密整數數據在C#
我目前正在編寫一個程序,要求用戶輸入一個不超過4位數的數字,然後加密它,然後它會要求用戶輸入一個數字進行解密。我遇到的主要問題是,我不知道從數學邏輯開始。任何建議或例子開始將非常感謝,謝謝。加密和解密整數數據在C#
寫在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);
}
}
這是一個類
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));
}
}
}
聲明:本只是一個例子。這個問題並不清楚。 我希望這個幫助。