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.PublicKey; 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.codec.binary.Base64; import org.apache.commons.io.IOUtils; import org.picketlink.pki.key.EncryptionAuthority; /** * @author Giriraj Sharma * @since December 2, 2014 */ @ApplicationScoped public class DefaultEncryptionAuthority implements EncryptionAuthority { private static final long serialVersionUID = 1L; @Override public String encrypt(String rawText, PublicKey publicKey, String transformation, String encoding) { String encryptedText = null; try { Cipher cipher = Cipher.getInstance(transformation); cipher.init(Cipher.ENCRYPT_MODE, publicKey); encryptedText = new String(Base64.encodeBase64(cipher.doFinal(rawText.getBytes(encoding)))); } catch (Exception e) { throw new RuntimeException("Could not encrypt rawText.", e); } return encryptedText; } @Override public String encrypt(String rawText, String publicKeyPath, String transformation, String encoding) { String encryptedText = null; try { X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec( IOUtils.toByteArray(new FileInputStream(publicKeyPath))); Cipher cipher = Cipher.getInstance(transformation); cipher.init(Cipher.ENCRYPT_MODE, KeyFactory.getInstance("RSA").generatePublic(x509EncodedKeySpec)); encryptedText = new String(Base64.encodeBase64(cipher.doFinal(rawText.getBytes(encoding)))); } catch (Exception e) { throw new RuntimeException("Could not encrypt rawText.", e); } return encryptedText; } @Override public File encryptFile(String inputFilePath, PublicKey publicKey, String transformation, String encoding) { // TODO : Specify a valid encryptedFilePath or receive it as an argument File encryptedFile = null; try { String inputRawText = readFileAsString(inputFilePath); Cipher cipher = Cipher.getInstance(transformation); cipher.init(Cipher.ENCRYPT_MODE, publicKey); String ecncryptedText = new String( Base64.encodeBase64(cipher.doFinal(inputRawText.getBytes(encoding)))); encryptedFile = new File("encryptedFilePath"); BufferedWriter out = new BufferedWriter(new FileWriter(encryptedFile)); out.write(ecncryptedText); out.close(); } catch (Exception e) { throw new RuntimeException("Could not encrypt inputFile.", e); } return encryptedFile; } @Override public File encryptFile(String inputFilePath, String publicKeyPath, String transformation, String encoding) { // TODO : Specify a valid encryptedFilePath or receive it as an argument File encryptedFile = null; try { String inputRawText = readFileAsString(inputFilePath); X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec( IOUtils.toByteArray(new FileInputStream(publicKeyPath))); Cipher cipher = Cipher.getInstance(transformation); cipher.init(Cipher.ENCRYPT_MODE, KeyFactory.getInstance("RSA").generatePublic(x509EncodedKeySpec)); String encryptedText = new String(Base64.encodeBase64(cipher.doFinal(inputRawText.getBytes(encoding)))); encryptedFile = new File("encryptedFilePath"); BufferedWriter out = new BufferedWriter(new FileWriter(encryptedFile)); out.write(encryptedText); out.close(); } catch (Exception e) { throw new RuntimeException("Could not encrypt inputFile.", e); } return encryptedFile; } 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(); } }