Java implements desede (dual des) for file encryption

Source: Internet
Author: User

 

Import java. Io. fileoutputstream;
Import java. Io. objectoutputstream;
Import java. Security. nosuchalgorithmexception;
Import java. util. Logging. level;
Import java. util. Logging. Logger;
Import javax. crypto. keygenerator;
Import javax. crypto. secretkey;

/*
* To change this template, choose tools | templates
* And open the template in the editor.
*/

/**
*
* @ Author Administrator
*/
Public class genkey {
Public static void main (string [] ARGs) {// generate a key
// Save the key in the file
Try {
KeyGenerator keyGen = KeyGenerator. getInstance ("DESede ");
// Initialize the key generator
KeyGen. init (1, 168 );
// Generate the key
SecretKey key = keyGen. generateKey (); // stored as a binary file stream
// Save the key in the file
FileOutputStream f = new FileOutputStream ("key3.dat ");
ObjectOutputStream B = new ObjectOutputStream (f );
B. writeObject (key );
F. close ();
B. close ();
} Catch (Exception ex ){
Logger. getLogger (GenKey. class. getName (). log (Level. SEVERE, null, ex );
}
}

}

 

 

 

 

 

/*
* To change this template, choose Tools | Templates
* And open the template in the editor.
*/

Package cipherinputstream;

Import java. Io. fileinputstream;
Import java. Io. fileoutputstream;
Import java. Io. objectinputstream;
Import java. Security. Key;
Import java. util. Logging. level;
Import java. util. Logging. Logger;
Import javax. crypto. cipher;
Import javax. crypto. cipherinputstream;

/**
*
* @ Author Administrator
*/
Public class encryptanddecrypt {
Public static void main (string [] ARGs ){
// Name of the file to be encrypted or decrypted
String datafilename = ARGs [0];
// Whether encryption or decryption is performed, encryption is encrypt, and decryption is decrypt
String opmode = ARGs [1];
// Name of the key file
String keyFileName = "key. dat ";
Try {
// Generate the key
FileInputStream keyfcm = new FileInputStream (keyFileName );
ObjectInputStream OIS = new ObjectInputStream (keyfs );
Key key = (Key) OIS. readObject ();
// Create and initialize the secret
Cipher cp = Cipher. getInstance ("DESede ");
If (opMode. inclusignorecase ("encrypt ")){
Cp. init (Cipher. ENCRYPT_MODE, key );
}
Else if (opMode. inclusignorecase ("decrypt ")){
Cp. init (Cipher. DECRYPT_MODE, key );
}
Else return;

// Create a CipherInputStream object
FileInputStream dataFIS = new FileInputStream (dataFileName );
CipherInputStream CIS = new CipherInputStream (dataFIS, cp );
// For encryption operations
If (opMode. inclusignorecase ("encrypt ")){
// Create a file output stream
FileOutputStream FOS = new FileOutputStream ("mi.txt ");
Int B = 0;
System. Out. Print ("the encrypted ciphertext is :");
While (B = CIS. Read ())! =-1 ){
System. Out. Print (char) B );
FOS. Write (byte) B );
}
System. Out. println ("/n ciphertext has been written to the file. ");
}
Else {// for decryption
Int B = 0;
Fileoutputstream Fos = new fileoutputstream ("jiemi.txt ");
System. Out. Print ("the decrypted plaintext is :");
While (B = CIS. Read ())! =-1 ){
FOS. Write (B); // write to file

}
}
} Catch (Exception ex ){
Logger. getLogger (EncryptAndDecrypt. class. getName (). log (Level. SEVERE, null, ex );
}
}

}

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.