Java tutorial
/* * JBoss, Home of Professional Open Source. * Copyright 2012, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.picketlink.pki.internal; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileReader; import java.io.FileWriter; import java.security.KeyFactory; import java.security.PrivateKey; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; import javax.crypto.Cipher; import javax.enterprise.context.ApplicationScoped; import org.apache.commons.codec.binary.Base64; import org.apache.commons.io.IOUtils; import org.picketlink.pki.key.DecryptionAuthority; /** * @author Giriraj Sharma * @since December 2, 2014 */ @ApplicationScoped public class DefaultDecryptionAuthority implements DecryptionAuthority { private static final long serialVersionUID = 1L; @Override public String decrypt(String cipherText, PrivateKey privateKey, String transformation, String encoding) { String decryptedText = null; try { Cipher cipher = Cipher.getInstance(transformation); cipher.init(Cipher.DECRYPT_MODE, privateKey); decryptedText = new String(cipher.doFinal(Base64.decodeBase64(cipherText.getBytes())), encoding); } catch (Exception e) { throw new RuntimeException("Could not decrypt cipherText.", e); } return decryptedText; } @Override public String decrypt(String cipherText, String privateKeyPath, String transformation, String encoding) { String decryptedText = null; try { PKCS8EncodedKeySpec pKCS8EncodedKeySpec = new PKCS8EncodedKeySpec( IOUtils.toByteArray(new FileInputStream(privateKeyPath))); Cipher cipher = Cipher.getInstance(transformation); cipher.init(Cipher.DECRYPT_MODE, KeyFactory.getInstance("RSA").generatePrivate(pKCS8EncodedKeySpec)); decryptedText = new String(Base64.decodeBase64(cipher.doFinal(cipherText.getBytes(encoding)))); } catch (Exception e) { throw new RuntimeException("Could not decrypt cipherText.", e); } return decryptedText; } @Override public File decryptFile(String cipherFilePath, PrivateKey privateKey, String transformation, String encoding) { // TODO : Specify a valid decryptedFilePath or receive it as an argument File decryptedFile = null; try { String cipherText = readFileAsString(cipherFilePath); Cipher cipher = Cipher.getInstance(transformation); cipher.init(Cipher.DECRYPT_MODE, privateKey); String decryptedText = new String(cipher.doFinal(Base64.decodeBase64(cipherText.getBytes())), encoding); decryptedFile = new File("decryptedFilePath"); BufferedWriter out = new BufferedWriter(new FileWriter(decryptedFile)); out.write(decryptedText); out.close(); } catch (Exception e) { throw new RuntimeException("Could not decrypt cipherFile.", e); } return decryptedFile; } @Override public File decryptFile(String cipherFilePath, String privateKeyPath, String transformation, String encoding) { // TODO : Specify a valid decryptedFilePath or receive it as an argument File decryptedFile = null; try { String cipherText = readFileAsString(cipherFilePath); X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec( IOUtils.toByteArray(new FileInputStream(privateKeyPath))); Cipher cipher = Cipher.getInstance(transformation); cipher.init(Cipher.DECRYPT_MODE, KeyFactory.getInstance("RSA").generatePrivate(x509EncodedKeySpec)); String decryptedText = new String(Base64.decodeBase64(cipher.doFinal(cipherText.getBytes(encoding)))); decryptedFile = new File("decryptedFilePath"); BufferedWriter out = new BufferedWriter(new FileWriter(decryptedFile)); out.write(decryptedText); out.close(); } catch (Exception e) { throw new RuntimeException("Could not decrypt cipherFile.", e); } return decryptedFile; } public static String readFileAsString(String filePath) throws java.io.IOException { StringBuffer fileData = new StringBuffer(1000); BufferedReader reader = new BufferedReader(new FileReader(filePath)); char[] buf = new char[1024]; int numRead = 0; while ((numRead = reader.read(buf)) != -1) { String readData = String.valueOf(buf, 0, numRead); fileData.append(readData); buf = new char[1024]; } reader.close(); System.out.println(fileData.toString()); return fileData.toString(); } }