JAVA RSA encrypt file amongst world cardinal using bouncy castle Crypto APIs

The next sample code encrypts a file using RSA world key. You tin flame transcend earth cardinal file name, input file mention to encrypt together with file mention to incorporate hex encoded encrypted data. This programme is real handy when it comes to encrypting xml file or text file. Before yous tin flame piece of job the programme yous need to install the next jars inwards your classpath. Learn RSA Encryption together with Decryption Basics

TIP: Get jars based on your coffee installation

package com.as400samplecode;  import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.security.Security;  import org.bouncycastle.crypto.AsymmetricBlockCipher; import org.bouncycastle.crypto.engines.RSAEngine; import org.bouncycastle.crypto.params.AsymmetricKeyParameter; import org.bouncycastle.crypto.util.PublicKeyFactory;  import sun.misc.BASE64Decoder;  world shape RSAEncryptFile {      world static void main(String[] args)     {          String publicKeyFilename = null;         String inputFilename = null;         String encryptedFilename = null;          RSAEncryptFile rsaEncryptFile = novel RSAEncryptFile();          if (args.length < 3)         {             System.err.println("Usage: coffee "+ rsaEncryptFile.getClass().getName()+             " Public_Key_Filename Input_Filename Encrypted_Filename");             System.exit(1);         }          publicKeyFilename = args[0].trim();         inputFilename = args[1].trim();         encryptedFilename = args[2].trim();         rsaEncryptFile.encrypt(publicKeyFilename, inputFilename, encryptedFilename);      }      mortal void encrypt (String publicKeyFilename, String inputFilename, String encryptedFilename){          seek {              Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());              String value = "";             String cardinal = readFileAsString(publicKeyFilename);             BASE64Decoder b64 = novel BASE64Decoder();             AsymmetricKeyParameter publicKey =                  (AsymmetricKeyParameter) PublicKeyFactory.createKey(b64.decodeBuffer(key));             AsymmetricBlockCipher e = novel RSAEngine();             e = novel org.bouncycastle.crypto.encodings.PKCS1Encoding(e);             e.init(true, publicKey);              String inputdata = readFileAsString(inputFilename);             byte[] messageBytes = inputdata.getBytes();             int i = 0;             int len = e.getInputBlockSize();             field (i < messageBytes.length)             {                 if (i + len > messageBytes.length)                     len = messageBytes.length - i;                  byte[] hexEncodedCipher = e.processBlock(messageBytes, i, len);                 value = value + getHexString(hexEncodedCipher);                 i += e.getInputBlockSize();             }              System.out.println(value);             BufferedWriter out = novel BufferedWriter(new FileWriter(encryptedFilename));             out.write(value);             out.close();                     }         select grip of (Exception e) {             System.out.println(e);         }     }      world static String getHexString(byte[] b) throws Exception {         String consequence = "";         for (int i=0; i < b.length; i++) {             consequence +=                 Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring( one );         }         render result;     }      mortal static String readFileAsString(String filePath)     throws java.io.IOException{         StringBuffer fileData = novel StringBuffer(1000);         BufferedReader reader = novel BufferedReader(                 novel FileReader(filePath));         char[] buf = novel char[1024];         int numRead=0;         while((numRead=reader.read(buf)) != -1){             String readData = String.valueOf(buf, 0, numRead);             fileData.append(readData);             buf = novel char[1024];         }         reader.close();         System.out.println(fileData.toString());         render fileData.toString();     }  } 


Sumber http://www.mysamplecode.com/