Vc ++ implements USB flash drive media encryption and decryption to ensure Storage Security

Source: Internet
Author: User

The core code is as follows:

# Include "StdAfx. h "<br/> # include" Function. h "</p> <p> CString GetDiskNumber (CString name) <br/>{< br/> HKEY hkey; <br/> char sz [256]; <br/> DWORD dwtype, sl = 256; <br/> int number = 0; </p> <p> // confirm the selected disk <br/> for (int I = 1; I <8; I ++) <br/>{< br/> if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, "SYSTEM // CurrentControlSet // Services // Disk // Enum",/<br/> NULL, KEY_ALL_ACCESS, & hkey) = ERROR_SUCCESS) <br/>{< br/> CString id; <br/> id. Format ("% d", I); <br/> if (RegQueryValueEx (hkey, id, NULL, & dwtype, (LPBYTE) sz, & sl) = ERROR_SUCCESS) <br/>{< br/> CString str = (CString) sz; <br/> if (str. compare (name) = 0) <br/>{< br/> number = I; <br/> break; <br/>}< br/> CString driver = "////. // PHYSICALDRIVE "; <br/> CString num; <br/> num. format ("% d", number); <br/> driver + = num; <br/> return driver; <br/>}</p> <p> int ReadDisk (CString driver, unsign Ed char * Buf, long addr) <br/>{< br/> HANDLE hDevice; <br/> BOOL bResult; <br/> DWORD bytesread; </p> <p> hDevice = CreateFile (driver, GENERIC_READ | GENERIC_WRITE, <br/> file_1__read | file_1__write, NULL, OPEN_EXISTING, 0, NULL ); </p> <p> if (hDevice = INVALID_HANDLE_VALUE) <br/>{< br/> AfxMessageBox ("Error! "); <Br/> return 0; <br/>}</p> <p> if (addr! = 0) <br/>{< br/> SetFilePointer (hDevice, 512 * addr, NULL, NULL ); <br/>}</p> <p> bResult = ReadFile (hDevice, Buf, 512, & bytesread, NULL ); </p> <p> if (bResult = FALSE) | (bytesread <512) <br/>{< br/> AfxMessageBox ("Error! "); <Br/> return 0; <br/>}</p> <p> CloseHandle (hDevice); </p> <p> return 1; <br/>}</p> <p> int WriteDisk (CString driver, unsigned char * Buf, long addr) <br/>{< br/> HANDLE hDevice; <br/> BOOL bResult; <br/> DWORD bytesread; </p> <p> hDevice = CreateFile (driver, GENERIC_READ | GENERIC_WRITE, <br/> file_pai_read | file_pai_write, NULL, OPEN_EXISTING, 0, NULL); </p> <p> if (hDevice = INVALID_HANDLE_VALUE) <br/>{< br/> AfxMessageBo X ("Error! "); <Br/> return 0; <br/>}</p> <p> if (addr! = 0) <br/>{< br/> SetFilePointer (hDevice, 512 * addr, NULL, NULL ); <br/>}</p> <p> bResult = WriteFile (hDevice, Buf, 512, & bytesread, NULL ); </p> <p> if (bResult = FALSE) | (bytesread <512) <br/>{< br/> AfxMessageBox ("Error! "); <Br/> return 0; <br/>}</p> <p> CloseHandle (hDevice); </p> <p> return 1; <br/>}</p> <p> void PopupUSBDevice () <br/> {<br/> char strSystemDirectory [256]; <br/> GetSystemDirectory (strSystemDirectory, 256); </p> <p> CString strTemp = strSystemDirectory; <br/> strTemp + = "// rundll32.exe shell32.dll, Control_RunDLL hotplug. dll "; </p> <p> WinExec (strTemp, SW_SHOW); <br/>}

The call code is as follows:

