Program to demonstrate the implementation of encryption and decryption algorithms

Source: Internet
Author: User

Text/figure dark night dancer
==========================================
Currently, the software developed by various companies has their own encryption algorithms to protect copyrights. users need to pay for all the functions of the software, and the merits and demerits of the algorithms directly determine the difficulty of the implementation method.
Next I will demonstrate the implementation of an encryption and decryption algorithm for the encryption and decryption of a single data, and the key is determined by myself, as shown in interface 1. The three edit boxes connect the three control variables CString m_jiami, CString m_jiemi, and WORD m_miyao. The WORD type is unsinged short. The encryption function code is as follows.
 
Figure 1

CString str, str1, r;
Int I, j;
Str = s;
For (I = 0; I <s. GetLength (); I ++)
{
Str. SetAt (I, s. GetAt (I) + k );
// Extract each character of the original data in sequence, add the key, and write a new string
}
S = str; // update the original string
For (I = 0; I <s. GetLength (); I ++)
{
J = (BYTE) s. GetAt (I );
Str1 = "01 ";
Str1.SetAt (0, 65 + j/26 );
Str1.SetAt (+ j % 26); // extract each character from the updated string in sequence, and use an algorithm to separate one character into two characters.
R + = str1; // synthesize the final string
}
Return r; // return this string

The algorithm of the code above is obviously to break down the original character into two parts, namely "65 + j/26" and "65 + j % 26 ". This algorithm is not static. You can write your own algorithms as needed. The decryption function is provided below. The Code is as follows.

CString r, str;
Int I, j;
For (I = 0; I <s. GetLength ()/2; I ++)
{
J = (BYTE) s. GetAt (2 * I)-65) * 26;
J + = (BYTE) s. GetAt (2 * I + 1)-65;
Str = "0 ";
Str. SetAt (0, j );
R + = str;
}
S = r;
For (I = 0; I <s. GetLength (); I ++)
{
R. SetAt (I, (BYTE) s. GetAt (I)-k );
}
Return r;

This function is the inverse operation of the encryption function. It is not difficult to understand, but pay attention to the type transformation. In string operations, especially in the development of such encryption and decryption algorithms, two functions are very useful, namely GetAt () and SetAt (), they are CString functions. The prototype is "TCHAR GetAt (int nIndex) const" and "void SetAt (int nIndex, TCHAR ch)", where nIndex is the character position, ch indicates a character. GetAt is the character whose reading position is nIndex, and SetAt is the ch character whose position is nIndex. Next we will test the compiled program, as shown in 2.
 
Figure 2
This article is just a reference to introduce the implementation methods of encryption and decryption algorithms. In fact, there are many advanced algorithms that are not implemented in this simple way, but I have limited skills and have no knowledge of them, I hope you will be enlightened by others. If you have any better implementation methods, please exchange and discuss them.

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.