Chương trình thử nghiệm

Một phần của tài liệu 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 (Trang 75 - 103)

Bước đầu tiên trong quá trình ứng dụng kiểm soát quyền truy cập trong hệ thống truyền hình trả tiền là giải quyết bài toán mã hóa và giải mã:

Số hóa bởi trung tâm học liệu http://www.lrc-tnu.edu.vn/

Khi nhập mã định danh ví dụ như email vananh@hubt.edu.vn, ta có kết quả mã hóa chương trình như sau:

Số hóa bởi trung tâm học liệu http://www.lrc-tnu.edu.vn/

KẾT LUẬN

Trong sự phát triển mạnh mẽ của công nghệ thông tin, cùng với nhu cầu cấp thiết về việc bảo vệ an ninh thông tin nói chung và bảo vệ thông tin cá nhân nói riêng, việc xây dựng các ứng dụng để bảo mật và kiểm soát trên hệ thống là vô cùng quan trọng.

Trong khuôn khổ của một luận văn thạc sĩ, tác giả đã giải quyết được mục tiêu là tìm hiểu mã hóa dựa trên định danh, các thuật toán cài đặt và ứng dụng IBE 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”, bao gồm:

-

- Nghiên cứu hệ mã hóa dựa trên định danh IBE và các ưu điểm của hệ mã hóa này so với hệ mã hóa công khai truyền thống.

- Tìm hiểu thuật toán mã hoá định danh IBE và xây dựng chương trình cho bài toán kiểm soát quyền truy cập trong hệ thống truyền hình trả tiền.

- Các kết quả cho thấy việc ứng dụng mã hoá IBE cho một số bài toán đơn giản hơn nhiều về cài đặt và ít tốn kém hơn về nguồn lực để hỗ trợ.

Tuy nhiên, do thời gian nghiên cứu và năng lực bản thân có hạn, tác giả mới chỉ xây dựng được chương trình demo quá trình mã hóa dữ liệu và giải mã dữ liệu. Hệ thống của tác giả đã sinh ra được khóa chung khóa riêng để sử dụng cho quá trình xác thực, bảo mật dữ liệu tránh mất thông tin tài khoản trong quá trình đăng nhập để sử dụng dịch vụ truyền hình trả tiền.

Định hướng nghiên cứu và phát triển tiếp theo:

Trong thời gian tới, tác giả sẽ tìm hiểu sâu thêm về các kỹ thuật, các giải pháp bảo mật dữ liệu khi truyền trên mạng, bảo mật các thông tin trên hệ thống: danh bạ, tài liệu cá nhân, tranh ảnh…. Bên cạnh đó, tác giả sẽ nghiên cứu thêm các ứng dụng của mã hóa IBE trên các hệ thống

Số hóa bởi trung tâm học liệu http://www.lrc-tnu.edu.vn/

TÀI LIỆU THAM KHẢO I. Tiếng Việt:

[1] Trần Duy Lai, "Mã hóa dựa trên định danh", Tạp chí An toàn thông tin số 3/2009 - ISSN 1859-1256

[2] Trần Duy Lai, "Mã hóa dựa trên định danh", Tạp chí An toàn thông tin số 4/2009 - ISSN 1859-1256

[3] Phạm Quốc Hoàng "Mã hóa dựa trên thuộc tính" Tạp chí An toàn thông tin số 2/2010.- ISSN 1859-1256

[4] Phạm Văn Hiếu, Phạm Thu Hằng dịch của Simon Singh “Mật mã- từ cổ điển đến lượng tử” Nhà xuất bản trẻ

II. Tiếng Anh:

[5] Identity-based cryptosystems and signature schemes

Adi Shamir,Department of Applied Mathematics,The Weizmann Institute of Science, Rehovot, 76100 Israel.

[6] J. Bethencourt, A. Sahai, B. Waters. Ciphertext Policy Attribute - Base Encryption. SP'07. Washington DC, USA: IEEE Computer Society, 2007. [7]. Jin Li, Kui Ren, Kwangjo Kim. Accountable Attribute Based Encyption

for Abuse Free Access Control. Cryptology ePrint Archive, Report 2009/118, 2009 http://eprint.iacr.org

[8]. Whitten, A., and J.Tygar, Why Jonny Can't Encrypt: A ussability Evalution of PGP 5.0, Proceedings of the 8th USENIX Security Symposium, Washington DC, August 23-26 1999, pp. 169-184.

Số hóa bởi trung tâm học liệu http://www.lrc-tnu.edu.vn/

Phụ lục chƣơng trình

1. Code chƣơng trình viết bằng ngôn ngữ Java trên máy chủ

