Last active
December 1, 2017 09:59
-
-
Save minhphong306/fba182c38c85d53946956268d8d79d51 to your computer and use it in GitHub Desktop.
Class main RSA
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * To change this license header, choose License Headers in Project Properties. | |
| * To change this template file, choose Tools | Templates | |
| * and open the template in the editor. | |
| */ | |
| package onthi; | |
| import java.security.PrivateKey; | |
| import java.security.PublicKey; | |
| /** | |
| * | |
| * @author Linh | |
| */ | |
| public class RSAMain { | |
| public static void main(String[] args) throws Exception { | |
| String duongdan_congkhai = "D:\\rsa\\congkhai.txt"; | |
| String duongdan_bimat = "D:\\rsa\\bimat.txt"; | |
| String duongdan_banma = "D:\\rsa\\banma.txt"; | |
| String duongdan_ban_giaima = "D:\\rsa\\bangiaima.txt"; | |
| String chuoi_goc = "minhphong306 - Make Things Simple"; | |
| // Ý 1, 2: Sinh khóa, lưu khóa vào file | |
| SinhKhoaRSA sinhKhoa = new SinhKhoaRSA(512); | |
| // Lấy ra khóa công khai, bí mật | |
| PublicKey khoaCK = sinhKhoa.getPub(); | |
| PrivateKey khoaBM = sinhKhoa.getPri(); | |
| // Lưu vào file | |
| sinhKhoa.ghifile(duongdan_bimat, khoaBM.getEncoded()); | |
| sinhKhoa.ghifile(duongdan_congkhai, khoaCK.getEncoded()); | |
| // Ý 3: Đọc file để nạp khóa bí mật | |
| byte[] bytes_bimat = sinhKhoa.docfile(duongdan_bimat); | |
| PrivateKey khoaBM_doc = sinhKhoa.sinhKhoaBiMat(bytes_bimat); | |
| // Ý 4: Đọc file để nạp khóa công khai | |
| byte[] bytes_congkhai = sinhKhoa.docfile(duongdan_congkhai); | |
| PublicKey khoaCK_doc = sinhKhoa.sinhKhoaCongKhai(bytes_congkhai); | |
| // Ý 5, 6: hàm mã hóa và giải mã thông điệp nằm ở class MaHoaRSA | |
| // Ý 7: Băm chuỗi gốc và mã hóa chuỗi gốc, lưu cả vào 1 file | |
| MaHoaRSA RSA = new MaHoaRSA(); | |
| String chuoi_bam = RSA.bamMD5(chuoi_goc); | |
| String chuoi_mahoa = RSA.maHoa(chuoi_goc, khoaBM_doc); | |
| // Nối 2 chuỗi lại, cách nhau bởi chữ split_here | |
| String chuoi_bamVaMaHoa = chuoi_bam + "split_here" + chuoi_mahoa; | |
| // Lưu cả vào 1 file | |
| sinhKhoa.ghifile(duongdan_banma, chuoi_bamVaMaHoa.getBytes()); | |
| // Ý 8: Đọc chuỗi được lưu từ file, tách ra bản mã và bản băm. | |
| // Giải mã bản mã và băm lại, so sánh với bản băm ban đầu | |
| byte[] bytes_banma = sinhKhoa.docfile(duongdan_banma); | |
| String chuoi_bamVaMaHoa_doc = new String(bytes_banma); | |
| System.out.println(chuoi_bamVaMaHoa_doc); | |
| String chuoi_bam_tach = chuoi_bamVaMaHoa_doc.split("split_here")[0]; | |
| String chuoi_mahoa_tach = chuoi_bamVaMaHoa_doc.split("split_here")[1]; | |
| String chuoi_giaima = RSA.giaiMa(chuoi_mahoa_tach, khoaCK_doc); | |
| System.out.println(chuoi_giaima); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment