SlideShare a Scribd company logo
1 of 18
Lập trình mạng – Chương 8 1
BẢO MẬT DỮ LIỆU
TRUYỀN TRÊN MẠNG
1.1 Các vấn đề về bảo mật dữ liệu
1.2 Các phương pháp mã hóa dữ liệu
1.3 Ví dụ mã hóa trong Java.
Lập trình mạng – Chương 8 2
1.1 Các vấn đề về bảo mật dữ liệu
• Secrecy: Thông tin truyền đi chỉ được phần tử gởi và nhận đọc mà
thôi.
• Authentication: Xác định đúng đối tác trước khi thực hiện một giao
dịch trên mạng.
• Non-repudiation: Chứng minh được chính đối tác đã gởi thông tin.
• Integrity control: Thông tin truyền đảm bảo không bị thêm bớt trong
quá trình truyền
Lập trình mạng – Chương 8 3
1.1 Các vấn đề về bảo mật dữ liệu
• Các nguy cơ tính bảo mật bị đe dọa:
• Thông tin bị đánh cắp:
• NIC card có thể lấy các thông tin không phải của máy mình
A
B
C
src:B dest:A payload
Lập trình mạng – Chương 8 4
1.1 Các vấn đề về bảo mật dữ liệu
• Các nguy cơ tính bảo mật bị đe dọa(tt):
• Thông tin giả mạo
• Chương trình giả mạo phần tử gởi để gởi cho phần tử nhận ( C giả mạo B gởi cho A)
A
B
C
src:B dest:A payload
Lập trình mạng – Chương 8 5
1.1 Các vấn đề về bảo mật dữ liệu
• Nguy cơ tấn công DOS (Denial of service)
• Tấn công bằng cách gởi nhiều message để làm tê liệt hệ thống
A
B
C
SYN
SYNSYNSYN
SYN
SYN
SYN
Lập trình mạng – Chương 8 6
1.2 Các phương pháp mã hóa dữ liệu
• Bảo mật bằng cách mã hóa (cryptography) dữ liệu trước khi truyền.
• Các phương pháp mã hóa:
• Mã hóa đối xứng, giải thuật bí mật (private key)
• Giải thuật khóa công khai (public key)
Lập trình mạng – Chương 8 7
1.2 Các phương pháp mã hóa
Lập trình mạng – Chương 8 8
1.2 Các phương pháp mã hóa
• Các vấn đề cần quan tâm trong hiện thực mã hoá:
• Giải thuật mã hóa (cryptography algorithm)
• Giao thức mã hóa (cryptography protocol)
• Quản lý khóa (key management)
Lập trình mạng – Chương 8 9
1.2 Các phương pháp mã hóa
• Các giải thuật mã hóa đơn giản:
• Thay thế một ký tự bằng một ký tự hay nhóm ký tự khác:
• Ví dụ:
• Hoán vị thứ tự các ký tự theo một quy tắc
plaintext: abcdefghijklmnopqrstuvwxyz
ciphertext: mnbvcxzasdfghjklpoiuytrewq
Plaintext: bob. i love you. alice
ciphertext: nkn. s gktc wky. mgsbc
Lập trình mạng – Chương 8 10
1.2 Các phương pháp mã hóa
• Phương pháp mã hóa khoá bí mật:
• Giải thuật DES: Data Encryption Standard
• US encryption standard [NIST 1993]
• 56-bit symmetric key, 64 bit plaintext input
Lập trình mạng – Chương 8 11
1.2 Các phương pháp mã hóa
• Phương pháp mã hóa khóa công khai (public key):
Lập trình mạng – Chương 8 12
1.2 Các phương pháp mã hóa
• Giải thuật RSA (Rivest, Shamir, Adelson) cho giải thuật mã hóa khóa
công khai
• Chọn hai số nguyên tố p,q liền nhau
• Tính n = p.q, z = (p-1)(q-1)
• Chọn e<n là không có thừa số chung với z
• Chọn d sao cho ed mod z = 1
• Khóa công khai là (n,e). Khoá bí mật là (n,d).
Lập trình mạng – Chương 8 13
1.2 Các phương pháp mã hóa
• Giải thuật RSA(tt)
• Với mẫu bit m muốn gởi, tính c = me mod n và gởi c
• Bên nhận nhận được mẫu bit c, tính ra
m = cd mod n
m = (m mod n)e mod n
d
Lập trình mạng – Chương 8 14
4
Ñoái
Taùc
1
Ñoái
Taùc
2
ÑT1gôûi public-keychoÑT2
ÑT2gôûi public-keychoÑT1
ÑT1gôûi döõlieäumaõhoùachoÑT2
vaødöõlieäu kieåm tratínhtoaønveïn
ÑT2gôûi döõlieäu maõhoaùchoÑT1
vaødöõlieäu kieåmtratínhtoaønveïn
1
2
3
5
6
8
7
Ví dụ giao thức mã hoá public key
Lập trình mạng – Chương 8 15
1.3 Ví dụ mã hóa trong Java.
1. import java.security.*; import java.security.spec.*; import java.security.interfaces.*;
2. import javax.crypto.*; import javax.crypto.spec.*;
3. import javax.crypto.interfaces.*; import com.sun.crypto.provider.SunJCE;
4. //…
5. SunJCE jce = new SunJCE();
6. Security.addProvider(jce);
7. System.out.println("BANK SERVER receives Client's public key");
8. byte[] clientPubKeyEnc = SocketUtil.receive(in);
9. KeyFactory serverKeyFac = KeyFactory.getInstance("DH");
10. X509EncodedKeySpec x509KeySpec =
11. new X509EncodedKeySpec(clientPubKeyEnc);
12. PublicKey clientPubKey = serverKeyFac.generatePublic(x509KeySpec);
13. DHParameterSpec dhParamSpec = ((DHPublicKey)clientPubKey).getParams();
14. System.out.println("SERVER: Generate DH keypair with Client ...");
15. KeyPairGenerator serverKeyPairGen = KeyPairGenerator.getInstance("DH");
16. serverKeyPairGen.initialize(dhParamSpec);
17. KeyPair serverKeyPair = serverKeyPairGen.generateKeyPair();
Lập trình mạng – Chương 8 16
1.3 Ví dụ mã hóa trong Java.
18. System.out.println("BANK SERVER: Initialize KeyAgreement with Client...");
19. KeyAgreement serverKeyAgree = KeyAgreement.getInstance("DH");
20. serverKeyAgree.init(serverKeyPair.getPrivate());
21. byte[] serverPubKeyEnc = serverKeyPair.getPublic().getEncoded();
22. SocketUtil.send(out,serverPubKeyEnc);
23.
24. serverKeyAgree.doPhase(clientPubKey, true);
25. byte[] serverSharedSecret = serverKeyAgree.generateSecret();
26. System.out.println("BANK SERVER's Secret-Key with WebSiteClient : "+
27. CipherUtil.toHexString(serverSharedSecret));
28.
29. serverKeyAgree.doPhase(clientPubKey, true);
30. SecretKey serverDesKey = serverKeyAgree.generateSecret("DES");
31. Cipher serverCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
32. Mac serverMac = Mac.getInstance("HmacSHA1");
33. serverMac.init(serverDesKey);
Lập trình mạng – Chương 8 17
1.3 Ví dụ mã hóa trong Java.
34. //Bank Server receives encrypted-data and Mac-code from Client
35. byte[] cipherTextBefore = SocketUtil.receive(in);
36. byte[] textMacBefore = SocketUtil.receive(in);
37.
38. //Bank Server decrypts encrypted-data and
39. //computes received-data's Mac-code
40. serverCipher.init(Cipher.DECRYPT_MODE, serverDesKey);
41. byte[] recoveredText = serverCipher.doFinal(cipherTextBefore);
42. byte[] textMacBeforePre = serverMac.doFinal(recoveredText);
43. //Bank Server process data
44. BankProcess bankPro;
45. if(java.util.Arrays.equals(textMacBefore, textMacBeforePre))
46. {
47. System.out.println("Data received from WebSiteClient is valid");
48. bankPro = new BankProcess(recoveredText,false);//no have error
49. }
Lập trình mạng – Chương 8 18
1.3 Ví dụ mã hóa trong Java.
50. else{
51. System.out.println("Data received is NOT valid");
52. }
53. byte[] result = bankPro.getResult();
54.
55. //Server encrypts result and computes its Mac-code
56. serverCipher.init(Cipher.ENCRYPT_MODE, serverDesKey);
57. byte[] cipherTextAfter = serverCipher.doFinal(result);
58. byte[] textMacAfter = serverMac.doFinal(result);
59.
60. //Server sends encrypted-result and Mac-code to client
61. SocketUtil.send(out,cipherTextAfter);
62. SocketUtil.send(out,textMacAfter);
63. //…
Dùng các packages JCE1.2.1 của Java

