[Share] convert global strings to local variable storage to prevent sensitive strings from being exposed by static analysis.

Source: Internet
Author: User

Source code: http://download.csdn.net/detail/asmcvc/4368397

Global String Conversion to local variable storage prevents software from being exposed to sensitive strings by static analysis, supports UNICODE character sets and multi-byte character sets, supports Chinese characters, and does not perform encryption.

Running effect:

 

Enter the global string to be converted, for example, C: \ file \ my file. txt. Output:

// C: \ file \ My file .txt # ifdef _ UNICODETCHAR szC: \ file \ My file .txt [] = {'C ',':','\\', 'F', 'I', 'l', 'E', '\', 0x6211, 0x7684, 0x6587, 0x4EF6 ,'. ', 't', 'x', 't',' \ 0'}; # elseCHAR szC: \ file \ My file .txt [] = {'C ',': ',' \ ', 'F',' I ', 'l', 'E',' \ ', 0xCE, 0xD2, 0xB5, 0xC4, 0xCE, 0xC4, 0xBC, 0xFE ,'. ', 't', 'x', 't',' \ 0'}; # endif

If the string does not contain Chinese characters, for example, file not found

//file not foundTCHAR szfile not found[] = { 'f', 'i', 'l', 'e', ' ', 'n', 'o', 't', ' ', 'f', 'o', 'u', 'n', 'd', '\0' };

 

The above result allows you to use the variable name with slight modification. The source code is as follows:

// StrtestDlg. cpp: implementation file // # include "stdafx. h "# include" strtest. h "# include" strtestDlg. h "# ifdef _ DEBUG # define new DEBUG_NEW # endif // CstrtestDlg dialog box CstrtestDlg: CstrtestDlg (CWnd * pParent/* = NULL */): CDialog (CstrtestDlg: IDD, pParent), m_strIN (_ T (""), m_strOut (_ T ("") {m_hIcon = AfxGetApp ()-> LoadIcon (IDR_MAINFRAME);} void CstrtestDlg :: doDataExchange (CDataExchange * pDX) {CDialog: DoDataExchange (pDX); DDX_Text (pDX, IDC_EDIT1, m_strIN); DDX_Text (pDX, IDC_EDIT2, m_strOut);} decrypt (CstrtestDlg, CDialog) //} AFX_MSG_MAP evaluate (IDOK, & CstrtestDlg :: onBnClickedOk) END_MESSAGE_MAP () // CstrtestDlg message processing program BOOL CstrtestDlg: OnInitDialog () {CDialog: OnInitDialog (); // set the icon of this dialog box. When the main window of the application is not a dialog box, the framework will automatically // execute this operation SetIcon (m_hIcon, TRUE); // set the big icon SetIcon (m_hIcon, FALSE ); // set the small icon // TODO: add the additional initialization code m_strOut = _ T here ("this program converts the global string to local variable storage to prevent the software from exposing sensitive strings by static analysis, no encryption is performed. \ R \ n for example, input: C :\\\\ file \\\\ my file .txt \ r \ n "); UpdateData (FALSE); return TRUE; // return TRUE} unless the focus is set to the control. // use \ r \ nvoid CopyToClipboard (LPCTSTR szText, int nCbSize/* =-1 */) for line feed */) {if (szText = NULL) {return;} if (nCbSize =-1) {nCbSize = lstrlen (szText) * sizeof (TCHAR );} HGLOBAL hClip = GlobalAlloc (GMEM_ZEROINIT | GMEM_MOVEABLE | GMEM_DDESHARE, nCbSize + 1); if (hClip = NULL) {return;} char * pBuff = (char *) GlobalLock (hClip ); if (pBuff = NULL) {GlobalFree (hClip); return;} memcpy (pBuff, szText, nCbSize); pBuff [nCbSize] = 0; GlobalUnlock (hClip ); if (OpenClipboard (NULL) {EmptyClipboard (); SetClipboardData (CF_TEXT, hClip); CloseClipboard ();} GlobalFree (hClip);} void CstrtestDlg: OnBnClickedOk () {UpdateData (); CString strHeader = _ T ("CHAR sz") + m_strIN + _ T ("[] = {"); CString strText; CStringA strInputA; CStringW strInputW; CStringA strTempA; CStringW strTempW; WCHAR chTempW = 0; CHAR chTempA = 0; strInputA = m_strIN; strInputW = m_strIN; BOOL bContainChinese = FALSE; // obtain the UNICODE mode for (int I = 0; I <strInputW. getLength (); ++ I) {chTempW = strInputW. getAt (I); if (chTempW = L' \ ') {// escape character, and then take a ++ I; strTempW = strInputW. getAt (I); strText + = L "\ '\" + strTempW + L "\',";} else if (chTempW <0 | chTempW> 127) {// The Chinese Character bContainChinese = TRUE; strTempW. format (L "0x % X,", chTempW); strText + = strTempW;} else {// other normal characters strTempW = chTempW; strText + = L "\ '" + strTempW + L "\',";} if (I = strInputW. getLength ()-1) {strText + = L "\ '\ 0 \'};";}} m_strOut = L "T" + strHeader + strText + L "\ r \ n"; // when there are Chinese characters, the generated TCHAR string is different from the CHAR string if (bContainChinese = TRUE) {strText. empty (); for (int I = 0; I <strInputA. getLength (); ++ I) {chTempA = strInputA. getAt (I); if (chTempA = '\') {// escape character, and then take a ++ I; strTempA = strInputA. getAt (I); strText + = "\ '\" + strTempA + "\',";} else if (chTempA <0 | chTempA> 127) {// The strTempA character. format ("0x % X,", (unsigned char) chTempA); strText + = strTempA;} else {// other normal characters strTempA = chTempA; strText + = "\ '" + strTempA + "\',";} if (I = strInputA. getLength ()-1) {strText + = "\ '\ 0 \'};";}} // end for m_strOut = _ T ("# ifdef _ UNICODE \ r \ n") + m_strOut + _ T ("# else \ r \ n ") + strHeader + strText + _ T ("\ r \ n # endif \ r \ n ");} // use the original string as the comment m_strOut = _ T ("\ r \ n //") + m_strIN + _ T ("\ r \ n") + m_strOut; copyToClipboard (m_strOut, m_strOut.GetLength (); UpdateData (FALSE );}

 

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.