Tùy chọn mã hóa dữ liệu

Một phần của tài liệu Nghiên cứu chữ ký số trong ứng dụng quản lý tài liệu lưu trữ quốc gia (Trang 53 - 61)

4. Kết luận

Với mục tiêu là nghiên cứu chữ ký số và ứng dụng chữ ký số trong quản lý tài liệu quốc gia, xét về mặt kết quả tổng quan, Luận văn đã đạt được các yêu cầu sau

Nội dung đạt đƣợc

- Nghiên cứu tổng quan về bảo mật an toàn thông tin, an toàn thông tin trong tài liệu lưu trữ.

- Khảo sát thực trạng tài liệu đang được lưu trữ tại câc trung tâm Lưu trữ quốc gia, đưa ra được số liệu cụ thể tài liệu số đang được lưu trữ tại các Trung tâm Lưu trữ quốc gia.

- Nghiên cứu tổng quan về chữ ký số, hàm Băm, hạ tầng khóa công khai - Xây dựng ứng dụng ký số cho tài liệu lưu trữ quôc gia.

Hạn chế

- Ứng dụng mới chỉ ký số cho tài liệu định dạng JPEG, chưa ký số cho các định dạng khác

Hƣớng phát triển

Trong thời gian tới ứng dụng sẽ bổ sung thêm tính năng ký các file ghi âm, và các định dạng video khác. Ngoài ra ứng dụng cũng cần hoàn tiện thêm khi triển khai thực tế tại các Trung tâm Lưu trữ quốc gia giúp đảm bảo tính toàn vẹn và xác thực của tài liệu cũng như tính bảo mật của tài liệu./.

5. Tài liệu tham khảo Tiếng Việt Tiếng Việt

1. Trịnh Nhật Tiến, Giáo trình An toàn dữ liệu;

2. TS. Hồ Văn Hương, Ths Đào Thị Ngọc Thuỳ, Ứng dụng hệ thống kiểm soát truy nhập mạng theo mô hình truy nhập một lần, Tạp chí An toàn thông tin, số 1 (025) 2013.

3. TS. Hồ Văn Hương, KS. Hoàng Chiến Thắng, Ký số và xác thực trên nền tảng web, Tạp chí An toàn thông tin, số 2 (026) năm 2013;

4. TS. Hồ Văn Hương, KS. Hoàng Chiến Thắng, KS. Nguyễn Quốc Uy Giải pháp bảo mật và xác thực thư điện tử, Tạp chí An toàn thông tin số 04 (028), 2013;

5. Vnisa, Báo cáo hiện trạng ATTT tại Việt Nam 2010.

6. Báo cáo tổng hợp tình hình lưu trữ và sử dụng cơ sở dữ liệu tài liệu lưu trữ tại các Trung tâm Lưu trữ quốc gia năm 2014 của Cục văn thư và Lưu trữ nhà nước.

Tiếng Anh

7. William Stallings, Cryptography and Network Security Principles and Practices, Fourth Edition, November 16, 2005;

8. Whitfield Diffie and Martin E. Hellman, New Directions in Cryptography, 1976;

9. Bart Van Rompay. Analysis and Desigbn of Cryptographic Hash Functions, MAC Algorithms and Block Ciphers, Juni 2004;

10.Burt Kaliski,RSA Laboratories, The Mathematics of the RSA Public- Key Cryptosystem; Website 11.www.antoanthongtin.vn 12.www.en.wikipedia.org 13.www.vi.wikipedia.org 14.www.pki.openca.org 15.www.ckca.vn

6. Phụ lục: Mã nguồn chƣơng trình

private void btnConvert_Click(object sender, EventArgs e) {

if (txtSrcFdr.Text == "") {

MessageBox.Show("Bạn chưa chọn thư mục nguồn"); return;

}

if (!Directory.Exists(txtSrcFdr.Text)) {

MessageBox.Show("Thư mục nguồn bạn chọn không tồm tại"); return;

}

if (txtDestFdr.Text == "") {

MessageBox.Show("Bạn chưa chọn thư mục đích"); return; } if (!Directory.Exists(txtDestFdr.Text)) { try { Directory.CreateDirectory(txtDestFdr.Text); } catch {

MessageBox.Show("Không tạo được thư mục file ký số"); return;

} }

success = false; bw.RunWorkerAsync();

toolStripProgressBar1.Style = ProgressBarStyle.Marquee; }

private void bw_RunWorkerCompleted(object sender,

RunWorkerCompletedEventArgs e)

