X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2FCryptUtils.java;h=681692a00e6323531b1dae680bf5d0b774c8190a;hb=272f5c86957c3caf4b3ed7d59448573914b7b33f;hp=8d39ccfa9e5a15d4e7db1e4650987464ea0cc973;hpb=b1ed544b1e56261ddcd02952bd6e9522f3f75258;p=nikiroo-utils.git diff --git a/src/be/nikiroo/utils/CryptUtils.java b/src/be/nikiroo/utils/CryptUtils.java index 8d39ccf..681692a 100644 --- a/src/be/nikiroo/utils/CryptUtils.java +++ b/src/be/nikiroo/utils/CryptUtils.java @@ -1,12 +1,16 @@ package be.nikiroo.utils; import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; +import javax.crypto.CipherInputStream; +import javax.crypto.CipherOutputStream; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import javax.crypto.SecretKey; @@ -38,7 +42,11 @@ public class CryptUtils { */ public CryptUtils(String key) { try { - init(key2key(key)); + byte[] bytes32 = key2key(key); + init(bytes32); + for (int i = 0 ; i < bytes32.length ; i++) { + bytes32[i] = 0; + } } catch (InvalidKeyException e) { // We made sure that the key is correct, so nothing here e.printStackTrace(); @@ -58,6 +66,57 @@ public class CryptUtils { */ public CryptUtils(byte[] bytes32) throws InvalidKeyException { init(bytes32); + for (int i = 0 ; i < bytes32.length ; i++) { + bytes32[i] = 0; + } + } + + /** + * Wrap the given {@link InputStream} so it is transparently encrypted by + * the current {@link CryptUtils}. + * + * @param in + * the {@link InputStream} to wrap + * @return the auto-encode {@link InputStream} + */ + public InputStream encryptInputStream(InputStream in) { + return new CipherInputStream(in, ecipher); + } + + /** + * Wrap the given {@link OutputStream} so it is transparently encrypted by + * the current {@link CryptUtils}. + * + * @param in + * the {@link OutputStream} to wrap + * @return the auto-encode {@link OutputStream} + */ + public OutputStream encryptOutpuStream(OutputStream out) { + return new CipherOutputStream(out, ecipher); + } + + /** + * Wrap the given {@link OutStream} so it is transparently decoded by the + * current {@link CryptUtils}. + * + * @param in + * the {@link InputStream} to wrap + * @return the auto-decode {@link InputStream} + */ + public InputStream decryptInputStream(InputStream in) { + return new CipherInputStream(in, dcipher); + } + + /** + * Wrap the given {@link OutStream} so it is transparently decoded by the + * current {@link CryptUtils}. + * + * @param out + * the {@link OutputStream} to wrap + * @return the auto-decode {@link OutputStream} + */ + public OutputStream decryptOutputStream(OutputStream out) { + return new CipherOutputStream(out, dcipher); } /** @@ -215,6 +274,28 @@ public class CryptUtils { } } + /** + * Decode the data which is assumed to be encrypted with the same utilities + * and to be a {@link String}. + * + * @param data + * the encrypted data to decode + * + * @return the original, decoded data,as a {@link String} + * + * @throws SSLException + * in case of I/O error + */ + public String decrypts(byte[] data) throws SSLException { + try { + return new String(decrypt(data), "UTF-8"); + } catch (UnsupportedEncodingException e) { + // UTF-8 is required in all confirm JVMs + e.printStackTrace(); + return null; + } + } + /** * Decode the data which is assumed to be encrypted with the same utilities * and is a Base64 encoded value.