1. Trang chủ
  2. » Luận Văn - Báo Cáo

Nghiên cứu mã hóa dựa trên định danh ibe và ứng dụng vào bài toán kiểm soát quyền truy cập trong hệ thống truyền hình trả tiền

103 14 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Nội dung

ĐẠI HỌC THÁI NGUYÊN TRƢỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN THÔNG –––––––––––––––––––––––––––––––––––– NGUYỄN VÂN ANH NGHIÊN CỨU MÃ HÓA DỰA TRÊN ĐỊNH DANH – IBE VÀ ỨNG DỤNG VÀO BÀI TOÁN KIỂM SOÁT QUYỀN TRUY CẬP TRONG HỆ THỐNG TRUYỀN HÌNH TRẢ TIỀN Chuyên ngành: Khoa học máy tính Mã số: 60.48.01.01 LUẬN VĂN THẠC SĨ KHOA HỌC MÁY TÍNH Ngƣời hƣớng dẫn khoa học: TS PHẠM THẾ QUẾ THÁI NGUYÊN - 2013 Số hóa trung tâm học liệu http://www.lrc-tnu.edu.vn/ i LỜI CẢM ƠN Trên thực tế khơng có thành cơng mà khơng gắn liền với hỗ trợ, giúp đỡ, Trong suốt thời gian từ bắt đầu học tập trường đến nay, em nhận nhiều quan tâm, giúp đỡ quý Thầy Cô Khoa sau đại học trường Đại học Công nghệ Thông tin Truyền thông – Đại học Thái Nguyên với tri thức tâm huyết để truyền đạt vốn kiến thức quý báu cho chúng em suốt thời gian học tập trường, luôn tạo điều kiện tốt cho chúng em suốt trình theo học Em xin chân thành cảm ơn quý Thầy Cô Ban lãnh đạo nhà trường! Với lòng biết ơn sâu sắc em xin gửi lời cảm ơn tới TS Phạm Thế Quế, Khoa Công nghệ Thơng tin – Học viện Bưu Chính Viễn Thơng, cán trực tiếp hướng dẫn khoa học cho em Thầy dành nhiều thời gian cho việc hướng dẫn em cách nghiên cứu, đọc tài liệu, cài đặt thuật toán giúp đỡ em việc xây dựng chương trình, em xin chân thành cảm ơn Thầy! Và cuối em xin bày tỏ lòng chân thành biết ơn tới lãnh đạo khoa Công nghệ Thông tin trường đại học Kinh doanh Công nghệ Hà Nội bạn bè đồng nghiệp bên cạnh lúc em khó khăn tạo điều kiện thuận lợi giúp em hoàn thành luận văn Hà Nội, ngày 20 tháng 10 năm 2013 Học viên: Nguyễn Vân Anh Số hóa trung tâm học liệu http://www.lrc-tnu.edu.vn/ ii MỤC LỤC Lời cảm ơn i Mục lục ii Danh mục bảng iii Danh mục hình iv LỜI MỞ ĐẦU Chƣơng 1: T 1.1 Tổng quan mật mã 1.1.1 Giới thiệu 1.1.2 Các thành phần hệ thống mã hoá 1.1.3 Các tiêu chí đặc trưng hệ thống mã hố: 1.2 Kỹ thuật mật mã khóa cơng khai 1.2.1.Cấu trúc hệ thống mật mã khóa công khai 1.2.2 Thuật toán mật mã RSA 1.2.3 Thuật toán trao đổi khoá Diffie-Hellman 10 1.2.4 Đánh giá kỹ thuật mật mã bất đối xứng 12 1.3 Hàm băm 13 1.3.1 Xác thực thông tin 13 1.3.2 Các hàm băm bảo mật 17 1.3.3 Hàm băm MD5 19 1.4 Chữ ký số 20 1.4.1 Nguyên lý hoạt động chữ ký số: 20 1.4.2 Chuẩn chữ ký DSS 24 1.4.3 Thuật toán tạo chữ ký DSA (Digital Signature Algorithm): 27 1.5 Quản lý khoá 28 1.5.1 Quản lý khố cơng khai mật mã bất đối xứng: 28 1.5.2 Sử dụng mật mã bất đối xứng để trao đổi khóa bí mật: 30 Số hóa trung tâm học liệu http://www.lrc-tnu.edu.vn/ iii Chƣơng 2: 33 2.1 Tổng quan mật mã dựa định danh 33 2.1.1.Giới thiệu 33 2.1.2.Các khả ứng dụng IBE 33 2.1.3 Hệ thống nhận dạng IBE 35 2.2 Mã hóa dựa thuộc tính 38 2.2.1 Khái qt mã hóa dựa thuộc tính 38 2.2.2 Mã hóa dựa thuộc tính sách mã (CP-ABE) 39 2.2.3 Mã hóa dựa thuộc tính sách khóa (KP-ABE) 42 2.3 Lược đồ mã hóa dựa đinh danh ibe 43 2.4 Cài đặt (THE IMPLEMENTATION) 45 2.4.1 Các thuật toán sử dụng IBE 45 2.4.2 Cài đặt 46 2.5 So sánh ibe hệ thống khóa cơng khai truyền thống 49 2.5.1 Thuật tốn trao đổi khóa Diffie - Hellman 49 2.5.2 Hệ mật mã ElGamal 51 2.5.3 Hệ mật bất đối xứng sỡ đường cong Elliptic 51 2.5.4 Đánh giá kỹ thuật mật mã khóa cơng khai 52 2.5.5 Sự khác IBE hệ thống khóa cơng khai truyền thống 53 2.6 Hướng phát triển mật mã khóa cơng khai 54 2.6.1 Bảo mật điện toán đám mây (cloud computing) 54 2.6.2 Mở rộng mơ hình mã hóa 55 2.6.3 An toàn trước công vật lý 56 2.6.4 An toàn trước cơng máy tính lượng tử 56 Chƣơng 3: ỨNG DỤNG HỆ MÃ HÓA ĐỊNH DANH BẢO MẬT THÔNG TIN 57 3.1 Ứng dụng IBE xác minh chữ ký số nhận dạng hệ thống thư điện tử 57 Số hóa trung tâm học liệu http://www.lrc-tnu.edu.vn/ iv 3.1.1 Xây dựng hệ thống bảo mật dựa IBE 57 3.1.2 Các bước thực xây dựng hệ thống bảo mật 61 3.2 Ứng dụng IBE vào toán kiểm sốt quyền truy cập hệ thống truyền hình trả tiền 64 3.2.1 Mơ tả tốn 64 3.2.2 Thiết kế hệ thống 66 3.2.3 Chương trình thử nghiệm 68 KẾT LUẬN 70 Số hóa trung tâm học liệu http://www.lrc-tnu.edu.vn/ v DANH SÁCH CÁC HÌNH Trang Hình 1.1: Cấu trúc hệ thống mật mã quy ước Hình 1.2: Cấu trúc hệ thống mật mã bất đối xứng Hình 1.3: Thuật tốn trao đổi khố Diffie-Hellman 11 Hình 1.4: Xác thực thông tin dùng mật mã 15 Hình 1.5: Xác thực thông tin dùng MAC 16 Hình 1.6: Xác thực thơng tin dùng hàm băm 17 Hình 1.7: Một ứng dụng điển hình hàm băm 19 Hình 1.8: Chữ ký trực tiếp 23 Hình 1.9: Xác thực thơng tin dùng mật mã RSA dùng chữ ký số DSS 26 Hình 1.10: Tạo kiểm chứng chữ ký với DSS 27 Hình 1.11: Quản lý khố cơng khai dùng chứng thực khóa (Certificate 30 Hình 1.12: Dùng mật mã bất đối xứng để trao đổi khố 31 Hình 2.1: “Mã khóa riêng”, “mã khóa cơng khai”, “hệ thống bảo mật nhận dạng 37 Hình 2.2:Phương thức “mã khóa cơng khai” “chữ ký nhận dạng 38 Hình 2.3 Mã hoá hệ thống IBE 44 Hình 2.4 Giải mã hệ thống IBE 45 Hình 3.1 Mơ hình hệ thống nhận dạng IBE 61 Hình 3.2: Hệ thống mã hố mơ hình bảo mật 62 Hình 3.3 Sơ đồ phân tích hệ thống 67 Số hóa trung tâm học liệu http://www.lrc-tnu.edu.vn/ vi DANH SÁCH CÁC BẢNG Trang Bảng 1.1 So sánh thông số SHA-1 MD5 20 Bảng 2.1 Bốn thuật toán tạo nên lược đồ IBE 46 Bảng 2.2 So sánh hệ thống IBE hệ thống khố cơng khai truyền thống 54 Số hóa trung tâm học liệu http://www.lrc-tnu.edu.vn/ LỜI MỞ ĐẦU Mã hóa dựa định danh (Indetity based encryption -IBE) xem công nghệ mật mã có nhiều thuận tiện thực thi ứng dụng so với thuật tốn khóa cơng khai khác Đối với hệ mật mã khóa cơng khai truyền thống, việc cài đặt khó khăn tốn kém, ứng dụng thành cơng cơng nghệ khóa cơng khai việc sử dụng rộng rãi SSL, yêu cầu tương tác tối thiểu với người sử dụng dùng để xác thực máy chủ mã hóa truyền thơng với máy chủ Các ứng dụng mà yêu cầu người sử dụng quản lý sử dụng khóa cơng khai khơng thành cơng IBE cơng nghệ mã hố khố cơng khai, cho phép người sử dụng tính khố công khai từ chuỗi Chuỗi biểu diễn định danh dạng sử dụng không định danh để tính khố cơng khai, mà cịn chứa thơng tin thời hạn hợp lệ khố để tránh cho người sử dụng dùng khoá IBE để đảm bảo người sử dụng nhận khoá khác từ hệ thống IBE khác Trong chuỗi có chứa thơng tin cài đặt IBE cụ thể, chẳng hạn URL mà định danh máy chủ sử dụng cài đặt hệ thống IBE khác Khả tính khố mong muốn làm cho hệ thống IBE có tính chất khác với tính chất hệ thống khố cơng khai truyền thống, tính chất tạo ưu thực hành đáng kể nhiều tình Bởi vậy, có số tình khơng thể giải tốn với cơng nghệ khố cơng khai truyền thống, lại giải với IBE sử dụng IBE đơn giản nhiều cài đặt tốn nguồn lực để hỗ trợ Số hóa trung tâm học liệu http://www.lrc-tnu.edu.vn/ IBE không đề xuất khả mà cơng nghệ khóa cơng khai truyền thống khơng thể cung cấp cho phép tạo giải pháp để giải vấn đề khó khăn tốn triển khai công nghệ trước Đây lý để chọn đề tài có tên: định danh - IBE hệ thống truyền hình trả tiền Nội dung nghiên cứu: - Nghiên cứu hệ mã hóa dựa định danh ưu điểm hệ mã hóa so với hệ mã hóa cơng khai truyền thống - Nội dung luận văn gồm chƣơng: Chương 1: Tổng quan hệ mật mã khóa cơng khai Chương 2: Hệ mật mã dưa định danh Chương 3: Ứng dụng mã hóa dựa dịnh danh vào tốn kiểm sốt quyền truy cập hệ thống truyền hình trả tiền Số hóa trung tâm học liệu http://www.lrc-tnu.edu.vn/ Chƣơng 1.1 Tổng quan mật mã 1.1.1 Giới thiệu Mật mã (Encryption) kỹ thuật sở quan trọng bảo mật thông tin Nguyên tắc mật mã biến đổi thông tin gốc thành dạng thơng tin bí mật mà có thực thể tham gia xử lý thông tin cách hợp lệ hiểu Một thực thể hợp lệ người, máy tính hay phần mềm phép nhận thơng tin Để giải mã thơng tin mật, thực thể cần phải biết cách giải mã (tức biết thuật tốn giải mã) thơng tin cộng thêm (khóa bí mật) Q trình chuyển thơng tin gốc thành thơng tin mật theo thuật tốn gọi q trình mã hố (encryption) Q trình biến đổi thông tin mật dạng thông tin gốc ban đầu gọi trình giải mã (decryption) Đây hai q trình khơng thể tách rời kỹ thuật mật mã mật mã (giấu thơng tin) có ý nghĩa ta giải mã (phục hồi lại) thơng tin Do vậy, dùng thuật ngữ mật mã có nghĩa bao hàm mã hóa giải mã Kỹ thuật mã hoá chia thành hai loại: mã hoá dùng khoá đối xứng (symmetric key encryption) mã hoá dùng khoá bất đối xứng (asymmetric key encryption) trình bày phần 1.1.2 Các thành phần hệ thống mã hố Hình 1.1 mơ tả ngun tắc chung hệ thống mật mã quy ước Các thành phần hệ thống mật mã điển hình bao gồm: - Plaintext: thơng tin gốc cần truyền hệ thống thông tinEncryption algorithm: thuật tốn mã hóa, cách thức tạo thơng tin mật từ thơng tin gốc Số hóa trung tâm học liệu http://www.lrc-tnu.edu.vn/ 82 BigInteger four = BigInteger.valueOf(4); BigInteger signOpenExp = BigInteger.valueOf(e); while (true) { P = BigInteger.probablePrime(security, new Random()); Q = BigInteger.probablePrime(security, new Random()); if (P.compareTo(Q) != && P.mod(four).compareTo(tree) == && Q.mod( four).compareTo(tree) == && signOpenExp.gcd(ResidueCalculation euler(P, Q)).compareTo(BigInteger.ONE) == 0) { break; } } MPK = P.multiply(Q); this.getSecretExponent(); } static public BigInteger genPkID(String id, BigInteger P, BigInteger Q, BigInteger MPK) throws NoSuchAlgorithmException { BigInteger a; int j = 0; int k = 0; MessageDigest md = MessageDigest.getInstance("SHA"); md.update(id.getBytes()); byte[] hash = md.digest(); a = new BigInteger(hash); a = a.abs(); a = a.mod(MPK); while (true) { j = ResidueCalculation.Jacobi(a, P); k = ResidueCalculation.Jacobi(a, Q); if (j == -1 && k == -1) { return MPK.subtract(a); } else if (j == && k == 1) { return a; } else { Số hóa trung tâm học liệu http://www.lrc-tnu.edu.vn/ 83 a = a.add(BigInteger.ONE); } } } public BigInteger keyExtract(String id) throws NoSuchAlgorithmException { BigInteger SKE; BigInteger a = genPkID(id, P, Q, MPK); BigInteger exp = MPK.add(BigInteger.valueOf(5)).subtract(P.add(Q)) divide(BigInteger.valueOf(8)); SKE = a.modPow(exp, MPK); return SKE; } private void getSecretExponent() { BigInteger phi = ResidueCalculation.euler(P, Q); this.d = BigInteger.valueOf(e).modInverse(phi); } public BigInteger signKeyExtract(String id) throws NoSuchAlgorithmException { BigInteger SKS; MessageDigest md = MessageDigest.getInstance("SHA"); md.update(id.getBytes()); byte[] hash = md.digest(); BigInteger _id = new BigInteger(hash); SKS = _id.modPow(d, MPK); return SKS; } }File Util.java package org.suai.idbased.util; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.math.BigInteger; import org.suai.idbased.crypto.Cryptocontainer; public class Util { private final static int aesKeyLen = 128; static public byte[] KeyToBinary(byte[] raw) Số hóa trung tâm học liệu http://www.lrc-tnu.edu.vn/ 84 { StringBuffer sb = new StringBuffer(); byte[] binaryKey; for (int i = 0; i < raw.length; i++) { String binary = null; binary = Integer.toBinaryString(raw[i] & 0xff); if (binary.length() < 8) { for (int k = 0; k < - binary.length(); k++) { sb.append("0"); } } sb.append(binary); } binaryKey = new byte[sb.length()]; for (int i = 0; i < sb.length(); i++) { binaryKey[i] = sb.charAt(i) == '0' ? (byte) : (byte) 1; } return binaryKey; } static public void GetEncryptedKey(DataInputStream din, boolean keytype, Cryptocontainer cc, BigInteger[] encryptedAESKey) throws IOException { int sizeofEncrAESKeyBit; din.skipBytes(8); if (keytype == false) { for (int i = 0; i < aesKeyLen; i++) { sizeofEncrAESKeyBit = din.readInt(); byte[] buff = new byte[sizeofEncrAESKeyBit]; din.read(buff); encryptedAESKey[i] = new BigInteger(buff); } din.skipBytes(cc.secondKeySize); } else Số hóa trung tâm học liệu http://www.lrc-tnu.edu.vn/ 85 { din.skipBytes(cc.firstKeySize); for (int i = 0; i < aesKeyLen; i++) { sizeofEncrAESKeyBit = din.readInt(); byte[] buff = new byte[sizeofEncrAESKeyBit]; din.read(buff); encryptedAESKey[i] = new BigInteger(buff); } } } static public byte[] BinaryToByteKey(int[] binaryAESKey) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < binaryAESKey.length; i++) { sb.append(binaryAESKey[i]); } int from = 0, to = 8; String byte_of_key = new String(); byte[] raw = new byte[aesKeyLen/8]; String aes_key = sb.toString(); for (int i = 0; i < aesKeyLen/8; i++) { byte_of_key = aes_key.substring(from, to); raw[i] = Integer.valueOf(byte_of_key, 2).byteValue(); from = to; to = to + 8; } return raw; } static public void usage() { System.out.println ("\n"); System.out.println(" Clifford Cocks Identity Based Cryptosystem for mail server (autonome version) "); System.out.println(""); System.out.println("To run the program, type the following:"); System.out.println("java -jar ibc.jar command [parameters]"); System.out.println("List of available commands:"); System.out.println("setup"); System.out.println("keyextract"); System.out.println("encrypt"); System.out.println("decrypt"); System.out.println( Số hóa trung tâm học liệu http://www.lrc-tnu.edu.vn/ 86 "Print help to see the details information of the program usage"); } static public void help() { System.out.println("\n"); System.out.println( "Please note: parameter order is irrelevant"); System.out.println(); System.out.println(" "); System.out.println("About setup:"); System.out.println( "Setup performs the initialization of Public Key Generator for domain and works with keystorage - " + "generate the necessary parameters for the cryptosystem, namely:"); System.out.println( "-MasterPublicKey generation of a given length (parameter security)"); System.out.println( "-MasterSecretKey generation of a given length (parameter security*2)"); System.out.println("These settings are stored in keystorage (encrypted by AES on password, specified by the user)" + "(parameters -keystorage and -mpk)"); System.out.println ("Otherwise, they are saved in the default directory"); System.out.println(); System.out.println("Usage:"); System.out.println( "setup -security 'positive integer' -mpk 'file_path' -keystorage 'path_to_keystorage' -domain 'domain-name (example@example.org)' -password 'root-pass-to-keystorage'"); System.out.println(" "); System.out.println(" "); System.out.println("About keyextract:"); System.out.println( "KeyExtract generates a secret key for encryption and signature for a user specified text identifier"); System.out.println ("In this case, you must specify the path to the master public key system, generated by running setup (if you are running with the -mpk parameter)"); System.out.println(); System.out.println("Usage"); System.out.println( "keyextract -keystorage 'path_to_keystorage' -password 'root-password-to-keystorage' -sk 'output_path_to_the_encryption_secret_key' -sks 'output_path_to_the_signing_secret_key' -domain 'domain-name' -id 'e-mail adress'"); System.out.println(" "); System.out.println(" "); System.out.println("About encrypt:"); System.out.println( "encrypt makes encryption and signature (cryptocontainer) of a user-specified file"); System.out.println("Usage:"); Số hóa trung tâm học liệu http://www.lrc-tnu.edu.vn/ 87 System.out.println( "encrypt -to 'recipient-e-mail' -sks 'sender signing secret key' -mpk 'path_to_mpk'" + "-in 'path_to_the_file_for_encryption' -out 'output_path_to_the_encrypted_file'"); System.out.println(" "); System.out.println(" "); System.out.println("About decrypt"); System.out.println( "decrypt makes decryption and signature verification previously encrypted files"); System.out.println("Usage:"); System.out.println( "decrypt -sk 'path-to-recipient-encryption-secret-key'" + "-in 'path_to_the_encrypted_file' -out 'output_path_to_the_decrypted_file' -to 'your-e-mail-adres' -from 'sender email adress' -mpk 'path-to-mpk'"); System.out.println(" "); } static public void invalidParameters() { System.out.println( "Entered incorrect settings or missing required parameters, type help for calling tips"); } static public byte[] writeKeyData(BigInteger data) { return data.toByteArray(); } static public BigInteger readKeyData(FileInputStream in) throws IOException { byte[] byteKey = new byte[in.available()]; in.read(byteKey); return new BigInteger(byteKey); } } 3.2.4 Chƣơng trình viết ngôn ngữ Java máy client File client.java package org.suai.idbased.crypto; import org.suai.idbased.util.Util; import org.suai.idbased.util.ResidueCalculation; import java.io.ByteArrayInputStream; Số hóa trung tâm học liệu http://www.lrc-tnu.edu.vn/ 88 import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.math.BigInteger; import java.security.InvalidKeyException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Random; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.KeyGenerator; import javax.crypto.NoSuchPaddingException; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; public class Client { private final int aesKeyLen = 128; private final int intLen = java.lang.Integer.SIZE/8; public BigInteger genPkID(String id, BigInteger MPK) throws NoSuchAlgorithmException { BigInteger a; int j = 0; MessageDigest md = MessageDigest.getInstance("SHA"); md.update(id.getBytes()); byte[] hash = md.digest(); a = new BigInteger(hash); a = a.abs(); a = a.mod(MPK); while (true) { j = ResidueCalculation.Jacobi(a, MPK); if (j == 1) { return a; } else { a = a.add(BigInteger.ONE); } } Số hóa trung tâm học liệu http://www.lrc-tnu.edu.vn/ 89 } public void encryptKey(byte[] binaryKey, BigInteger[] ciphertext, BigInteger[] invCiphertext, BigInteger MPK, BigInteger PkID) { byte m = 0; int j = 0; BigInteger invT; BigInteger t; BigInteger b; Random rand = new Random(); int length = MPK.bitLength() / 4; int keyLength = binaryKey.length; for (int i = 0; i < keyLength ; i++) { m = binaryKey[i]; while (true) { t = new BigInteger(length, rand); j = ResidueCalculation.Jacobi(t, MPK); if ((m == && j == -1) || (m == && j == 1)) { invT = t.modInverse(MPK); b = PkID.multiply(invT); ciphertext[i] = t.add(b).mod(MPK); invCiphertext[i] = t.subtract(b).mod(MPK); break; } } } } private void writeEncryptedData(DataOutputStream dos, BigInteger[] ciphertext, BigInteger[] invCiphertext, byte[] encrypted_data) throws IOException { int keySizeFirst = 0; int keySizeScnd = 0; for (int i = 0; i < ciphertext.length; i++) { keySizeFirst = keySizeFirst + ciphertext[i].toByteArray().length; } for (int i = 0; i < invCiphertext.length; i++) { keySizeScnd = keySizeScnd + invCiphertext[i].toByteArray().length; Số hóa trung tâm học liệu http://www.lrc-tnu.edu.vn/ 90 } dos.writeInt(keySizeFirst + this.aesKeyLen * this.intLen); dos.writeInt(keySizeScnd + this.aesKeyLen * this.intLen); for (int i = 0; i < ciphertext.length; i++) { dos.writeInt(ciphertext[i].toByteArray().length); dos.write(ciphertext[i].toByteArray()); } for (int i = 0; i < invCiphertext.length; i++) { dos.writeInt(invCiphertext[i].toByteArray().length); dos.write(invCiphertext[i].toByteArray()); } int encrypted_data_size = encrypted_data.length; dos.writeInt(encrypted_data_size); dos.write(encrypted_data); } private void writeSignature(FileInputStream fis, DataOutputStream dos, Sign signature, BigInteger MPK, BigInteger sk, long pk) throws IOException, NoSuchAlgorithmException { byte[] dataToHash = new byte[fis.available()]; fis.read(dataToHash); BigInteger[] sign = signature.getSign(dataToHash, sk, pk, MPK); dos.writeInt(sign[0].toByteArray().length); dos.writeInt(sign[1].toByteArray().length); dos.write(sign[0].toByteArray()); dos.write(sign[1].toByteArray()); } private void writeSignature(ByteArrayInputStream bis, DataOutputStream dos, Sign signature, BigInteger MPK, BigInteger sk, long pk) throws IOException, NoSuchAlgorithmException { byte[] dataToHash = new byte[bis.available()]; bis.read(dataToHash); BigInteger[] sign = signature.getSign(dataToHash, sk, pk, MPK); dos.writeInt(sign[0].toByteArray().length); dos.writeInt(sign[1].toByteArray().length); dos.write(sign[0].toByteArray()); dos.write(sign[1].toByteArray()); } Số hóa trung tâm học liệu http://www.lrc-tnu.edu.vn/ 91 private boolean verifySignature(DataInputStream ds, Sign signature, String id, byte[] data, long pkey, BigInteger MPK) throws NoSuchAlgorithmException, IOException { byte[] tByte = new byte[ds.readInt()]; byte[] sByte = new byte[ds.readInt()]; ds.read(tByte); ds.read(sByte); BigInteger[] sign = new BigInteger[2]; sign[0] = new BigInteger(tByte); sign[1] = new BigInteger(sByte); return signature.verifySign(data, id, sign, pkey, MPK); } public int[] decryptKey(BigInteger[] encryptedAESKey, BigInteger SkID, BigInteger MPK, int keylength) { int[] binaryAESKey = new int[keylength]; int Jacobi; BigInteger root = SkID.add(SkID).mod(MPK); for (int i = 0; i < keylength; i++) { BigInteger currBit = encryptedAESKey[i].add(root); Jacobi = ResidueCalculation.Jacobi(currBit, MPK); if (Jacobi == 1) { binaryAESKey[i] = 1; } else { binaryAESKey[i] = 0; } } return binaryAESKey; } public byte[] encryptData(String inname, String outname, BigInteger PkID, BigInteger MPK, BigInteger sk, long pk) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IOException, IllegalBlockSizeException, BadPaddingException { FileOutputStream fout = new FileOutputStream(outname); Số hóa trung tâm học liệu http://www.lrc-tnu.edu.vn/ 92 FileInputStream fin = new FileInputStream(inname); DataOutputStream dos = new DataOutputStream(fout); Sign signature = new Sign(); KeyGenerator kgen = KeyGenerator.getInstance("AES"); kgen.init(this.aesKeyLen); SecretKey skey = kgen.generateKey(); byte [] raw = skey.getEncoded(); SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec); byte[] binaryKey = Util.KeyToBinary(raw); BigInteger [] ciphertext = new BigInteger[binaryKey.length]; BigInteger [] invCiphertext = new BigInteger[binaryKey.length]; encryptKey(binaryKey, ciphertext, invCiphertext, MPK, PkID); byte[] dataToEncrypt = new byte[fin.available()]; fin.read(dataToEncrypt); byte[] encryptedData = cipher.doFinal(dataToEncrypt); writeEncryptedData(dos, ciphertext, invCiphertext, encryptedData); FileInputStream fis = new FileInputStream(outname); writeSignature(fis, dos, signature, MPK, sk, pk); dos.close(); fout.close(); fis.close(); return dataToEncrypt; } public byte[] encryptData(ByteArrayInputStream is, BigInteger PkID, BigInteger MPK, BigInteger sk, long pk) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IOException, IllegalBlockSizeException, BadPaddingException { ByteArrayOutputStream out = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(out); Sign signature = new Sign(); KeyGenerator kgen = KeyGenerator.getInstance("AES"); kgen.init(this.aesKeyLen); SecretKey skey = kgen.generateKey(); byte[] raw = skey.getEncoded(); SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec); byte[] binaryKey = Util.KeyToBinary(raw); BigInteger[] ciphertext = new BigInteger[binaryKey.length]; BigInteger[] invCiphertext = new BigInteger[binaryKey.length]; encryptKey(binaryKey, ciphertext, invCiphertext, MPK, PkID); byte[] dataToEncrypt = new byte[is.available()]; is.read(dataToEncrypt); byte[] encryptedData = cipher.doFinal(dataToEncrypt); writeEncryptedData(dos, ciphertext, invCiphertext, encryptedData); Số hóa trung tâm học liệu http://www.lrc-tnu.edu.vn/ 93 ByteArrayInputStream bis = new ByteArrayInputStream (out.toByteArray()); writeSignature(bis, dos, signature, MPK, sk, pk); byte [] result = out.toByteArray(); dos.close(); out.close(); is.close(); return result; } public byte[] decryptData(String inname, String outname, String id, String idfrom, BigInteger SkID, BigInteger MPK, long pkey) throws FileNotFoundException, IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException { boolean negative = false; BigInteger[] encryptedAESKey = new BigInteger[this.aesKeyLen]; Cryptocontainer cc = new Cryptocontainer(); BigInteger pk = genPkID(id, MPK); BigInteger quadr = SkID.modPow(BigInteger.valueOf(2), MPK); Sign signature = new Sign(); if (quadr.compareTo(pk) == 0) { negative = false; } else { negative = true; } FileInputStream fin = new FileInputStream(inname); DataInputStream ds = new DataInputStream(fin); cc = cc.getCryptocontainerParameters(ds); if (cc == null) { return null; } ds.close(); fin.close(); fin = new FileInputStream(inname); ds = new DataInputStream(fin); byte[] data = new byte[cc.dataSize - cc.signatureSize]; ds.read(data); boolean check = verifySignature(ds, signature, idfrom, data, pkey, MPK); if (check == false) { return null; } fin.close(); ds.close(); Số hóa trung tâm học liệu http://www.lrc-tnu.edu.vn/ 94 fin = new FileInputStream(inname); DataInputStream din = new DataInputStream(fin); Util.GetEncryptedKey(din, negative, cc, encryptedAESKey); int[] binaryAESKey = decryptKey(encryptedAESKey, SkID, MPK, this.aesKeyLen); byte[] raw = Util.BinaryToByteKey(binaryAESKey); din.close(); SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, skeySpec); byte[] encryptedData = new byte[cc.encryptedDataSize]; System.arraycopy(data, cc.firstKeySize + cc.secondKeySize + 12, encryptedData, 0, cc.encryptedDataSize); byte[] decryptedData = cipher.doFinal(encryptedData); FileOutputStream fos = new FileOutputStream(outname); fos.write(decryptedData); fos.close(); return decryptedData; } public byte[] decryptData(ByteArrayInputStream is, String id, String idfrom, BigInteger SkID, BigInteger MPK, long pkey) throws FileNotFoundException, IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException { boolean negative = false; BigInteger[] encryptedAESKey = new BigInteger[128]; Cryptocontainer cc = new Cryptocontainer(); BigInteger pk = genPkID(id, MPK); BigInteger quadr = SkID.modPow(BigInteger.valueOf(2), MPK); Sign signature = new Sign(); if (quadr.compareTo(pk) == 0) { negative = false; } else { negative = true; } DataInputStream ds = new DataInputStream(is); cc = cc.getCryptocontainerParameters(ds); if (cc == null) { return "[IBC JAMES MAILET]: Failed to decrypt: letter was changed (Cryptocontainer is damaged)".getBytes(); } ds.close(); is.reset(); ds = null; Số hóa trung tâm học liệu http://www.lrc-tnu.edu.vn/ 95 ds = new DataInputStream(is); byte[] data = new byte[cc.dataSize - cc.signatureSize]; ds.read(data); boolean check = verifySignature(ds, signature, idfrom, data, pkey, MPK); if (check == false) { return "[IBC JAMES MAILET]: Failed to decrypt: maybe a letter was changed (Error during signature verification)".getBytes(); } is.reset(); ds.close(); DataInputStream din = new DataInputStream(is); Util.GetEncryptedKey(din, negative, cc, encryptedAESKey); int[] binaryAESKey = decryptKey(encryptedAESKey, SkID, MPK, this.aesKeyLen); byte[] raw = Util.BinaryToByteKey(binaryAESKey); din.close(); SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, skeySpec); byte[] encryptedData = new byte[cc.encryptedDataSize]; System.arraycopy(data, cc.firstKeySize + cc.secondKeySize + 12, encryptedData, 0, cc.encryptedDataSize); return cipher.doFinal(encryptedData); } } Số hóa trung tâm học liệu http://www.lrc-tnu.edu.vn/ 96 Số hóa trung tâm học liệu http://www.lrc-tnu.edu.vn/ ... mật mã khóa công khai Chương 2: Hệ mật mã dưa định danh Chương 3: Ứng dụng mã hóa dựa dịnh danh vào toán kiểm soát quyền truy cập hệ thống truy? ??n hình trả tiền Số hóa trung tâm học liệu http://www.lrc-tnu.edu.vn/... 2.2.3 Mã hóa dựa thuộc tính sách khóa (KP-ABE) Trong hệ thống CP- ABE sách truy cập xác định mã, hệ thống mã hóa dựa thuộc tính sách khóa (KPABE), sách truy cập định khóa riêng thuộc tính Mã hóa dựa. .. thực xây dựng hệ thống bảo mật 61 3.2 Ứng dụng IBE vào toán kiểm soát quyền truy cập hệ thống truy? ??n hình trả tiền 64 3.2.1 Mô tả toán 64 3.2.2 Thiết kế hệ thống

Ngày đăng: 25/03/2021, 00:24

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

w