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