BOOL CMSSDlg: OnInitDialog () <br/>{< br/> CDialog: OnInitDialog (); </p> <p> // Add "About... "menu item to system menu. </p> <p> // IDM_ABOUTBOX must be in the system command range. <br/> ASSERT (IDM_ABOUTBOX & 0xFFF0) = IDM_ABOUTBOX); <br/> ASSERT (IDM_ABOUTBOX <0xF000 ); </p> <p> CMenu * pSysMenu = GetSystemMenu (FALSE); <br/> if (pSysMenu! = NULL) <br/>{< br/> CString strAboutMenu; <br/> strAboutMenu. LoadString (IDS_ABOUTBOX); <br/> if (! StrAboutMenu. isEmpty () <br/>{< br/> pSysMenu-> AppendMenu (MF_SEPARATOR); <br/> pSysMenu-> AppendMenu (MF_STRING, IDM_ABOUTBOX, strAboutMenu ); <br/>}</p> <p> // Set the icon for this dialog. the framework does this automatically <br/> // when the application's main window is not a dialog <br/> SetIcon (m_hIcon, TRUE ); // Set big icon <br/> SetIcon (m_hIcon, FALSE); // Set small icon </p> <p> // TODO: Add ext Ra initialization here <br/> HKEY hkey; <br/> char sz [256]; <br/> DWORD dwtype, sl = 256; </p> <p> for (int I = 1; I <8; I ++) <br/> {<br/> if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, "SYSTEM // CurrentControlSet // Services // Disk // Enum",/<br/> NULL, KEY_ALL_ACCESS, & hkey) = ERROR_SUCCESS) <br/>{< br/> CString id; <br/> id. format ("% d", I); <br/> if (RegQueryValueEx (hkey, id, NULL, & dwtype, (LPBYTE) sz, & sl) = ERROR_SUCCESS) <br/> {<br/> CString str = (CString) sz; <br/> m_select.AddString (sz ); <br/>}< br/> RegCloseKey (hkey); </p> <p> m_select.SetCurSel (0 ); </p> <p> if (m_select.GetCount () = 0) <br/>{< br/> AfxMessageBox ("no mobile device found! "); <Br/>}< br/> return TRUE; // return TRUE unless you set the focus to a control <br/>}</p> <p> void CMSSDlg: OnSysCommand (UINT nID, LPARAM lParam) <br/>{< br/> if (nID & 0xFFF0) = IDM_ABOUTBOX) <br/>{< br/> CAboutDlg dlgAbout; <br/> dlgAbout. doModal (); <br/>}< br/> else <br/>{< br/> CDialog: OnSysCommand (nID, lParam ); <br/>}</p> <p> // If you add a minimize button to your dialog, you will need th E code below <br/> // to draw the icon. for MFC applications using the document/view model, <br/> // this is automatically done for you by the framework. </p> <p> void CMSSDlg: OnPaint () <br/>{< br/> if (IsIconic ()) <br/> {<br/> CPaintDC (this); // device context for painting </p> <p> SendMessage (WM_ICONERASEBKGND, (WPARAM) dc. getSafeHdc (), 0); </p> <p> // Center icon in client rectangle <br/> int cxIcon = Ge TSystemMetrics (SM_CXICON); <br/> int cyIcon = GetSystemMetrics (SM_CYICON); <br/> CRect rect; <br/> GetClientRect (& rect ); <br/> int x = (rect. width ()-cxIcon + 1)/2; <br/> int y = (rect. height ()-cyIcon + 1)/2; </p> <p> // Draw the icon <br/> dc. drawIcon (x, y, m_hIcon); <br/>}< br/> else <br/>{< br/> CDialog: OnPaint (); <br/>}</p> <p> // The system callthis to obtain the cursor to display while t He user drags <br/> // the minimized window. <br/> HCURSOR CMSSDlg: OnQueryDragIcon () <br/>{< br/> return (HCURSOR) m_hIcon; <br/>}</p> <p> void CMSSDlg: OnExit () <br/>{< br/> // TODO: add your control notification handler code here <br/> CMSSDlg: OnCancel (); <br/>}</p> <p> LRESULT CMSSDlg: WindowProc (UINT message, WPARAM wParam, LPARAM lParam) <br/>{< br/> if (WM_DEVICECHANGE = message) <br/>{< br/> m_sele Ct. resetContent (); </p> <p> HKEY hkey; <br/> char sz [256]; <br/> DWORD dwtype, sl = 256; </p> <p> for (int I = 1; I <8; I ++) <br/> {<br/> if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, "SYSTEM // CurrentControlSet // Services // Disk // Enum",/<br/> NULL, KEY_ALL_ACCESS, & hkey) = ERROR_SUCCESS) <br/>{< br/> CString id; <br/> id. format ("% d", I); <br/> if (RegQueryValueEx (hkey, id, NULL, & dwtype, (LPBYTE) sz, & sl) = ERROR_SUCCESS) <br/>{< br/> CStr Ing str = (CString) sz; <br/> m_select.AddString (sz ); <br/>}</p> <p> m_select.SetCurSel (0); </p> <p> RegCloseKey (hkey ); <br/>}</p> <p> return CDialog: WindowProc (message, wParam, lParam); <br/>}</p> <p> void CMSSDlg:: OnAddPassword () <br/>{< br/> // TODO: Add your control notification handler code here <br/> unsigned char MBRBuf [512]; </p> <p> CString name; <br/> CString driver; </p> <p> int id; // select mobile device encoding. No. </p> <p> if (m_select.GetCurSel () = CB_ERR) <br/>{< br/> AfxMessageBox ("select the device to be encrypted! "); <Br/> return; <br/>}</p> <p> id = m_select.GetCurSel (); <br/> m_select.GetLBText (id, name ); </p> <p> // confirm the selected disk <br/> driver = GetDiskNumber (name ); </p> <p> // read the MBR of the disk <br/> if (ReadDisk (driver, MBRBuf, 0) = 0) <br/> return; </p> <p>/* RC4 encryption. The KEY is the Key. Here, the KEY [] = "MobileStorageSecurity ", later, you can <br/> enter the password as the Key */<br/> RC4_KEY rc4_key; <br/> build_rc4_key (Key, strlen (char *) Key ), & rc4_key); <br/> rc4_handler (MBRBuf, strlen (char *) MBRBuf), & rc4_key); </p> <p> // write the encrypted MBR to the disk. <br/> if (WriteDisk (driver, MBRBuf, 0) = 0) <br/> return; </p> <p> AfxMessageBox ("encrypted! "); </P> <p> // pop-up USB storage device <br/> PopupUSBDevice (); <br/>}</p> <p> void CMSSDlg :: onRemovePassword () <br/>{< br/> // TODO: Add your control notification handler code here <br/> unsigned char MBRBuf [512]; </p> <p> CString name; <br/> CString driver; </p> <p> int id; // select the mobile device number </p> <p> if (m_select.GetCurSel () = CB_ERR) <br/>{< br/> AfxMessageBox ("select the device to be encrypted! "); <Br/> return; <br/>}</p> <p> id = m_select.GetCurSel (); <br/> m_select.GetLBText (id, name ); </p> <p> // confirm the selected disk <br/> driver = GetDiskNumber (name ); </p> <p> // read the MBR of the disk <br/> if (ReadDisk (driver, MBRBuf, 0) = 0) <br/> return; </p> <p>/* RC4 encryption. The KEY is the Key. Here, the KEY [] = "MobileStorageSecurity ", later, you can <br/> enter the password as the Key */<br/> RC4_KEY rc4_key; <br/> build_rc4_key (Key, strlen (char *) Key ), & rc4_key); <br/> rc4_handler (MBRBuf, strlen (char *) MBRBuf), & rc4_key); </p> <p> // write the decrypted MBR to the disk <br/> if (WriteDisk (driver, MBRBuf, 0) = 0) <br/> return; </p> <p> AfxMessageBox ("decryption successful! "); </P> <p> // pop-up USB storage device <br/> PopupUSBDevice (); <br/>}</p> <p>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.