Bouncycastle JCE Practice (4)

xiaoxiao2021-03-17  222

Symmetrical encryption

?? Encryption can improve the physical security of terminals and network communication, and three methods encrypt transfer data:?

*? Link Encryption: Encryption between network nodes, transfer encryption between nodes, decrypts after the node is transferred, and different passwords are used in different nodes.?

*? Node encryption: Similar to link encryption, different only when data is transmitted between nodes, no use of clear format transmission, but decrypts and re-encryption with special "encrypted hardware, this dedicated hardware is usually rotated in safety safe in.?

*? Leading 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 is the advantage of the network. Each user (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. Is the disadvantage that each system is necessary? There is a cryptographic device and corresponding Software (management encryption keyword) or each system must complete encryption work yourself (when the transmission rate is calculated by the unit of megabits / second, the amount of encryption tasks 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) Java-based information security technology. Welcome to discuss all aspects of Java related issues

HONGBOSoftware@163.com

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

New Post(0)