File main.java package org.suai.idbased; import java.io.DataOutputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.math.BigInteger; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.util.logging.Level; import java.util.logging.Logger; import javax.crypto.BadPaddingException; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import org.suai.idbased.Main.Arguments.commands; import org.suai.idbased.util.*; import org.suai.idbased.pkg.*; import org.suai.idbased.crypto.*; import org.suai.idbased.keymng.*;

publicclass Main {

privatestatic FileOutputStream mpkf; privatestatic FileOutputStream mskf; privatestatic FileOutputStream default_mpkf; privatestatic FileOutputStream default_mskf; privatestatic DataOutputStream dmpkf; privatestatic DataOutputStream default_dmpkf; privatestatic DataOutputStream dmskf; privatestatic DataOutputStream default_dmskf; privatestatic FileOutputStream mskf2; privatestatic FileOutputStream mskf1; privatestatic DataOutputStream dmskf1; privatestatic DataOutputStream dmskf2; privatestatic FileOutputStream default_mskf1; privatestatic DataOutputStream default_dmskf1; privatestatic FileOutputStream default_mskf2; privatestatic DataOutputStream default_dmskf2; privatestatic FileOutputStream skef;

privatestatic DataOutputStream dskef; privatestatic FileOutputStream sksf;

Số hóa bởi trung tâm học liệu http://www.lrc-tnu.edu.vn/

publicstaticclass Arguments {

public String[] args = null; public String keystoragePath = null; public String mpk_path = null; public String msk_path1 = null; public String msk_path2 = null; public String id = null; public String sk_path = null; public String in_path = null; public String out_path = null; public String sks_path = null; public String keyStorage = null; public String password = null; publicint security = 0; private String sender = null; private String recip = null; private String domain = null; public enum commands {

setup, keyextract, encrypt, decrypt, help, notfound; }

commands command_type;

public Arguments(String[] args) {

this.args = args; }

public commands getCommand() {

if (args.length == 0) {

this.command_type = commands.notfound; return commands.notfound;

}

if (args[0].equals("setup")) {

this.command_type = commands.setup; }

elseif (args[0].equals("keyextract")) {

this.command_type = commands.keyextract; }

elseif (args[0].equals("encrypt")) {

Số hóa bởi trung tâm học liệu http://www.lrc-tnu.edu.vn/

}

elseif (args[0].equals("decrypt")) {

this.command_type = commands.decrypt; }

elseif (args[0].equals("help")) {

this.command_type = commands.help; }

else

{

this.command_type = commands.notfound; }

returnthis.command_type; }

privatevoid verifyCorrect(int idx) { if (idx >= args.length) { Util.invalidParameters(); System.exit(0); } }

publicvoid extractArgs() {

for (int i = 1; i < args.length; i++) {

if (args[i].equals("-mpk")) {

verifyCorrect(i + 1); this.mpk_path = args[i + 1];

}

elseif (args[i].equals("-keystorage")) {

verifyCorrect(i + 1);

this.keystoragePath = args[i+1]; }

elseif (args[i].equals("-id")) {

verifyCorrect(i + 1); this.id = args[i + 1];

Số hóa bởi trung tâm học liệu http://www.lrc-tnu.edu.vn/

}

elseif (args[i].equals("-from")) {

verifyCorrect(i + 1); this.sender = args[i + 1];

}

elseif (args[i].equals("-to")) {

verifyCorrect(i + 1); this.recip = args[i + 1];

}

elseif (args[i].equals("-in")) {

verifyCorrect(i + 1); this.in_path = args[i + 1]; }

elseif (args[i].equals("-out")) {

verifyCorrect(i + 1); this.out_path = args[i + 1]; }

elseif (args[i].equals("-sk")) {

verifyCorrect(i + 1); this.sk_path = args[i + 1]; }

elseif (args[i].equals("-security")) {

verifyCorrect(i + 1);

this.security = Integer.parseInt(args[i + 1]); if (this.security <=0) { Util.invalidParameters(); System.exit(0); } } elseif (args[i].equals("-sks")) { verifyCorrect(i + 1); this.sks_path = args[i + 1]; }

elseif (args[i].equals("-password")) { verifyCorrect(i + 1);

Số hóa bởi trung tâm học liệu http://www.lrc-tnu.edu.vn/

}

elseif (args[i].equals("-domain")) { verifyCorrect(i + 1);

this.domain = args[i + 1]; }

} }

publicvoid verifyRequiredParameters() {

if (this.command_type == commands.setup && (this.security == 0 || this.keystoragePath == null || this.password == null

|| this.mpk_path == null || this.domain == null)) {

Util.invalidParameters(); System.exit(0); }

if (this.command_type == commands.keyextract && (this.id == null || this.sk_path == null || this.sks_path == null ||

this.keystoragePath == null || this.password == null)) {

Util.invalidParameters(); System.exit(0); }

if (this.command_type == commands.encrypt && (this.recip == null || this.in_path == null || this.out_path == null ||

this.sks_path == null || this.mpk_path == null)) {

Util.invalidParameters(); System.exit(0); }

if (this.command_type == commands.decrypt && (this.sender == null || this.sk_path == null || this.in_path == null ||

this.out_path == null || this.mpk_path == null || this.recip == null)) { Util.invalidParameters(); System.exit(0); } } }

public static void main(String[] args) throws UnsupportedEncodingException, FileNotFoundException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IOException

{

//Client client = new Client();

Arguments argum = new Arguments(args); argum.getCommand();

if (argum.command_type == commands.notfound ) {

Util.usage(); System.exit(1);

Số hóa bởi trung tâm học liệu http://www.lrc-tnu.edu.vn/ } argum.extractArgs(); switch (argum.command_type) { casesetup: { argum.verifyRequiredParameters(); int security = argum.security;

System.out.print ("Private Key Generator initialization starting: "); PKG pkg = new PKG(security);

pkg.setup();

System.out.println ("Done.");

System.out.println ("Master Public Key length: " + argum.security*2 + " bit"); System.out.println ("Master Secret Key length: " + argum.security + " bit"); System.out.println ("Public signing exponent: " + pkg.getSigningPublicKey()); System.out.print ("Adding keydata into " + argum.keystoragePath + ": ");

KeyStorage ks = new KeyStorage (argum.keystoragePath); try { ks.addKey(argum.domain, pkg.getMPK().toByteArray(), pkg.getMSK1().toByteArray(), pkg.getMSK2().toByteArray(), argum.password); }

catch (IllegalBlockSizeException ex) {

Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);

}

catch (BadPaddingException ex) {

Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);

}

System.out.println ("Done.");

System.out.println ("Domain Name: " + argum.domain); System.out.println ("Master Public Key: " + pkg.getMPK()); FileOutputStream mpkf1 = new FileOutputStream(argum.mpk_path); mpkf1.write (pkg.getMPK().toByteArray());

mpkf1.close();

System.out.println ("MPK saved into " + argum.mpk_path); break;

}

