Hard work

xiaoxiao2021-03-06  90

Second Job 1, Source / * @ Author Liuxin * / Import Java.io. *; Import Java.Security. *; Import Javax.crypto. *; Import Javax.crypto.spec. *;

public class sdes {public static void main (String args []) {String s1 = "0101010101010101"; String s2 = "1f1f1f1f0e0e0e0e"; String s3 = "e0e0e0e0f1f1f1f1"; String s4 = "fefefefefefefefe"; String Algrithm = "DES"; String code = "Hello World!"; Byte [] KB1 = S1.GetBytes (); byte [] KB2 = S2.GetBytes (); byte [] KB3 = S3.GetBytes (); byte [] KB4 = S4.Getbytes (); Doencode (ReadByteArray)

(KB1), Algrithm, Code; doencode (ReadByteArray)

(KB2), Algrithm, Code; doencode (ReadByteArray)

(KB3), Algrithm, Code; doencode (ReadByteArray)

(kb4), algrithm, code);} public static byte [] readbytearray (byte "

b) {byte [] returnb = new byte [(int)

B.LENGTH / 2]; if ((B.Length% 2) == 1) {system.out.println ("HEHE

, WRONG "); return return;} for (int i = 0, j = 0; i <

B.LENGTH; I ) {IF (B [i] <0x3a) {RETURNB [J] =

(BYTE) ((B [i] & 0x0f) << 4);} else {returnb [j] =

(BYTE) (((B [i] & 0x0f) 0x09) << 4);} // high bit if (b [ i] <0x3a) {RETURNB [J] =

(byte) (Returnb [J] | (B [i] & 0x0f);} else {return [j] =

(Byte) ((b [i] & 0x0f) 0x09));} j ;} return return;} public static void doencode (byte []

EncodeKey, String Algrithm, String code) {Try {// Get Specified Key SecretKeyspec Key = New

SecretKeyspec (EncodeKey, Algrithm); // Encoding ... Cipher CP =

Cipher.getInstance (algrithm); cp.init

(Cipher.Encrypt_mode, key); // Origin code byte [] Origincode =

Code.getbytes ("UTF8"); // Output Origin Code if (Times == 1) {/ * for (INT i = 0; I

(Origincode [i] "");} * / system.out.print

(byte2hex (Origincode));} Times ; system.out.println (""); // do encode byte [] eNCode =

cp.dofinal (Origincode); // Output the Encode Code / * for (int i =

0; i

(Encode [i] "");} * / system.out.print (byte2hex

(eNCode); system.out.println (""); system.out.println

(Byte2Hex (EncodeKey));} catch

(java.security.nosuchaalgorithmexception E1)

{E1.PrintStackTrace ();} catch

(javax.crypto.nosuchpaddingexception E2)

{E2.PrintStackTrace ();} catch (java.lang.exception E3)

{E3.PrintStackTrace ();}} public static string byte2hex (Byte [] b) //

Two-line system string {string hs = "; string stmp =" "; for (int N = 0; n

0xFF)); if (stmp.Length () == 1) HS = HS "0" stmp; Else HS = HS STMP; if (n

My description is as follows: Use of the expressions to: "Hello World!" Key is KEY1-4, is the content of Java SDES> SDES.TXT 68: 65: 6C: 6C: 6F: 20: 77: 6F: 72: 6c: 64: 21: F3: F8: EA: 78: 1A: 12: 41: C0: 45: F9: 53: C5: 14: 01: 2501: 01: 01: 01: 01: 01: 01: 01 F8: 83: 2D: B2: E0: 0A: 89: C4: F4: 60: AB: 0F: 14: 12: 54: 5E1F: 1F: 1F: 1F: 0e: 0e: 0e: 0e E7: AE: 6C : 25: 00: 6A: D5: AC: F7: ED: F7: FB: 27: CF: 21: 16E0: E0: E0: E0: F1: F1: F1: F1 11: EB: 97: A9: 54: 01: 0C: 86: 81: C3: C5: 99: 8F: 11: FF: C6FE: Fe: Fe: Fe: Fe: Fe: Fe

Since cipherology and plaintext are saved with byte arrays such as Byte [], I wrote the Byte2Hex function, and the 16-way form of each byte of the array is output.

The first line is clear text, and each of the two lines below, the above is Ciphertext,

The face is the corresponding Keyn. The specific law is the specific algorithm of the DES encryption algorithm, DES is a symmetrical

The algorithm is to say that the encrypted decryption is symmetrical. DES algorithm

The calculation, the specific algorithm teacher's courseware: reflecting this

Law of law.

2, running in a directory: java keyagree bpub.dat Apri.dat Generates shared keys Under the B directory: java keyagree APUB.DAT BPRI.DAT Generates the shared key According to the code, the output of the instruction is shared. Key, and

The result of running instructions in these two directories is the same, so running

Java keyagree bpub.dat apri.dat> Share.txt

The content of the shared key is 1, -32, 7, 71, -91, 69, -48, -77, -85, -35, 19, 16, -41, -89, -

100, 56, 15, -110, 98, 110, 53, -48, -20, 43, -48, 15, -64,

80, 86, 66, 113, 63, 73, 60, 119, -

42, 110, 100, 19, 55, 65, 55, -73, 77, 2, 29, -122, -46, -84,

66, -30, -43, -54, -88, 9, -127, 61, 56, -63, -

55, 52, 99, 69, 52, 45, -109, -89, -117, 119, 95, 89, 6, -

67, 62, -126, 82, 48, -53, 60, -1, 93, -44, -29, 66, -62, -54,

-75, 23, 30, 114, -85, 80, -76, 57, 68, -18, 63, 125,

118, 117, 99, 67, 41, -75, 36, -86, 98, 74, 68, 22, -56,

2, 5, 29, 30, 53, 120, 111, -127, -56, -44, -92, 120, 94, -54,

-10, -82, -90,

1. Using the DES algorithm, encrypt the following key, observe the characteristics of clear text and ciphertext, summarize the law

Key1 (hexadecimal)

0101 0101 0101 0101

Key2

Allf

Allf

Allf

Allf

0e0e 0e0e

Key3

E0E0 eoeo f

Allf

1

fly

Allf

1

Key4

Fefe Fefe Fefe Fefe

Requirements: give the source code, secret literary pair, regular summary

2. Use the Diffie_hellman algorithm for key exchange

File function

Key_DH.JAVA

Create a Diffie_Hellman's public key and private key

Keyagree.java

Create a shared key

Exercise process

Establish two directories A and B, simulate the A, B parties that need to be communicated, because the DH algorithm requires A and B each generate DH public key and private key, so copy the compiled file KEY_DH in these two directories.

First create your own public key and private key, that is, enter the "Java Key_DH APUB.DAT APRI.DAT" running in the A directory, which is the file APUB.DAT and APRI.DAT, which will generate file APUB.DAT and APRI.DAT, respectively A public key and private key.

Then create its own public key and private key, that is, enter the "Java Key_DH BPUB.DAT BPRI.DAT" running in the B directory, which is the file bpub.dat and bPri.dat, which will generate file B, respectively, save B The public key and private key.

Finally release the public key, A copies the APUB.DAT to the B directory, b copies bpub.dat to the A directory.

Copy KEYAGREE and copy it to the A and B directory.

Run under a directory: java keyagree bpub.dat Apri.dat generates shared keys

Run under the B directory:

Java keyagree apub.dat bpri.dat generates shared keys

Requires the last shared key

转载请注明原文地址:https://www.9cbs.com/read-102203.html

New Post(0)