More Related Content

What's hot

Lap trinhcosodulieuvoi c-sharp_phan-2
Lap trinhcosodulieuvoi c-sharp_phan-2Lap trinhcosodulieuvoi c-sharp_phan-2
Lap trinhcosodulieuvoi c-sharp_phan-2
Hiển Phùng
 
4.thuc thi menh lenh voi co so du lieu
4.thuc thi menh lenh voi co so du lieu4.thuc thi menh lenh voi co so du lieu
4.thuc thi menh lenh voi co so du lieu
Dao Uit
 
Thuc thi menh lenh voi co so du lieu
Thuc thi menh lenh voi co so du lieuThuc thi menh lenh voi co so du lieu
Thuc thi menh lenh voi co so du lieu
truong le hung
 
Cach su dung databinding
Cach su dung databindingCach su dung databinding
Cach su dung databinding
truong le hung
 

What's hot (19)

itlchn 20 - Kien truc he thong chung khoan - Phan 2
itlchn 20 - Kien truc he thong chung khoan - Phan 2itlchn 20 - Kien truc he thong chung khoan - Phan 2
itlchn 20 - Kien truc he thong chung khoan - Phan 2
 
ITLC HN 14 - Bizweb Microservices Architecture
ITLC HN 14  - Bizweb Microservices ArchitectureITLC HN 14  - Bizweb Microservices Architecture
ITLC HN 14 - Bizweb Microservices Architecture
 
