1 21 22 package net.sourceforge.jcetaglib.lib; 23 24 import net.sourceforge.jcetaglib.exceptions.CryptoException; 25 import org.bouncycastle.jce.provider.BouncyCastleProvider; 26 import org.bouncycastle.util.encoders.Base64; 27 28 import javax.crypto.Cipher; 29 import java.io.*; 30 import java.security.PrivateKey ; 31 import java.security.PublicKey ; 32 import java.security.Security ; 33 34 42 public class Asymmetric { 43 private static int BUFFERSIZE_TEXT = 64; 45 46 54 public static StringBuffer encrypt(StringBuffer text 55 , PublicKey encryptKey) 56 throws CryptoException { 57 58 ByteArrayOutputStream bao = null; 59 DataOutputStream dao = null; 60 61 try { 62 bao = new ByteArrayOutputStream(); 63 dao = new DataOutputStream(bao); 64 65 encrypt(new ByteArrayInputStream(text.toString().getBytes()), dao, encryptKey, BUFFERSIZE_TEXT); 67 68 return new StringBuffer (new String (Base64.encode(bao.toByteArray()))); 69 } catch (IOException ioe) { 70 ioe.printStackTrace(); 71 throw new CryptoException(ioe.getMessage()); 72 } finally { 73 if (dao != null) { 74 try { 76 dao.close(); 77 } catch (IOException e) { 78 ; 79 } 80 } 81 } 82 } 83 84 94 public static void encrypt(InputStream is 95 , DataOutputStream daos 96 , PublicKey encryptKey 97 , int bufferlength) 98 throws CryptoException, IOException { 99 100 Cipher cipher = null; 101 102 try { 103 Security.addProvider(new BouncyCastleProvider()); 104 105 cipher = Cipher.getInstance("RSA/ECB/OAEPPadding", "BC"); 107 108 cipher.init(Cipher.ENCRYPT_MODE, encryptKey); 109 110 byte[] buffer = new byte[bufferlength]; 111 int length = 0; 112 while ((length = is.read(buffer)) != -1) { 113 cipher.update(buffer, 0, length); 114 } 115 116 byte[] result = cipher.doFinal(); 117 daos.write(result); 118 } catch (IOException ioe) { 119 ioe.printStackTrace(); 120 throw new IOException(ioe.getMessage()); 121 } catch (Exception ex) { 122 ex.printStackTrace(); 123 throw new CryptoException(ex.getMessage()); 124 } 125 } 126 127 135 public static StringBuffer decrypt(StringBuffer text 136 , PrivateKey decryptKey) 137 throws CryptoException { 138 139 ByteArrayOutputStream bao = null; 140 DataOutputStream dao = null; 141 142 try { 143 bao = new ByteArrayOutputStream(); 144 dao = new DataOutputStream(bao); 145 146 decrypt(new ByteArrayInputStream(Base64.decode(text.toString())), dao, decryptKey, BUFFERSIZE_TEXT); 148 149 return new StringBuffer (new String (bao.toByteArray())); 150 } catch (IOException ioe) { 151 ioe.printStackTrace(); 152 throw new CryptoException(ioe.getMessage()); 153 } finally { 154 if (dao != null) { 155 try { 157 dao.close(); 158 } catch (IOException e) { 159 ; 160 } 161 } 162 } 163 } 164 165 175 public static void decrypt(InputStream is 176 , DataOutputStream daos 177 , PrivateKey decryptKey 178 , int bufferlength) 179 throws CryptoException, IOException { 180 181 Cipher cipher = null; 182 183 try { 184 Security.addProvider(new BouncyCastleProvider()); 185 186 cipher = Cipher.getInstance("RSA/ECB/OAEPPadding", "BC"); 188 189 cipher.init(Cipher.DECRYPT_MODE, decryptKey); 190 191 byte[] buffer = new byte[bufferlength]; 192 int length = 0; 193 while ((length = is.read(buffer)) != -1) { 194 cipher.update(buffer, 0, length); 195 } 196 197 byte[] result = cipher.doFinal(); 198 daos.write(result); 199 } catch (IOException ioe) { 200 ioe.printStackTrace(); 201 throw new IOException(ioe.getMessage()); 202 } catch (Exception ex) { 203 ex.printStackTrace(); 204 throw new CryptoException(ex.getMessage()); 205 } 206 } 207 } 208 | Popular Tags |