JET WORKSgit add .!

This commit is contained in:
vanitasvitae 2017-07-31 14:21:49 +02:00
parent d2f979ed15
commit 422660e9fe
Signed by: vanitasvitae
GPG Key ID: 62BEE9264BF17311
3 changed files with 16 additions and 20 deletions

View File

@ -25,7 +25,7 @@ import javax.crypto.Cipher;
import javax.crypto.KeyGenerator; import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException; import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey; import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.SecretKeySpec; import javax.crypto.spec.SecretKeySpec;
public abstract class AesGcmNoPadding { public abstract class AesGcmNoPadding {
@ -54,10 +54,10 @@ public abstract class AesGcmNoPadding {
System.arraycopy(key, 0, keyAndIv, 0, bytes); System.arraycopy(key, 0, keyAndIv, 0, bytes);
System.arraycopy(iv, 0, keyAndIv, bytes, bytes); System.arraycopy(iv, 0, keyAndIv, bytes, bytes);
SecretKey secretKey = new SecretKeySpec(key, keyType);
IvParameterSpec ivSpec = new IvParameterSpec(iv);
cipher = Cipher.getInstance(cipherMode, "BC"); cipher = Cipher.getInstance(cipherMode, "BC");
cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivSpec); SecretKey keySpec = new SecretKeySpec(key, keyType);
GCMParameterSpec gcmSpec = new GCMParameterSpec(128, iv);
cipher.init(Cipher.ENCRYPT_MODE, keySpec, gcmSpec);
} }
public AesGcmNoPadding(byte[] key, byte[] iv) throws NoSuchPaddingException, NoSuchAlgorithmException, public AesGcmNoPadding(byte[] key, byte[] iv) throws NoSuchPaddingException, NoSuchAlgorithmException,
@ -72,8 +72,8 @@ public abstract class AesGcmNoPadding {
cipher = Cipher.getInstance(cipherMode, "BC"); cipher = Cipher.getInstance(cipherMode, "BC");
SecretKeySpec keySpec = new SecretKeySpec(key, keyType); SecretKeySpec keySpec = new SecretKeySpec(key, keyType);
IvParameterSpec ivSpec = new IvParameterSpec(iv); GCMParameterSpec gcmSpec = new GCMParameterSpec(128, iv);
cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec); cipher.init(Cipher.DECRYPT_MODE, keySpec, gcmSpec);
} }
public byte[] getKeyAndIv() { public byte[] getKeyAndIv() {

View File

@ -72,30 +72,25 @@ public class JingleIncomingFileOffer extends AbstractJingleFileOffer<RemoteFile>
inputStream = bytestreamSession.getInputStream(); inputStream = bytestreamSession.getInputStream();
outputStream = new FileOutputStream(mFile); outputStream = new FileOutputStream(mFile);
byte[] filebuf = new byte[(int) file.getSize()]; int length = 0;
int read = 0; int read = 0;
byte[] bufbuf = new byte[4096]; byte[] bufbuf = new byte[4096];
LOGGER.log(Level.INFO, "Begin receiving bytes."); while ((length = inputStream.read(bufbuf)) >= 0) {
while (read < filebuf.length) { outputStream.write(bufbuf, 0, length);
int r = inputStream.read(bufbuf); read += length;
if (r >= 0) { LOGGER.log(Level.INFO, "Read " + read + " (" + length + ") of " + file.getSize() + " bytes.");
System.arraycopy(bufbuf, 0, filebuf, read, r); if (read == (int) file.getSize()) {
read += r;
LOGGER.log(Level.INFO, "Read " + r + " (" + read + " of " + filebuf.length + ") bytes.");
} else {
break; break;
} }
} }
LOGGER.log(Level.INFO, "Reading/Writing finished.");
outputStream.write(filebuf);
outputStream.flush();
} catch (IOException e) { } catch (IOException e) {
LOGGER.log(Level.SEVERE, "Cannot get InputStream from BytestreamSession: " + e, e); LOGGER.log(Level.SEVERE, "Cannot get InputStream from BytestreamSession: " + e, e);
} finally { } finally {
if (inputStream != null) { if (inputStream != null) {
try { try {
inputStream.close(); inputStream.close();
LOGGER.log(Level.INFO, "CipherInputStream closed.");
} catch (IOException e) { } catch (IOException e) {
LOGGER.log(Level.SEVERE, "Could not close InputStream: " + e, e); LOGGER.log(Level.SEVERE, "Could not close InputStream: " + e, e);
} }
@ -104,12 +99,12 @@ public class JingleIncomingFileOffer extends AbstractJingleFileOffer<RemoteFile>
if (outputStream != null) { if (outputStream != null) {
try { try {
outputStream.close(); outputStream.close();
LOGGER.log(Level.INFO, "FileOutputStream closed.");
} catch (IOException e) { } catch (IOException e) {
LOGGER.log(Level.SEVERE, "Could not close OutputStream: " + e, e); LOGGER.log(Level.SEVERE, "Could not close OutputStream: " + e, e);
} }
} }
} }
notifyProgressListenersFinished(); notifyProgressListenersFinished();
} }

View File

@ -61,6 +61,7 @@ public class JingleOutgoingFileOffer extends AbstractJingleFileOffer<LocalFile>
outputStream.write(fileBuf); outputStream.write(fileBuf);
outputStream.flush(); outputStream.flush();
outputStream.close();
} catch (IOException e) { } catch (IOException e) {
LOGGER.log(Level.SEVERE, "Exception while sending file: " + e, e); LOGGER.log(Level.SEVERE, "Exception while sending file: " + e, e);