Kinh nghiệm triển khai Microservices tại Sapo.vn
Kinh nghiệm triển khai Microservices tại Sapo.vnKinh nghiệm triển khai Microservices tại Sapo.vn
Kinh nghiệm triển khai Microservices tại Sapo.vn
 
Laptrinh jdbc
Laptrinh jdbcLaptrinh jdbc
Laptrinh jdbc
 
Lap trinhcosodulieuvoi c-sharp_phan-2
Lap trinhcosodulieuvoi c-sharp_phan-2Lap trinhcosodulieuvoi c-sharp_phan-2
Lap trinhcosodulieuvoi c-sharp_phan-2
 
Vue.js
Vue.jsVue.js
Vue.js
 
Các bước kết nối csdl my sql với java
Các bước kết nối csdl my sql với javaCác bước kết nối csdl my sql với java
Các bước kết nối csdl my sql với java
 
4.thuc thi menh lenh voi co so du lieu
4.thuc thi menh lenh voi co so du lieu4.thuc thi menh lenh voi co so du lieu
4.thuc thi menh lenh voi co so du lieu
 
Technical note playframework_documentation_working with play - java_vn
Technical note playframework_documentation_working with play - java_vnTechnical note playframework_documentation_working with play - java_vn
Technical note playframework_documentation_working with play - java_vn
 
Postgresql các vấn đề thực tế
Postgresql các vấn đề thực tếPostgresql các vấn đề thực tế
Postgresql các vấn đề thực tế
 
Go micro framework to build microservices
Go micro framework to build microservicesGo micro framework to build microservices
Go micro framework to build microservices
 
Lớp kết nối csdl dùng jdbc trong java
Lớp kết nối csdl dùng jdbc trong javaLớp kết nối csdl dùng jdbc trong java
Lớp kết nối csdl dùng jdbc trong java
 
Thuc thi menh lenh voi co so du lieu
Thuc thi menh lenh voi co so du lieuThuc thi menh lenh voi co so du lieu
Thuc thi menh lenh voi co so du lieu
 
Cach su dung databinding
Cach su dung databindingCach su dung databinding
Cach su dung databinding
 
Slide bc
Slide bcSlide bc
Slide bc
 
Nodejs Introduction
Nodejs IntroductionNodejs Introduction
Nodejs Introduction
 
Bizweb Microservices Architecture
Bizweb Microservices ArchitectureBizweb Microservices Architecture
Bizweb Microservices Architecture
 
Web203 slide 5
Web203   slide 5Web203   slide 5
Web203 slide 5
 
Node.js căn bản
Node.js căn bảnNode.js căn bản
Node.js căn bản
 

