Encryption_Demo
--- Introduction to analog information encryption flow diagram
Encryption_Demo code simulates the flowchart content of the first and Fig. 2, but just simulates, talking about what application; its application process is limited to the process of delivery, not involving data transmission between networks, so it is just simulating the process. .
Figure 1. Flowchart of information encryption with secure authentication
Figure 2. Information decryption process using safety certification technology
Figure 3. Information encryption flow chart in secure authentication
Figure 4. Text description of the information decryption process diagram with secure authentication technology
There is no novelty of Encryption_Demo code. The purpose of the release is to make everyone understand the operation process of "using secure authentication information encryption flow chart". Code, you can download it. Below I have to say is that the main work is based on the calculation of the offset of the byte array. For example, in this program, the HD5 hash, its hash value fixed length 16 bytes; use the DES algorithm packet encryption, its key and IV bytes are 8 bytes; using RSA Signature, Signature Length Fixed For 128 bytes, while the length of the encrypted result is 128 8 8 = 144, then the number of bytes after RSA is 1152 bytes. Then, in the first flowchart, the entire ciphertext data byte length after the merger, we know that it is not fixed, but its first part and the third portion are fixed (again, we use DES packet encryption here. Algorithm, MD5 hash function, RSA method signature, and encryption, the following analysis results are also based on this basis). such as:
Cipher Cipher (RSAENCryptdata: 1152)
Summary sign ----- Combindata: 144
We can get the length of the entire file to be intercept file
String filename = OpenFile ("Please open the file you want to decrypted:");
FILESTREAM FS = New FileStream (FileName, Filemode.Open, FileAccess.Read);
(Int) fs.length - 1152-144 The result is the length of the middle ciphertext-EncryptionDate. After decrypting the middle secret text, it will handle timestamp. We analyze the timestamp's byte array composition:
File hash length: 16
Receive time: 23
Signature length: 128
Total length: 167
Since you know how the array of bytes, what should you know?
About processing the processing of receiving timestamps, it is a little attention:
Public static byte [] gettimenow ()
{
// This is the format that is transformed into: 2004-11-09 13: 04: 28-108 23 bytes
DateTime now = datetime.now;
System.Text.utf8encoding utf = new system.text.utf8encoding ();
String Month = NULL;
IF (now.Month <10)
Month = "0" now.month.tostring ();
Else
Month = now.month.toString ();
String day = NULL;
IF (now.day <10)
Day = "0" now.day.tostring ();
Else
Day = now.day.tostring (); string millisecond = NULL;
IF (now.MilliseCond <10)
MilliseCond = "00" now.millisecond.toString ();
IF (now.millisecond> = 10 &&ow.millisecond <= 99)
Millisecond = "0" now.millisecond.toString ();
IF (now.millisecond> = 100)
Millisecond = now.millisecond.toString ();
String fullformattime = now.year.tostring () "-" MONTH "-" day " now.tolongTimeString () " - " MilliseCond; //, such as: 2004-11-09 13:04 : 28-108
Return Utf.getBytes (FullFormattime);
}
The result of such a processed is that the reception time is accurate to milliseconds, and the fixed byte length is 23 bytes.
We write the results of the first flowchart into the file (its path to the original plain text you want to encrypted in the same directory, mainly for the convenience of storage), and the decryption results of the flowchart I save are in Bin / Release Here. This needs to be read in this decryption, which provides two ways, the first to read the file content into an array, and the other is the specified byte array extracted directly from the file. The first method reads the contents of the file to a population, decrypts the decomposition array, and uses the buffer.blockcopy () function to decompose. For example, start copying "Length" length bytes from the offset 1152 from the FileContent array to a new array "encrypt_two".
INT length = (int) fs.length-1296; // -1152-144; // Ciphertext length AllencryptedData
Encrypt_two = new byte [length];
Buffer.blockcopy (FileContent, 1152, Encrypt_TWO, 0, Length);
The second method is adopted:
Encrypt_one = new byte [1152];
fs.read (Encrypt_One, 0,1152);
This reads 1152 bytes directly from the file stream start position to the array "encrypt_one". But if you want to extract bytes from the starting point, you need to set the file stream:
// Read the encrypted plain text
INT length = (int) fs.length-1296; // -1152-144; // Ciphertext length AllencryptedData
Encrypt_two = new byte [length];
fs.position = 1152; // File flow offset location 1152 bytes
fs.read (Encrypt_TWO, 0, Length);
Note the representation of the parameters in the fs.read () method, which expression: fs.read (Encrypt_TWO, Length, Length), the program reports the file offset error.
It should be noted that when the design code is designed, the function that the main form program can be encapsulated into a class, and all the functions that are used are static types, which is convenient for easy use. Test procedures require three pairs of keys: the respective private key and public key of the sender (a), the receiver (b), and the timestamp server (DTS). Based on the flow chart 1. You use the timestamp server private key once, the private key is twice, B's public key once. Based on the flow chart 2, you use the timestamp server public key once, the public key of A is twice, and the private key is once. As for the key to test, you can see three folders in the compressed package Encryption demo / bin / release directory, which stores the respective senders (a), recipients (b) and timestamp server (DTS), respectively. Key. Of course, you can also generate a key yourself, click on "Generate a key" button, see Figure: The resulting key can be found in the bin / release directory of the program.
So how to use this program, you can use the three pairs of public keys that have been prepared, follow the figure below:
1. "Open File" - Select the file you want to add, click on the "Submit to the DTS Service Center ..." button below.
2. Click the tab "Encrypted Step 2", click the button below "Start the above steps", which will ask you to open the key file, don't make a mistake.
3. The last tab, "Start Decryption" button, you know how to do it :)
Note: This will appear in the middle, ask you to open the key file, don't choose the wrong, or not, there will be no expected goals. The reason why it is, it is necessary to pay attention to the difference between the division key and the key (or if it does not reflect the written intent of this program. You said ~!? J). Of course, everyone can use better symmetrical encryption algorithms, haveh functions, such as Rijndeal symmetrical encryption algorithms, SHA512, etc., then you need to recalculate the byte offset in this (if necessary, you can customize). It's not enough, code and comment are written, please look at J Bye_bye!
Code download address: http://nie_yong.go.nease.net/code/encryption demo.ra