Bouncycastle JCE Practice (4)

zhaozj2021-02-17  85

?

Symmetrical encryption

?? Encryption can improve the physical security of terminal and network communication, three ways to encrypt transmission data:? *? Link encryption: Encryption between network nodes, transfer encryption between nodes, transfer to node, decrypt, different nodes Password.? *? Node encryption: Similar to the link encryption, the different is only when the data is transmitted between nodes, not using the format transmission, but is decrypted and re-encrypted with special "encrypted hardware, this dedicated hardware is usually rotated. In the safety safe.? *? First tail encryption: Encrypt the data to the network, then the data is transmitted from the network, then decrypt. The network itself will not know how the data is transmitted is encrypted data. This method The advantage is that each user on the network (usually one of the machines of each machine) can have different encrypted keywords, and the network itself does not need to add any specialized encryption devices. Disadvantages is that each system must be? A encryption device and corresponding software (management encrypted keyword) or each system must complete encryption work yourself (when the transmission rate is calculated by the unit of megabits / second, the amount of computation task is very large)

? This article uses the first encryption, the code is as follows:

/ / Read the key from the key file

?? SECRETKEY Key = NULL;

?? Try

?? {

?? // read the key from the key file

ObjectInputStream KeyFile = New ObjectInputStream

???? New fileinputstream ("C: // Security File //" Misclass.Username "/ Symmetric / Symmetric Key //yHb.des")));

??? k = (secretkey) keyfile.readObject ();

??? kyfile.close ();

???}

??? catch (filenotfoundexception EY1)

??? {

??? System.out.println ("Error When Read KeyFile);

??? system.exit (0);

???}

??? carat (Exception EY2)

??? {

??? System.out.println ("Error When Read The KeyFile);

??? system.exit (0);

???}

??? // produce a Cipher with KEY

??? cipher copher = null;

??? Try

{

// Encryption should be implemented in cipher

Cipher = Cipher.GetInstance ("DES");

// Set an encryption mode

???? cipher.init (cipher.encrypt_mode, key);

????} catch (Exception EY3)

???? {

???? System.out.Println ("Error When Create the Cipher);

???? system.exit (0);

????}

???? // From the dialog box to encrypt the file

???? file file = new file (Dirstring, String1);

???? String filename = file.getname ();

???? // read and encrypt file

???? Try

{

// input stream

BufferedInputStream in = New BufferedInputStream (New FileInputStream (file));

// output flow

????? cipheroutputstream out = new cipheroutputstream (New BufferedoutputStream (New BufferedoutputStream (New BufferedOutputStream

??????????? new fileoutputstream ("C: // security file // file //" filename ".yhb ")), acipher;

??? INT i;

??????? do {

??????? i = IN.READ ();

??????? IF (i! = - 1) Out.write (i); ????????} while (i! = - 1);

????? in.close ();

????? out.close ();

?????}

????? caratch (Exception EY5)

????? {

????? system.out.println ("Error When Encrypt the file");

????? system.exit (0);

?????}

?

Author's name HONGSOFT, research area: 1) Research Based on Workflow 2) Based on Java-based information security technology. Welcome to discuss Java-related issues hongbosoftware@163.com

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

New Post(0)