/*
To run this program you need to download the JCE with Provider and lightweight APIs jce,bouncy Castle
URL is http://www.bouncycastle.org
The configuration is as follows:
In Windows, you need to copy the downloaded Bcprov-jdk14-119.jar file to two places:
One in your installed JDK directory, for example, mine is C:\j2sdk1.4.0-rc\jre\lib\ext.
Another in your JDK running environment I was in the
C:\Program Files\java\j2re1.4.0-rc\lib\ext;
In addition, two java.security are modified:
Mine is in the C:\j2sdk1.4.0-rc\jre\lib\security\java.security;
C:\Program files\java\j2re1.4.0-rc\lib\security\java.security;
Add Security.provider.6=org.bouncycastle.jce.provider.bouncycastleprovider to Java.security
If all goes well, you can run this program.
This program has the ability to encrypt your files. You need to specify the data, the program has given the interface.
For example, you specify the file name to encrypt "4.txt", the Encrypted file location "6.txt",
There are password password such as "Liufeng", run the program, then "6.txt" will be "4.txt" ciphertext.
Note that the password is the key to decryption, do not forget.
Other decryption process for its own reference.
This program uses the session key to encrypt, provides many interfaces. If you need an encryption process in your project, you can improve it for you
*/
Import java.security.*;
Import java.security.spec.*;
Import javax.crypto.*;
Import javax.crypto.spec.*;
Import java.io.*;
Import java.util.*;
public class Fileencryptorrsa {
private static final int iterations=1000;//calculation times, in addition to salt
private static byte[] publickeybytes;//public key
private static byte[] privatekeybytes;//private key
private static String sessionkey;//session key
public static String encrypt_privatekey_file= "1.txt";//The file places an encrypted private key
private static String text_file= "4.txt";//files to be encrypted
private static String encrptor_text_file= "5.txt";/Encrypted File
private static String dencryptor_text_file= "6.txt";//decrypted file
private static String password= "Liufeng";//password for encrypting private key
public void Settext_file (String fileName) {
Text_file=filename;
}
public void Setencrypt_privatekey_file (String fileName) {
Encrypt_privatekey_file=filename;
}
Public String Getencrypt_privatekey_file () {
return encrypt_privatekey_file;
}
public void Setencrptor_text_file (String fileName) {
Encrptor_text_file=filename;
}
Public String Getencrptor_text_file () {
return encrptor_text_file;
}
public void Setdencryptor_text_file (String fileName) {
Dencryptor_text_file=filename;
}
Public String Getdencryptor_text_file () {
return dencryptor_text_file;
}
public void SetPassword (String password) {
This.password=password;
}
Create a RSA Secretkey
public static void CreateKey () throws exception{
Keypairgenerator keypairgenerator=keypairgenerator.getinstance ("RSA");
Keypairgenerator.initialize (1024);
KeyPair Keypair=keypairgenerator.genkeypair ();
The byte array that gets the public key
Publickeybytes=keypair.getpublic (). getencoded ();
Get the private key
Byte[] Privatekeybytes=keypair.getprivate (). getencoded ();
Byte[] Encrytedprivatekey=passwordencrypt (Password.tochararray (), privatekeybytes);
FileOutputStream fos=new FileOutputStream (encrypt_privatekey_file);
Fos.write (Encrytedprivatekey);
Fos.close ();
}
Encrypt the private key by giving the password
private static byte[] Passwordencrypt (char[] password,byte[] privatekeybytes)
Throws exception{
Create 8 byte salt
Byte[] Salt=new byte[8];
Random random=new Random ();
Random.nextbytes (salt);
Create a PBE key and cipher
Pbekeyspec keyspec=new pbekeyspec (password);
Secretkeyfactory keyfactory=secretkeyfactory.getinstance ("PBEWITHSHAANDTWOFISH-CBC");
Secretkey Key=keyfactory.generatesecret (KEYSPEC);
Pbeparameterspec paramspec=new Pbeparameterspec (salt,iterations);
Cipher cipher=cipher.getinstance ("PBEWITHSHAANDTWOFISH-CBC");
Cipher.init (CIPHER.ENCRYPT_MODE,KEY,PARAMSPEC);
Encrypt the byte[]
Byte[] Cipherprikey=cipher.dofinal (privatekeybytes);
Write out salt, and then the Cipherprikey
Bytearrayoutputstream baos=new Bytearrayoutputstream ();
Baos.write (salt);
Baos.write (Cipherprikey);
return Baos.tobytearray ();
}
Encrypts the given file with the session key, encrypts the session key with the public key, and stores it in the file
The last encrypted file consists of the key length + encrypted key (session key) + ciphertext
public static void Encrypt () throws exception{
Convert to RSA key
X509encodedkeyspec keyspec=new X509encodedkeyspec (publickeybytes);
Keyfactory keyfactory=keyfactory.getinstance ("RSA");
PublicKey publickey=keyfactory.generatepublic (KEYSPEC);
Open the file for storing ciphertext
DataOutputStream output=new DataOutputStream (New FileOutputStream (Encrptor_text_file));
Create cipher for RSA
Cipher rsacipher=cipher.getinstance ("rsa/ecb/pkcs1padding");
Rsacipher.init (Cipher.encrypt_mode,publickey);
Create session key (Rijndael)
Keygenerator rijndaelkeygenerator=keygenerator.getinstance ("Rijndael");
Rijndaelkeygenerator.init (256);
Key Rijndaelkey=rijndaelkeygenerator.generatekey ();
Public Key Cryptography Session key
Byte[] Encodedkeybytes=rsacipher.dofinal (rijndaelkey.getencoded ());
Output.writeint (encodedkeybytes.length);
Output.write (encodedkeybytes);
Generate IV Vectors
SecureRandom random=new securerandom ();
Byte[] Iv=new byte[16];
Random.nextbytes (iv);
Output.write (iv);
Encrypting body
Ivparameterspec spec=new Ivparameterspec (iv);
Cipher symmetriccipher=cipher.getinstance ("rijndael/cbc/pkcs5padding");
Symmetriccipher.init (CIPHER.ENCRYPT_MODE,RIJNDAELKEY,SPEC);
CipherOutputStream cos=new CipherOutputStream (output,symmetriccipher);
FileInputStream input=new FileInputStream (text_file);
int thebyte=0;
while ((Thebyte=input.read ())!=-1) {
Cos.write (Thebyte);
}
Input.close ();
Cos.close ();
Return
}
Get the private key
private static byte[] Passworddecrypt (char[] password,byte[] ciphertext)
Throws exception{
Byte[] Salt=new byte[8];
Bytearrayinputstream bais=new Bytearrayinputstream (ciphertext);
Bais.read (salt,0,8);
Byte[] Remainingciphertext=new byte[ciphertext.length-8];
Bais.read (remainingciphertext,0,ciphertext.length-8);
Pbekeyspec keyspec=new pbekeyspec (password);
Secretkeyfactory keyfactory=secretkeyfactory.getinstance ("PBEWITHSHAANDTWOFISH-CBC");
Secretkey Key=keyfactory.generatesecret (KEYSPEC);
Pbeparameterspec paramspec=new Pbeparameterspec (salt,iterations);
Cipher cipher=cipher.getinstance ("PBEWITHSHAANDTWOFISH-CBC");
Cipher.init (CIPHER.DECRYPT_MODE,KEY,PARAMSPEC);
Return cipher.dofinal (Remainingciphertext);
}
Decrypting an encrypted file
public static void Decrypt ()
Throws exception{
FileInputStream fis=new FileInputStream (encrypt_privatekey_file);
Bytearrayoutputstream baos=new Bytearrayoutputstream ();
int thebyte=0;
while ((Thebyte=fis.read ())!=-1) {
Baos.write (Thebyte);
}
Fis.close ();
Get the encrypted private key
Byte[] Keybytes=baos.tobytearray ();
Baos.close ();
Get the private key
Byte[] Skey=passworddecrypt (Password.tochararray (), keybytes);
Generate RSA private key
Pkcs8encodedkeyspec keyspec=new Pkcs8encodedkeyspec (SKey);
Keyfactory keyfactory=keyfactory.getinstance ("RSA");
Privatekey privatekey=keyfactory.generateprivate (KEYSPEC);
Cipher rsacipher=cipher.getinstance ("rsa/ecb/pkcs1padding");
DataInputStream dis=new DataInputStream (New FileInputStream (Encrptor_text_file));
Password length and password in read cipher text
Byte[] Encryptedkeybytes=new byte[dis.readint ()];
Dis.readfully (encryptedkeybytes);
Rsacipher.init (Cipher.decrypt_mode,privatekey);
Byte[] Rijdaelkeybytes=rsacipher.dofinal (encryptedkeybytes);
Get session key
Secretkey rijndaelkey=new Secretkeyspec (rijdaelkeybytes, "Rijndael");
Byte[] Iv=new byte[16];
Dis.read (iv);
Ivparameterspec spec=new Ivparameterspec (iv);
To decrypt the body with a session key
Cipher cipher=cipher.getinstance ("rijndael/cbc/pkcs5padding");
Cipher.init (CIPHER.DECRYPT_MODE,RIJNDAELKEY,SPEC);
CipherInputStream cis=new CipherInputStream (dis,cipher);
FileOutputStream fos=new FileOutputStream (dencryptor_text_file);
thebyte=0;
while ((Thebyte=cis.read ())!=-1) {
Fos.write (Thebyte);
}
Cis.close ();
Fos.close ();
Return
}
public static void Main (string[] args) throws exception{
CreateKey ();
Encrypt ();
Decrypt ();
}
}