/ ************** Run this program You need to download JCE, Bounce Castle's JCE with Lightweight API network stop is http://www.bouncecastle.org configuration: in Windows In the middle, you need to copy the downloaded bcprov-jdk14-119.jar file to two places: a JDK directory you install, for example, I am C: /J2SDK1.4.0-RC/JRE/LIB/EXT One in your JDK running environment, I am in C: / Program files / java / j2re1.4.0-rc / lib / ext; also modify two java.securities: I am in 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 = in java.security Org.bouncecastle.jce.Provider.bouncecastleProvider If everything goes well, you can run this program. The program has a file add-in-density feature. You need the data you specified, and the interface has been given. For example, if you specify the file name "4.txt" to encrypt, "6.txt" after the encrypted file storage, and the password password, such as "liufeng", run the program, then "6.txt" It is a ciphertext of "4.txt". Note that the password is the key to decrypt, don't forget. Other decryption processes have a reference. This program uses session key encryption to provide many interfaces. If you need an encryption process in your project, you can improve your use * /
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 utions = 1000; // The number of calculations is used in the salt static byte [] publickeyBytes; // public key private static byte [] privatekeyBytes; // private key private static string sessionkey; // Session secret Key Public Static String Encrypt_PrivateKey_File = "1.txt"; // This file places the encrypted private key private static string text_file = "4.txt"; // To encrypted file private static string encrptor_text_file = "5.txt"; / / Encrypted file private static string Dencryptor_text_file = "6.txt"; // Decryption file private static string password = "12345678"; // password is used to encrypt 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;} pu blic 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 secretKeypublic static void createKey () throws Exception {KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance ( "RSA"); keyPairGenerator.initialize (1024); KeyPair keyPair = keyPairGenerator.genKeyPair (); // get the public key byte array publicKeyBytes = keyPair.getPublic () getEncoded (). ; // Get 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 ();} // private key encryption private static byte [] passwordEncrypt (password given by Char [] password, byte [] privatekeybytes) throws exception {// create 8 byte salt byte [] salt = new byte [8]; random.nextbytes (salt); // Create a PBE key and cipherPBEKeySpec 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 cipherprikeybook baos = new byteArrayoutputStream (); baos.write (salt); baos.write (cipherprikey); return baos.tobyteaRay ();} // Encrypted by session key Piece, then encrypt the session key with the public key, and store it in the file // Last encrypted file by key length encrypted key (session key) ciphertic public static void encrypt () throws exception { // converted into the RSA key X509EncodedKeySpec keySpec = new X509EncodedKeySpec (publicKeyBytes); KeyFactory keyFactory = KeyFactory.getInstance ( "RSA"); PublicKey publickey = keyFactory.generatePublic (keySpec); // open the file stored ciphertext DataOutputStream output = new DataOutputStream (new FileOutputStream (ENCRPTOR_TEXT_FILE)); // create the RSA CIpherCipher rsaCipher = Cipher.getInstance ( "RSA / ECB / PKCS1Padding"); rsaCipher.init (Cipher.ENCRYPT_MODE, publickey); // create a session key (Rijndael KeyGenerator rijndaelkeygenerator = keygenerator.getInstance ("rijndael");
rijndaelKeyGenerator.init (256); Key rijndaelKey = rijndaelKeyGenerator.generateKey (); // public key encrypted session key byte [] encodedKeyBytes = rsaCipher.doFinal (rijndaelKey.getEncoded ()); output.writeInt (encodedKeyBytes.length); output .write (endedKeybytes); // Generate IV vector securerandom random = new securerandom (); byte [] iv = new byte [16]; random.nextBytes (iv); output.write (iv); // encryption body Ivarameterspec 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;} 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 the encrypted file public static void decrypt () throws Exception {FileInputStream fis = new FileInputStream (ENCRYPT_PRIVATEKEY_FILE); ByteArrayOutputStream baos = new ByteArrayOutputStream (); int theByte =