1 20 21 package net.sourceforge.lightcrypto; 22 23 import org.bouncycastle.crypto.engines.RC4Engine; 24 import org.bouncycastle.crypto.params.KeyParameter; 25 26 import java.io.DataOutputStream ; 27 import java.io.InputStream ; 28 29 37 38 public class Stream { 39 49 public static void encrypt( 50 InputStream is 51 , DataOutputStream daos 52 , SafeObject keybytes 53 , int bufferlength 54 ) throws CryptoException { 55 56 KeyParameter key = null; 57 58 try { 59 RC4Engine streamCipher = new RC4Engine(); 61 62 key = new KeyParameter(keybytes.getBytes()); 64 65 byte[] buffer = new byte[bufferlength]; 66 int length = bufferlength; 67 byte[] result = new byte[length]; 68 69 streamCipher.init(true, key); 71 72 while ((length = is.read(buffer)) != -1) { 74 streamCipher.processBytes(buffer, 0, length, result, 0); 75 daos.write(result, 0, length); 76 } 77 78 } catch (Exception ex) { 79 ex.printStackTrace(); 80 throw new CryptoException(ex.getMessage()); 81 } finally { 82 key = null; 84 } 85 } 86 87 96 public static void decrypt( 97 InputStream is 98 , DataOutputStream daos 99 , SafeObject keybytes 100 , int bufferlength 101 ) throws CryptoException { 102 KeyParameter key = null; 103 104 try { 105 key = new KeyParameter(keybytes.getBytes()); 107 108 RC4Engine streamCipher = new RC4Engine(); 110 111 byte[] buffer = new byte[bufferlength]; 112 int length = bufferlength; 113 byte[] result = new byte[length]; 114 115 streamCipher.init(false, key); 117 118 while ((length = is.read(buffer)) != -1) { 120 streamCipher.processBytes(buffer, 0, length, result, 0); 121 daos.write(result, 0, length); 122 } 123 124 } catch (Exception ex) { 125 ex.printStackTrace(); 126 throw new CryptoException(ex.getMessage()); 127 128 } finally { 129 key = null; 131 } 132 } 133 } 134 135 | Popular Tags |