{

toolStripProgressBar1.Style = ProgressBarStyle.Blocks; toolStripProgressBar1.Value = 0;

if (success)

MessageBox.Show("Bạn đã chuyển đổi thành công: " + TongFileTao +

"/" + TongFileTao + TongFileLoi, "Hoan thanh", MessageBoxButtons.OK,

MessageBoxIcon.Information);

PdfSharp.Pdf.PdfDocument AddPage(PdfSharp.Pdf.PdfDocument doc, XSize

size, string strFileName, int intPage) {

double Height = 0, Width = 0;

XGraphics xgr = XGraphics.FromPdfPage(doc.Pages[intPage]); XImage img = XImage.FromFile(strFileName);

if (XUnit.FromPoint(size.Height) < XUnit.FromPoint(img.Height)) {

Height = Math.Round((XUnit.FromPoint(size.Height) -

XUnit.FromPoint(img.Height)) / 2);

}

if (XUnit.FromPoint(img.Width) < XUnit.FromPoint(size.Width)) {

Width = Math.Round((XUnit.FromPoint(size.Width) -

XUnit.FromPoint(img.Width)) / 2);

}

xgr.DrawImage(img, 20, 20); return doc;

}

void CreateAllFilePDF(string strSounrcetFolder, ref int TongFileTao, ref int TongFileLoi)

{

CreateFilePDF(strSounrcetFolder, ref TongFileTao, ref TongFileLoi); string[] subdirectoryEntries =

Directory.GetDirectories(strSounrcetFolder);

foreach (string subdirectory in subdirectoryEntries) {

CreateAllFilePDF(subdirectory, ref TongFileTao, ref TongFileLoi);

} }

void CreateFilePDF(string strSounrcetFolder, ref int TongFileTao, ref int TongFileLoi)

{ try {

XSize size = new XSize(XUnit.FromMillimeter(297),

XUnit.FromMillimeter(210));

PdfSharp.Pdf.PdfDocument doc = new PdfSharp.Pdf.PdfDocument(); string strTargetFile = txtDestFdr.Text +

strSounrcetFolder.Substring(txtSrcFdr.Text.Length, strSounrcetFolder.Length - txtSrcFdr.Text.Length);

strTargetFile += "\\" + new

DirectoryInfo(strSounrcetFolder).Name + ".pdf";

string strTarrgetFolder = Path.GetDirectoryName(strTargetFile); if (CreateDir(strTarrgetFolder) != 1)

string[] filePaths = Directory.GetFiles(strSounrcetFolder,

"*.jpg", SearchOption.TopDirectoryOnly); if (filePaths.Length >= 0) {

for (int intThutu = 0; intThutu < filePaths.Length; intThutu++)

{

doc.Pages.Add(new PdfSharp.Pdf.PdfPage());

AddPage(doc, size, filePaths[intThutu], intThutu); } } doc.Save(strTargetFile); doc.Close(); success = true; TongFileTao += 1; }

catch (Exception ex) {

MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK,

MessageBoxIcon.Information);

TongFileLoi += 1; }

}

private void bw_DoWork(object sender, DoWorkEventArgs e) {

try {

TongFileTao = 0; TongFileLoi = 0;

CreateAllFilePDF(txtSrcFdr.Text, ref TongFileTao, ref TongFileLoi);

}

catch (Exception ex) {

MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK,

MessageBoxIcon.Error); } } Class Ký số using System; using System.Collections.Generic; using System.Text; using Org.BouncyCastle.Crypto; using Org.BouncyCastle.X509; using System.Collections; using Org.BouncyCastle.Pkcs;

