Bouncycastle JCE practice (5)

Source: Internet
Author: User
?

Implementation of symmetric decryption

Symmetric encryption/decryption algorithms have several problems in the e-commerce transaction process:

(1 )?????? A secure channel is required for both parties to negotiate a common key during the first communication. Direct face-to-face negotiation may be unrealistic and difficult to implement. Therefore, both parties may need to negotiate through emails, phone calls, and other relatively insecure means;

(2 )?????? The number of keys is difficult to manage. Because different keys are required for each partner, it is difficult to adapt to a large amount of information exchange in the open society;

(3 )?????? Symmetric encryption algorithms generally do not provide information integrity identification. It cannot verify the identity of the sender and receiver;

The management and distribution of symmetric keys is a potentially dangerous and cumbersome process. Symmetric encryption is implemented based on mutual protection of secrets. The trade sides that adopt symmetric encryption technology must ensure that the same key is used and that the exchange of keys between them is secure and reliable, you must also set a program to prevent key leaks and change the key.

The symmetric decryption code is implemented 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 key // yhb. Des "));

??? Key = (secretkey) Keyfile. readobject ();

??? Keyfile. Close ();

??? }

??? Catch (filenotfoundexception ey1)

??? {

??? System. Out. println ("error when read Keyfile ");

??? System. Exit (0 );

??? }

??? Catch (exception EY2)

??? {

??? System. Out. println ("error when read the Keyfile ");

??? System. Exit (0 );

??? }

?? // Generate cipher with key

??? Cipher cipher = NULL;

??? Try

{

// Set the algorithm. It should be the same as the setting during encryption.

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 );

???? }

???? // Obtain the file to be decrypted from the dialog box and decrypt it

???? File file = new file (dirstring, string1 );

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

??? Try

{

// Output stream. Pay attention to obtaining the file name.

Bufferedoutputstream out = new bufferedoutputstream (New fileoutputstream (

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

???? // Input stream

????? Cipherinputstream in = new cipherinputstream (New bufferedinputstream (

??????????? New fileinputstream (File), cipher );

??? Int thebyte = 0;

??? While (thebyte = in. Read ())! =-1)

??? {

??? Out. Write (thebyte );

??? }

????? In. Close ();

????? Out. Close ();

????? }

????? Catch (exception ey5)

????? {

????? System. Out. println ("error when encrypt the file ");

????? System. Exit (0 );

????? }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.