Viewers also liked

Tieu luan nhom 3 slide
Tieu luan nhom 3   slideTieu luan nhom 3   slide
Tieu luan nhom 3 slide
laptrung
 
Slide bai giang_an_toan_va_bao_mat_thong_tin
Slide bai giang_an_toan_va_bao_mat_thong_tinSlide bai giang_an_toan_va_bao_mat_thong_tin
Slide bai giang_an_toan_va_bao_mat_thong_tin
Lang Codon
 
Giáo trình mật mã học công nghệ thông tin
Giáo trình mật mã học công nghệ thông tinGiáo trình mật mã học công nghệ thông tin
Giáo trình mật mã học công nghệ thông tin
https://www.facebook.com/garmentspace
 

Viewers also liked (7)

Java 8 New features
Java 8 New featuresJava 8 New features
Java 8 New features
 
Báo cáo tiểu luận hệ thống tên miền dns - học viện bưu chính viễn thông
Báo cáo tiểu luận   hệ thống tên miền dns - học viện bưu chính viễn thôngBáo cáo tiểu luận   hệ thống tên miền dns - học viện bưu chính viễn thông
Báo cáo tiểu luận hệ thống tên miền dns - học viện bưu chính viễn thông
 
Tieu luan nhom 3 slide
Tieu luan nhom 3   slideTieu luan nhom 3   slide
Tieu luan nhom 3 slide
 
Bai thao giang
Bai thao giangBai thao giang
Bai thao giang
 
Slide bai giang_an_toan_va_bao_mat_thong_tin
Slide bai giang_an_toan_va_bao_mat_thong_tinSlide bai giang_an_toan_va_bao_mat_thong_tin
Slide bai giang_an_toan_va_bao_mat_thong_tin
 
Bai 20-Mạng máy tính
Bai 20-Mạng máy tínhBai 20-Mạng máy tính
Bai 20-Mạng máy tính
 
Giáo trình mật mã học công nghệ thông tin
Giáo trình mật mã học công nghệ thông tinGiáo trình mật mã học công nghệ thông tin
Giáo trình mật mã học công nghệ thông tin
 

Similar to Bảo mật dữ liệu

Security Bootcamp 2013 penetration testing (basic)
Security Bootcamp 2013   penetration testing (basic)Security Bootcamp 2013   penetration testing (basic)
Security Bootcamp 2013 penetration testing (basic)
Security Bootcamp
 
Introduction ns2
Introduction ns2Introduction ns2
Introduction ns2
Tien Quan
 
Ssl report
Ssl reportSsl report
Ssl report
DiodeZ
 
Lab security+ Bài 8: Netcat
Lab security+ Bài 8: NetcatLab security+ Bài 8: Netcat
Lab security+ Bài 8: Netcat
xeroxk
 
Lab security+baiso8 netcat
Lab security+baiso8 netcatLab security+baiso8 netcat
Lab security+baiso8 netcat
xeroxk
 
Slide_Chữ ký điện tử của chaum van antwerpen
Slide_Chữ ký điện tử của chaum van antwerpenSlide_Chữ ký điện tử của chaum van antwerpen
Slide_Chữ ký điện tử của chaum van antwerpen
Tai Tran
 

Similar to Bảo mật dữ liệu (20)

Dsd05 01-rpca
Dsd05 01-rpcaDsd05 01-rpca
Dsd05 01-rpca
 
Python Beginner Class day-15-networking
Python Beginner Class day-15-networkingPython Beginner Class day-15-networking
Python Beginner Class day-15-networking
 
file_1_mot_so_ky_thuat_tan_cong_mang_pptx_0046.pdf
file_1_mot_so_ky_thuat_tan_cong_mang_pptx_0046.pdffile_1_mot_so_ky_thuat_tan_cong_mang_pptx_0046.pdf
file_1_mot_so_ky_thuat_tan_cong_mang_pptx_0046.pdf
 
Cain & abel
Cain & abelCain & abel
Cain & abel
 
Modern stream cipher
Modern stream cipherModern stream cipher
Modern stream cipher
 
Security Bootcamp 2013 penetration testing (basic)
Security Bootcamp 2013   penetration testing (basic)Security Bootcamp 2013   penetration testing (basic)
Security Bootcamp 2013 penetration testing (basic)
 
Introduction ns2
Introduction ns2Introduction ns2
Introduction ns2
 