using iTextSharp.text.pdf; using System.IO; using iTextSharp.text.xml.xmp; using Org.BouncyCastle.Security; namespace TTTH.DAITD.KYSO { class PDFSigner {

private string inputPDF = ""; private string outputPDF = ""; private Cert myCert;

private MetaData metadata;

public PDFSigner(string input, string output) {

this.inputPDF = input; this.outputPDF = output; }

public PDFSigner(string input, string output, Cert cert) {

this.inputPDF = input; this.outputPDF = output; this.myCert = cert; }

public PDFSigner(string input, string output, MetaData md) {

this.inputPDF = input; this.outputPDF = output; this.metadata = md; }

public PDFSigner(string input, string output, Cert cert, MetaData md) { this.inputPDF = input; this.outputPDF = output; this.myCert = cert; this.metadata = md; }

public void Sign(PDFSignatureAP sigAP, bool encrypt, PDFEncryption Enc) {

PdfReader reader = new PdfReader(this.inputPDF);

FileStream fs = new FileStream(this.outputPDF, FileMode.Create,

FileAccess.Write);

PdfStamper st;

if (this.myCert == null) {

st = new PdfStamper(reader, fs); }

else {

st = PdfStamper.CreateSignature(reader, fs, '\0', null, sigAP.Multi);

}

if (encrypt && Enc != null) Enc.Encrypt(st);

//st.SetEncryption(PdfWriter.STRENGTH128BITS, "user", "owner", PdfWriter.ALLOW_COPY);

st.MoreInfo = this.metadata.getMetaData();

st.XmpMetadata = this.metadata.getStreamedMetaData();

if (this.myCert == null) //No signature just write meta-data and quit

{

st.Close(); return; }

PdfSignatureAppearance sap = st.SignatureAppearance; sap.SetCrypto(this.myCert.Akp, this.myCert.Chain, null,

PdfSignatureAppearance.WINCER_SIGNED); sap.Reason = sigAP.SigReason; sap.Contact = sigAP.SigContact; sap.Location = sigAP.SigLocation; if (sigAP.Visible) {

iTextSharp.text.Rectangle rect = st.Reader.GetPageSize(sigAP.Page);

sap.Image = sigAP.RawData == null ? null : iTextSharp.text.Image.GetInstance(sigAP.RawData);

sap.Layer2Text = sigAP.CustomText;

sap.SetVisibleSignature(new iTextSharp.text.Rectangle(sigAP.SigX,

sigAP.SigY, sigAP.SigX + sigAP.SigW, sigAP.SigY + sigAP.SigH), sigAP.Page, null); }

PdfSignature dic = new PdfSignature(PdfName.ADOBE_PPKLITE, new

PdfName("adbe.pkcs7.detached"));

dic.Reason = sap.Reason; dic.Location = sap.Location; dic.Contact = sap.Contact;

dic.Date = new PdfDate(sap.SignDate); sap.CryptoDictionary = dic;

int contentEstimated = 15000;

// Preallocate excluded byte-range for the signature content (hex

encoded)

Dictionary<PdfName, int> exc = new Dictionary<PdfName, int>(); exc[PdfName.CONTENTS] = contentEstimated * 2 + 2;

sap.PreClose(exc);

"SHA-256", false);

IDigest messageDigest = DigestUtilities.GetDigest("SHA-256"); Stream data = sap.RangeStream;

byte[] buf = new byte[8192]; int n;

while ((n = data.Read(buf, 0, buf.Length)) > 0) {

messageDigest.BlockUpdate(buf, 0, n); }

byte[] hash = new byte[messageDigest.GetDigestSize()]; messageDigest.DoFinal(hash, 0);

DateTime cal = DateTime.Now; byte[] ocsp = null;

if (this.myCert.Chain.Length >= 2) {

String url = PdfPKCS7.GetOCSPURL(this.myCert.Chain[0]); if (url != null && url.Length > 0)

ocsp = new OcspClientBouncyCastle(this.myCert.Chain[0], this.myCert.Chain[1], url).GetEncoded();

}

byte[] sh = sgn.GetAuthenticatedAttributeBytes(hash, cal, ocsp); sgn.Update(sh, 0, sh.Length);

byte[] paddedSig = new byte[contentEstimated];

if (this.myCert.Tsc != null) {

byte[] encodedSigTsa = sgn.GetEncodedPKCS7(hash, cal, this.myCert.Tsc, ocsp);

System.Array.Copy(encodedSigTsa, 0, paddedSig, 0, encodedSigTsa.Length);

if (contentEstimated + 2 < encodedSigTsa.Length) throw new Exception("Không đủ không gian để ký"); }

else {

byte[] encodedSig = sgn.GetEncodedPKCS7(hash, cal); System.Array.Copy(encodedSig, 0, paddedSig, 0, encodedSig.Length);

if (contentEstimated + 2 < encodedSig.Length)

throw new Exception("Không đủ không gian để ký"); }

PdfDictionary dic2 = new PdfDictionary(); dic2.Put(PdfName.CONTENTS, new

PdfString(paddedSig).SetHexWriting(true));

sap.Close(dic2); }

} }

Một phần của tài liệu Nghiên cứu chữ ký số trong ứng dụng quản lý tài liệu lưu trữ quốc gia (Trang 53 - 61)