Bouncycastle JCE Practice (5)

xiaoxiao2021-03-17  189

Symmetrical decryption

Symmetrical encryption / decryption algorithm exists in the e-commerce transaction process:

(1) ?????? Requires a secure channel to make communication between the communication between the first communication, a common key. Direct face-to-face negotiation may be unrealistic and difficult, so both parties may need to be negotiated with other relatively unacceptable means such as email and phones;

(2) ?????? The number of keys is difficult to manage. Because every collaborator needs to use different keys, it is difficult to adapt to a large number of information exchange in the open society;

(3) ?????? The symmetric encryption algorithm generally does not provide identification of information integrity. It can't verify the identity of the sender and the recipient;

The management and distribution of symmetrical keys is a potentially dangerous and cumbersome process. Symmetric encryption is based on common conservative secrets, and the trade between the trade of symmetric encryption technology must ensure that the same key is used, ensuring that the exchange of keys to each other is safe and reliable, but also set to prevent key leakage and change Program of the key.

The code of symmetrical decryption is achieved as follows:

/ / Read the key from the key file

?? SECRETKEY Key = NULL;

?? Try

?? {ObjectInputStream KeyFile = New ObjectInputStream

???? New fileinputstream ("c: // security file //" misclass.username "/-symmetric // symmetry 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);

???}

?? // generate Cipher with Key

??? cipher copher = null;

??? Try

{

// Set the algorithm, should be the same as the encrypted setting

Cipher = Cipher.GetInstance ("DES");

/ / Set the decryption mode

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

????} catch (Exception EY3)

???? {

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

???? system.exit (0);

????}

???? // Net files to decrypt from the dialog and decrypt

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

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

??? Try

{

// Output stream, please note the acquisition of the file name

BufferedoutputStream out = new bufferedoutputstream (New FileoutputStream

????????? "c: // security file // file //" filename.substring (0, FileName.Length ()-4)))));

???? // input stream

????? CipherInputStream in = New CipherInputStream (New BufferedInputStream (New BufferedInputStream

?????????? new fileinputstream (file)), cipher); ??? Int thebyte = 0;

??? while ((thebyte = in.read ())! = - 1)

??? {

??? out.write (thebyte);

???}

????? in.close ();

????? out.close ();

?????}

????? caratch (Exception EY5)

????? {

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

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

?????}

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

New Post(0)