casekeyextract: { //keyExtract

Số hóa bởi trung tâm học liệu http://www.lrc-tnu.edu.vn/

argum.verifyRequiredParameters(); PKG pkg = new PKG();

System.out.print ("Getting master keydata from: " + argum.keystoragePath +" : "); KeyStorage ks = new KeyStorage (argum.keystoragePath);

BigInteger[] keys = new BigInteger[3]; int res = 0;

try

{

res = ks.getKey(argum.domain, keys, argum.password); }

catch (IllegalBlockSizeException ex) {

Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);

}

if (res == 1) {

System.out.println ("OK."); pkg.init(keys[0], keys[1], keys[2]);

System.out.print ("Starting keyextract for: " + argum.id + " :"); BigInteger encrSecrKey = pkg.keyExtract(argum.id); BigInteger signSecKey = pkg.signKeyExtract(argum.id); FileOutputStream skf = new FileOutputStream (argum.sk_path); skf.write(encrSecrKey.toByteArray());

FileOutputStream sks = new FileOutputStream (argum.sks_path); sks.write(signSecKey.toByteArray());

skf.close(); sks.close();

System.out.println ("Done.");

} else

System.out.println ("keyextract failed"); break;

}

caseencrypt: {

PKG pkg = new PKG(); Client client = new Client(); argum.verifyRequiredParameters();

BigInteger MPK = Util.readKeyData(new FileInputStream(argum.mpk_path)); BigInteger signSecretKey = (Util.readKeyData(new FileInputStream(argum.sks_path))); System.out.print("Encrypting and signing " + argum.in_path +" to " + argum.recip + " : "); try

{

client.encryptData(argum.in_path, argum.out_path,

client.genPkID(argum.recip, MPK), MPK, signSecretKey, pkg.getSigningPublicKey());

Số hóa bởi trung tâm học liệu http://www.lrc-tnu.edu.vn/

}

catch (IllegalBlockSizeException ex) {

Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);

}

catch (BadPaddingException ex) {

Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);

}

System.out.println ("Done"); break;

}

casedecrypt: { //decrypt

PKG pkg = new PKG(); Client client = new Client(); argum.verifyRequiredParameters();

System.out.print("Decrypting and verify sign " + argum.in_path +" from " + argum.sender + " to: " + argum.recip + " : ");

BigInteger MPK = Util.readKeyData(new FileInputStream(argum.mpk_path)); BigInteger encrSecretKey = (Util.readKeyData(new FileInputStream(argum.sk_path))); byte[] decryptData = null;

try

{

decryptData = client.decryptData(argum.in_path, argum.out_path, argum.recip, argum.sender, encrSecretKey, MPK,

pkg.getSigningPublicKey()); }

catch (IllegalBlockSizeException ex) {

System.out.println ("Decryption error: key are illegal"); }

catch (BadPaddingException ex) {

System.out.println ("Decryption error: key are illegal"); }

if (decryptData == null) {

System.out.println ("Sign verification error"); }

else

System.out.println ("OK.");

break; }

