Tôi đang cố xây dựng một chương trình nhỏ có thể khám phá xem tệp/tệp đã được truy cập hoặc sửa đổi bằng thuật toán ký DSA hay chưa.Kết quả khác nhau khi ký cùng một dữ liệu với cùng một khóa trong nhà cung cấp dịch vụ mã hóa DSA
Tôi sẽ cung cấp cho bạn mã ví dụ nhỏ này để mô tả sự cố. Giả sử chúng ta có một tệp c: \ Temporary \ Temp.txt. Chúng tôi có trong các nút chương trình 2 của chúng tôi. Khi chúng ta nhấp vào nút đầu tiên, chúng tôi tạo chữ ký số vào tên của tệp và thời gian truy cập cuối cùng trên đó. Chúng tôi cũng xuất các tham số và lưu chúng. Trên nút thứ hai, chúng tôi tạo lại chữ ký trên cùng một tệp bằng cách sử dụng các tham số mà chúng tôi đã xuất trước đó và so sánh chữ ký mới với chữ ký trước đó.
Vấn đề ở đây là chương trình (luôn luôn) cho tôi kết quả là tệp đã được truy cập !!!!
Tôi đã sử dụng trình gỡ lỗi để tìm sự cố và thấy rằng tất cả các giá trị biến đều giống nhau giữa 2 sự kiện button_click và sự khác biệt xảy ra trong quá trình ký.
Bạn vui lòng cho tôi biết sự cố ở đâu? Đây là mã:
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Security.Cryptography;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
string filename = @"C:\Temporary\Temp.txt";
DSAParameters parameters;
byte[] data_to_sign, signature;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Sign_button_Click(object sender, EventArgs e)
{
FileInfo f_info = new FileInfo(filename);
string file_information = f_info.FullName + f_info.LastAccessTime;
UnicodeEncoding byteEncoder = new UnicodeEncoding();
data_to_sign = byteEncoder.GetBytes(file_information);
DSACryptoServiceProvider dsaprovider = new DSACryptoServiceProvider();
parameters = dsaprovider.ExportParameters(true);
signature = dsaprovider.SignData(data_to_sign);
label1.Text = " Signature generated";
}
private void Verify_button_Click(object sender, EventArgs e)
{
FileInfo f_info = new FileInfo(filename);
string file_information = f_info.FullName + f_info.LastAccessTime;
UnicodeEncoding byteEncoder = new UnicodeEncoding();
data_to_sign = byteEncoder.GetBytes(file_information);
DSACryptoServiceProvider dsaprovider2 = new DSACryptoServiceProvider();
dsaprovider2.ImportParameters(parameters);
byte [] signature2 = dsaprovider2.SignData(data_to_sign);
if (signature == signature2)
label1.Text = "The file hasn't been accessed";
else
label1.Text = "Opp... The file has been accessed";
}
}
}
có SequenceEqual, hoặc chuyển sang chuỗi để so sánh. –