Lt socket
Lt socketLt socket
Lt socket
 
Ipsec_Vietnamese
Ipsec_VietnameseIpsec_Vietnamese
Ipsec_Vietnamese
 
Mã hóa: hash
Mã hóa: hashMã hóa: hash
Mã hóa: hash
 
Ma hoa.pdf
Ma hoa.pdfMa hoa.pdf
Ma hoa.pdf
 
Report password cracking (2)
Report password cracking (2)Report password cracking (2)
Report password cracking (2)
 
Chuyên đề về hàm băm nguyên cứu hash fution
Chuyên đề về hàm băm nguyên cứu hash futionChuyên đề về hàm băm nguyên cứu hash fution
Chuyên đề về hàm băm nguyên cứu hash fution
 
Ssl report
Ssl reportSsl report
Ssl report
 
Lab security+ Bài 8: Netcat
Lab security+ Bài 8: NetcatLab security+ Bài 8: Netcat
Lab security+ Bài 8: Netcat
 
Lab security+baiso8 netcat
Lab security+baiso8 netcatLab security+baiso8 netcat
Lab security+baiso8 netcat
 
IT005 - Chương 4.pdf
IT005 - Chương 4.pdfIT005 - Chương 4.pdf
IT005 - Chương 4.pdf
 
ATTT.pptx
ATTT.pptxATTT.pptx
ATTT.pptx
 
Bai giang-Lap trinh he thong-Ch2-Lap trinh Socket.pdf
Bai giang-Lap trinh he thong-Ch2-Lap trinh Socket.pdfBai giang-Lap trinh he thong-Ch2-Lap trinh Socket.pdf
Bai giang-Lap trinh he thong-Ch2-Lap trinh Socket.pdf
 
Slide_Chữ ký điện tử của chaum van antwerpen
Slide_Chữ ký điện tử của chaum van antwerpenSlide_Chữ ký điện tử của chaum van antwerpen
Slide_Chữ ký điện tử của chaum van antwerpen
 

More from Son Nguyen

More from Son Nguyen (20)

Wsdl connector introduction
Wsdl connector introductionWsdl connector introduction
Wsdl connector introduction
 
Android intergrate with mule
Android intergrate with muleAndroid intergrate with mule
Android intergrate with mule
 
Mule flow overview
Mule flow overviewMule flow overview
Mule flow overview
 
Mule flow and filter
Mule flow and filterMule flow and filter
Mule flow and filter
 
Handle exceptions in mule
Handle exceptions in muleHandle exceptions in mule
Handle exceptions in mule
 
Spring security integrate with mule
Spring security integrate with muleSpring security integrate with mule
Spring security integrate with mule
 
Message processor in mule
Message processor in muleMessage processor in mule
Message processor in mule
 
Expression language in mule
Expression language in muleExpression language in mule
Expression language in mule
 
Mule with data weave
Mule with data weaveMule with data weave
Mule with data weave
 
Using spring scheduler mule
Using spring scheduler muleUsing spring scheduler mule
Using spring scheduler mule
 
Composite source in bound and out-bound
Composite source in bound and out-boundComposite source in bound and out-bound
Composite source in bound and out-bound
 
Batch job processing
Batch job processingBatch job processing
Batch job processing
 
Using message enricher
Using message enricherUsing message enricher
Using message enricher
 
Finance connectors with mule
Finance connectors with muleFinance connectors with mule
Finance connectors with mule
 
Google drive connection
Google drive connectionGoogle drive connection
Google drive connection
 
Using properties in mule
Using properties in muleUsing properties in mule
Using properties in mule
 
Mule integrate with microsoft
Mule integrate with microsoftMule integrate with microsoft
Mule integrate with microsoft
 
Jms queue
Jms queueJms queue
Jms queue
 
Anypoint connectors
Anypoint connectorsAnypoint connectors
Anypoint connectors
 
Mule esb basic introduction
Mule esb basic introductionMule esb basic introduction
Mule esb basic introduction
 