Số hóa bởi trung tâm học liệu http://www.lrc-tnu.edu.vn/ casehelp: { Util.help(); System.exit(1); break; } default: { } } } }

Số hóa bởi trung tâm học liệu http://www.lrc-tnu.edu.vn/ File PKG.java package org.suai.idbased.pkg; import java.math.BigInteger; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Random; import org.suai.idbased.util.ResidueCalculation; publicclass PKG { private BigInteger MPK; private BigInteger P, Q; privateint security; privatelong e = 65537; private BigInteger d;

public PKG(int size) { security = size; } public PKG() { }

publicvoid init (BigInteger MPK, BigInteger P, BigInteger Q) { this.MPK = MPK;

this.P = P; this.Q = Q;

this.getSecretExponent(); }

public BigInteger getMPK () { returnthis.MPK; }

public BigInteger getMSK1 () { returnthis.P;

}

public BigInteger getMSK2() { returnthis.Q;

}

publiclong getSigningPublicKey () { returnthis.e;

}

publicvoid setup() {

Số hóa bởi trung tâm học liệu http://www.lrc-tnu.edu.vn/

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) != 0 && P.mod(four).compareTo(tree) == 0 && Q.mod( four).compareTo(tree) == 0 && signOpenExp.gcd(ResidueCalculation. euler(P, Q)).compareTo(BigInteger.ONE) == 0)

{ break; } } MPK = P.multiply(Q); this.getSecretExponent(); }

staticpublic 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); } elseif (j == 1 && k == 1) { return a; } else {

Số hóa bởi trung tâm học liệu http://www.lrc-tnu.edu.vn/

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;

}

privatevoid 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;

publicclass Util {

privatefinalstaticintaesKeyLen = 128;

Số hóa bởi trung tâm học liệu http://www.lrc-tnu.edu.vn/

{

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 < 8 - binary.length(); k++) { sb.append("0"); } } sb.append(binary); }

binaryKey = newbyte[sb.length()]; for (int i = 0; i < sb.length(); i++) {

binaryKey[i] = sb.charAt(i) == '0' ? (byte) 0 : (byte) 1; }

return binaryKey;

}

staticpublicvoid 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 = newbyte[sizeofEncrAESKeyBit]; din.read(buff);

encryptedAESKey[i] = new BigInteger(buff);

}

din.skipBytes(cc.secondKeySize); }

Số hóa bởi trung tâm học liệu http://www.lrc-tnu.edu.vn/

{

din.skipBytes(cc.firstKeySize); for (int i = 0; i < aesKeyLen; i++) {

sizeofEncrAESKeyBit = din.readInt(); byte[] buff = newbyte[sizeofEncrAESKeyBit]; din.read(buff);

encryptedAESKey[i] = new BigInteger(buff);

} } }

staticpublicbyte[] 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 = newbyte[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; }

staticpublicvoid 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 bởi trung tâm học liệu http://www.lrc-tnu.edu.vn/

"Print help to see the details information of the program usage"); }

staticpublicvoid 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 bởi trung tâm học liệu http://www.lrc-tnu.edu.vn/

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 e- mail adress' -mpk 'path-to-mpk'");

System.out.println("---");

}

staticpublicvoid invalidParameters() {

System.out.println(

"Entered incorrect settings or missing required parameters, type help for calling tips"); }

staticpublicbyte[] writeKeyData(BigInteger data) {

return data.toByteArray();

}

staticpublic BigInteger readKeyData(FileInputStream in) throws IOException {

byte[] byteKey = newbyte[in.available()]; in.read(byteKey);

returnnew BigInteger(byteKey);

} }

3.2.4. Chƣơng trình viết bằng ngôn ngữ Java trên máy client. File client.java

package org.suai.idbased.crypto;

import org.suai.idbased.util.Util;

import org.suai.idbased.util.ResidueCalculation;

Số hóa bởi trung tâm học liệu http://www.lrc-tnu.edu.vn/ 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;

publicclass Client {

privatefinalint aesKeyLen = 128;

privatefinalint 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 bởi trung tâm học liệu http://www.lrc-tnu.edu.vn/

}

publicvoid 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 == 0 && j == -1) || (m == 1 && 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; } } } }

Một phần của tài liệu 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 (Trang 75 - 103)