JAVA Implementation of AES and RSA Algorithms

Source: Internet
Author: User

There is nothing to say. Everything is in the hierarchy.

Import java. Io .*;
Import java. Security .*;

Import javax. crypto .*;

/**
* The AES algorithm generates a secret and decrypts the file.
* @ Author Li Xiaofeng
* Since the DES algorithm can break through the Limit algorithm in the later version of The AES--DES algorithm, it is not recommended to use it.
*/
Public class aesimpl {
// Produce AES Password
Public void createkey (){
Try {
Keygenerator keygen = keygenerator. getinstance ("AES ");
Securerandom random = new securerandom ();
Keygen. INIT (random );
Secretkey key = keygen. generatekey ();
Objectoutputstream out = new objectoutputstream (New fileoutputstream ("aeskey.txt "));
Out. writeobject (key );
Out. Close ();
} Catch (nosuchalgorithmexception e ){
E. printstacktrace ();
} Catch (ioexception e ){
E. printstacktrace ();
}
}
 
Public void run (string code ){
Int mode = cipher. encrypt_mode;
String inputfilename = "encode.txt"; // name of the file to be encrypted
String outputfilename = "aesdecode.txt"; // encrypted file name
If ("decode". Equals (CODE )){
Mode = cipher. decrypt_mode;
Inputfilename = "aesdecode.txt"; // name of the file to be decrypted
Outputfilename = "encode.txt"; // decrypted file name
}
Try {
// Encrypt the AES password file.
Objectinputstream keyin = new objectinputstream (New fileinputstream ("aeskey.txt "));
Key key = (key) keyin. readobject ();
Keyin. Close ();

Inputstream in = new fileinputstream (inputfilename );
Outputstream out = new fileoutputstream (outputfilename );
Cipher cipher = cipher. getinstance ("AES ");
Cipher. INIT (mode, key );

// Encryption/Decryption
Crypt (In, out, cipher );
In. Close ();
Out. Close ();
} Catch (exception e ){
E. printstacktrace ();
}
}

Public static void crypt (inputstream in, outputstream out, cipher) throws ioexception, writable bufferexception, generalsecurityexception {
Int blocksize = cipher. getblocksize ();
Int outputsize = cipher. getoutputsize (blocksize );
Byte [] inbytes = new byte [blocksize];
Byte [] outbytes = new byte [outputsize];

Int inlength = 0;
Boolean more = true;

While (more ){
Inlength = in. Read (inbytes );
If (inlength = blocksize ){
Int outlength = cipher. Update (inbytes, 0, blocksize, outbytes );
Out. Write (outbytes, 0, outlength );
}
Else more = false;
}
If (inlength> 0 ){
Outbytes = cipher. dofinal (inbytes, 0, inlength );
}
Else {
Outbytes = cipher. dofinal ();
}
Out. Write (outbytes );
}
 
Public static void main (string [] ARGs ){
Aesimpl _ AES = new aesimpl ();
// _ AES. createkey ();
_ AES. Run ("decode ");
}

}

 

Import java. Io. datainputstream;
Import java. Io. dataoutputstream;
Import java. Io. fileinputstream;
Import java. Io. fileoutputstream;
Import java. Io. inputstream;
Import java. Io. objectinputstream;
Import java. Io. objectoutputstream;
Import java. Io. outputstream;
Import java. Security. Key;
Import java. Security. keypair;
Import java. Security. keypairgenerator;
Import java. Security. securerandom;

Import javax. crypto. cipher;

/**
* 1. The RSA algorithm generates a password pair.
* 2. Use the generated public secret to encrypt the AES secret and put the content encrypted with the AES secret into a file.
* 3. Use the generated private key pair to decrypt the AES password in the file, and use the decrypted AES password pair to decrypt the file content.
* @ Author Li Xiaofeng
*
*/
Public class rsaimpl {
Private Static final int keysize = 512;
Public void createkey (){
Try {
Keypairgenerator pairgen = keypairgenerator. getinstance ("RSA ");
Securerandom random = new securerandom ();
Pairgen. initialize (keysize, random );
Keypair = pairgen. generatekeypair ();
// Generate a public secret file
Objectoutputstream out = new objectoutputstream (New fileoutputstream ("publickey.txt "));
Out. writeobject (keypair. getpublic ());
Out. Close ();
// Generate a private secret file
Out = new objectoutputstream (New fileoutputstream ("privatekey.txt "));
Out. writeobject (keypair. getprivate ());
Out. Close ();
} Catch (exception e ){
E. printstacktrace ();
}
}
 
Public void encode (){
Try {
// Encrypt the public key pair to encrypt the AES key pair.
Objectinputstream in = new objectinputstream (New fileinputstream ("publickey.txt "));
Key publickey = (key) in. readobject ();
In. Close ();

// Encrypt the AES Password
In = new objectinputstream (New fileinputstream ("aeskey.txt "));
Key key = (key) in. readobject ();
In. Close ();

// Encrypt the AES Password
Cipher cipher = cipher. getinstance ("RSA ");
Cipher. INIT (Cipher. wrap_mode, publickey );
Byte [] wrappedkey = cipher. Wrap (key );
// The encrypted AES connector is in the file
Dataoutputstream out = new dataoutputstream (New fileoutputstream ("rasdecode.txt "));
Out. writeint (wrappedkey. Length );
Out. Write (wrappedkey );

// Encrypt the file to be encrypted
Inputstream input = new fileinputstream ("encode.txt ");
Cipher = cipher. getinstance ("AES ");
Cipher. INIT (Cipher. encrypt_mode, key );
// Use AES encrypted encryption to encrypt the file
Aesimpl. crypt (input, out, cipher );
Input. Close ();
Out. Close ();
} Catch (exception e ){
E. printstacktrace ();
}
}
 
Public void decode (){
Try {
// Decrypt the file to be decrypted, including the encrypted content of AES encrypted using the RAS Algorithm
Datainputstream in = new datainputstream (New fileinputstream ("rasdecode.txt "));
Int length = in. readint ();
Byte [] wrappedkey = new byte [length];
In. Read (wrappedkey, 0, length );

// Encrypt the private secret file of RSA
Objectinputstream keyin = new objectinputstream (New fileinputstream ("privatekey.txt "));
Key privatekey = (key) keyin. readobject ();
Keyin. Close ();

// Use RSA private cipher to decrypt the encrypted AES cipher
Cipher cipher = cipher. getinstance ("RSA ");
Cipher. INIT (Cipher. unwrap_mode, privatekey );
Key key = cipher. Unwrap (wrappedkey, "AES", cipher. secret_key );

Outputstream out = new fileoutputstream ("encode.txt ");
Cipher = cipher. getinstance ("AES ");
Cipher. INIT (Cipher. decrypt_mode, key );
// Use the decrypted AES password to decrypt the encrypted file
Aesimpl. crypt (In, out, cipher );
In. Close ();
Out. Close ();
} Catch (exception e ){
E. printstacktrace ();
}
}
 
Public static void main (string [] ARGs ){
Rsaimpl _ RSA = new rsaimpl ();
// _ RSA. createkey ();
// _ RSA. encode ();
_ RSA. Decode ();
}

}

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.