Bảo mật dữ liệu

  • 1. Lập trình mạng – Chương 8 1 BẢO MẬT DỮ LIỆU TRUYỀN TRÊN MẠNG 1.1 Các vấn đề về bảo mật dữ liệu 1.2 Các phương pháp mã hóa dữ liệu 1.3 Ví dụ mã hóa trong Java.
  • 2. Lập trình mạng – Chương 8 2 1.1 Các vấn đề về bảo mật dữ liệu • Secrecy: Thông tin truyền đi chỉ được phần tử gởi và nhận đọc mà thôi. • Authentication: Xác định đúng đối tác trước khi thực hiện một giao dịch trên mạng. • Non-repudiation: Chứng minh được chính đối tác đã gởi thông tin. • Integrity control: Thông tin truyền đảm bảo không bị thêm bớt trong quá trình truyền
  • 3. Lập trình mạng – Chương 8 3 1.1 Các vấn đề về bảo mật dữ liệu • Các nguy cơ tính bảo mật bị đe dọa: • Thông tin bị đánh cắp: • NIC card có thể lấy các thông tin không phải của máy mình A B C src:B dest:A payload
  • 4. Lập trình mạng – Chương 8 4 1.1 Các vấn đề về bảo mật dữ liệu • Các nguy cơ tính bảo mật bị đe dọa(tt): • Thông tin giả mạo • Chương trình giả mạo phần tử gởi để gởi cho phần tử nhận ( C giả mạo B gởi cho A) A B C src:B dest:A payload
  • 5. Lập trình mạng – Chương 8 5 1.1 Các vấn đề về bảo mật dữ liệu • Nguy cơ tấn công DOS (Denial of service) • Tấn công bằng cách gởi nhiều message để làm tê liệt hệ thống A B C SYN SYNSYNSYN SYN SYN SYN
  • 6. Lập trình mạng – Chương 8 6 1.2 Các phương pháp mã hóa dữ liệu • Bảo mật bằng cách mã hóa (cryptography) dữ liệu trước khi truyền. • Các phương pháp mã hóa: • Mã hóa đối xứng, giải thuật bí mật (private key) • Giải thuật khóa công khai (public key)
  • 7. Lập trình mạng – Chương 8 7 1.2 Các phương pháp mã hóa
  • 8. Lập trình mạng – Chương 8 8 1.2 Các phương pháp mã hóa • Các vấn đề cần quan tâm trong hiện thực mã hoá: • Giải thuật mã hóa (cryptography algorithm) • Giao thức mã hóa (cryptography protocol) • Quản lý khóa (key management)
  • 9. Lập trình mạng – Chương 8 9 1.2 Các phương pháp mã hóa • Các giải thuật mã hóa đơn giản: • Thay thế một ký tự bằng một ký tự hay nhóm ký tự khác: • Ví dụ: • Hoán vị thứ tự các ký tự theo một quy tắc plaintext: abcdefghijklmnopqrstuvwxyz ciphertext: mnbvcxzasdfghjklpoiuytrewq Plaintext: bob. i love you. alice ciphertext: nkn. s gktc wky. mgsbc
  • 10. Lập trình mạng – Chương 8 10 1.2 Các phương pháp mã hóa • Phương pháp mã hóa khoá bí mật: • Giải thuật DES: Data Encryption Standard • US encryption standard [NIST 1993] • 56-bit symmetric key, 64 bit plaintext input
  • 11. Lập trình mạng – Chương 8 11 1.2 Các phương pháp mã hóa • Phương pháp mã hóa khóa công khai (public key):
  • 12. Lập trình mạng – Chương 8 12 1.2 Các phương pháp mã hóa • Giải thuật RSA (Rivest, Shamir, Adelson) cho giải thuật mã hóa khóa công khai • Chọn hai số nguyên tố p,q liền nhau • Tính n = p.q, z = (p-1)(q-1) • Chọn e<n là không có thừa số chung với z • Chọn d sao cho ed mod z = 1 • Khóa công khai là (n,e). Khoá bí mật là (n,d).
  • 13. Lập trình mạng – Chương 8 13 1.2 Các phương pháp mã hóa • Giải thuật RSA(tt) • Với mẫu bit m muốn gởi, tính c = me mod n và gởi c • Bên nhận nhận được mẫu bit c, tính ra m = cd mod n m = (m mod n)e mod n d
  • 14. Lập trình mạng – Chương 8 14 4 Ñoái Taùc 1 Ñoái Taùc 2 ÑT1gôûi public-keychoÑT2 ÑT2gôûi public-keychoÑT1 ÑT1gôûi döõlieäumaõhoùachoÑT2 vaødöõlieäu kieåm tratínhtoaønveïn ÑT2gôûi döõlieäu maõhoaùchoÑT1 vaødöõlieäu kieåmtratínhtoaønveïn 1 2 3 5 6 8 7 Ví dụ giao thức mã hoá public key
  • 15. Lập trình mạng – Chương 8 15 1.3 Ví dụ mã hóa trong Java. 1. import java.security.*; import java.security.spec.*; import java.security.interfaces.*; 2. import javax.crypto.*; import javax.crypto.spec.*; 3. import javax.crypto.interfaces.*; import com.sun.crypto.provider.SunJCE; 4. //… 5. SunJCE jce = new SunJCE(); 6. Security.addProvider(jce); 7. System.out.println("BANK SERVER receives Client's public key"); 8. byte[] clientPubKeyEnc = SocketUtil.receive(in); 9. KeyFactory serverKeyFac = KeyFactory.getInstance("DH"); 10. X509EncodedKeySpec x509KeySpec = 11. new X509EncodedKeySpec(clientPubKeyEnc); 12. PublicKey clientPubKey = serverKeyFac.generatePublic(x509KeySpec); 13. DHParameterSpec dhParamSpec = ((DHPublicKey)clientPubKey).getParams(); 14. System.out.println("SERVER: Generate DH keypair with Client ..."); 15. KeyPairGenerator serverKeyPairGen = KeyPairGenerator.getInstance("DH"); 16. serverKeyPairGen.initialize(dhParamSpec); 17. KeyPair serverKeyPair = serverKeyPairGen.generateKeyPair();
  • 16. Lập trình mạng – Chương 8 16 1.3 Ví dụ mã hóa trong Java. 18. System.out.println("BANK SERVER: Initialize KeyAgreement with Client..."); 19. KeyAgreement serverKeyAgree = KeyAgreement.getInstance("DH"); 20. serverKeyAgree.init(serverKeyPair.getPrivate()); 21. byte[] serverPubKeyEnc = serverKeyPair.getPublic().getEncoded(); 22. SocketUtil.send(out,serverPubKeyEnc); 23. 24. serverKeyAgree.doPhase(clientPubKey, true); 25. byte[] serverSharedSecret = serverKeyAgree.generateSecret(); 26. System.out.println("BANK SERVER's Secret-Key with WebSiteClient : "+ 27. CipherUtil.toHexString(serverSharedSecret)); 28. 29. serverKeyAgree.doPhase(clientPubKey, true); 30. SecretKey serverDesKey = serverKeyAgree.generateSecret("DES"); 31. Cipher serverCipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); 32. Mac serverMac = Mac.getInstance("HmacSHA1"); 33. serverMac.init(serverDesKey);
  • 17. Lập trình mạng – Chương 8 17 1.3 Ví dụ mã hóa trong Java. 34. //Bank Server receives encrypted-data and Mac-code from Client 35. byte[] cipherTextBefore = SocketUtil.receive(in); 36. byte[] textMacBefore = SocketUtil.receive(in); 37. 38. //Bank Server decrypts encrypted-data and 39. //computes received-data's Mac-code 40. serverCipher.init(Cipher.DECRYPT_MODE, serverDesKey); 41. byte[] recoveredText = serverCipher.doFinal(cipherTextBefore); 42. byte[] textMacBeforePre = serverMac.doFinal(recoveredText); 43. //Bank Server process data 44. BankProcess bankPro; 45. if(java.util.Arrays.equals(textMacBefore, textMacBeforePre)) 46. { 47. System.out.println("Data received from WebSiteClient is valid"); 48. bankPro = new BankProcess(recoveredText,false);//no have error 49. }
  • 18. Lập trình mạng – Chương 8 18 1.3 Ví dụ mã hóa trong Java. 50. else{ 51. System.out.println("Data received is NOT valid"); 52. } 53. byte[] result = bankPro.getResult(); 54. 55. //Server encrypts result and computes its Mac-code 56. serverCipher.init(Cipher.ENCRYPT_MODE, serverDesKey); 57. byte[] cipherTextAfter = serverCipher.doFinal(result); 58. byte[] textMacAfter = serverMac.doFinal(result); 59. 60. //Server sends encrypted-result and Mac-code to client 61. SocketUtil.send(out,cipherTextAfter); 62. SocketUtil.send(out,textMacAfter); 63. //… Dùng các packages JCE1.2.1 của Java