Skip to content

Commit 30e5195

Browse files
author
Ryan
committed
Debug encryption and decryption of files.
1 parent 0858b17 commit 30e5195

File tree

4 files changed

+81
-30
lines changed

4 files changed

+81
-30
lines changed

build.gradle

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ buildscript {
66
support_version = '25.4.0'
77
kotlin_version = '1.1.3'
88
anko_version = '0.10.1'
9+
bintray_version = '1.7.3'
10+
maven_version = '1.5'
911
}
1012
repositories {
1113
google()
@@ -16,6 +18,9 @@ buildscript {
1618
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
1719
classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
1820

21+
classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:$bintray_version"
22+
classpath "com.github.dcendents:android-maven-gradle-plugin:$maven_version"
23+
1924
// NOTE: Do not place your application dependencies here; they belong
2025
// in the individual module build.gradle files
2126
}

easycrypt/build.gradle

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,29 @@
11
apply plugin: 'com.android.library'
2+
3+
ext {
4+
bintrayRepo = 'maven'
5+
bintrayName = 'easy-crypt'
6+
7+
publishedGroupId = 'com.pvryan.easycrypt'
8+
libraryName = 'EasyCrypt'
9+
artifact = 'easy-crypt'
10+
11+
libraryDescription = 'Library to easily encrypt, decrypt, and hash any type of data.'
12+
13+
siteUrl = 'https://github.com/ryan652/EasyCrypt'
14+
gitUrl = 'https://github.com/ryan652/EasyCrypt.git'
15+
16+
libraryVersion = '1.0.1-beta'
17+
18+
developerId = 'ryan652'
19+
developerName = 'Priyank Vasa'
20+
developerEmail = 'pv.ryan14@gmail.com'
21+
22+
licenseName = 'The Apache Software License, Version 2.0'
23+
licenseUrl = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
24+
allLicenses = ["Apache-2.0"]
25+
}
26+
227
apply plugin: 'kotlin-android'
328

429
apply plugin: 'kotlin-android-extensions'
@@ -39,3 +64,6 @@ dependencies {
3964
repositories {
4065
mavenCentral()
4166
}
67+
68+
apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/installv1.gradle'
69+
apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/bintrayv1.gradle'

easycrypt/src/main/java/com/pvryan/easycrypt/ECrypt.kt

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import java.io.File
2424
import java.io.FileInputStream
2525
import java.io.IOException
2626
import java.io.InputStream
27+
import java.security.InvalidKeyException
2728
import java.security.InvalidParameterException
2829
import java.security.MessageDigest
2930
import java.security.SecureRandom
@@ -77,7 +78,9 @@ class ECrypt : AnkoLogger {
7778
fun <T> encrypt(input: T, password: String, erl: ECryptResultListener,
7879
outputFile: File = File(DEF_ENCRYPTED_FILE_PATH)) {
7980
doAsync {
80-
81+
if (password.trim().isNullOrBlank()) {
82+
erl.onFailure("Password is null or blank.", InvalidKeyException())
83+
}
8184
when (input) {
8285
is String -> {
8386
encrypt(input.toByteArray(), password, erl, outputFile)
@@ -142,28 +145,33 @@ class ECrypt : AnkoLogger {
142145
}
143146

144147
is InputStream -> {
148+
if (outputFile.exists()) {
149+
if (outputFile.absolutePath != DEF_ENCRYPTED_FILE_PATH) {
150+
erl.onFailure(MSG_OUTPUT_FILE_EXISTS,
151+
FileAlreadyExistsException(outputFile))
152+
return@doAsync
153+
}
154+
outputFile.delete()
155+
}
156+
outputFile.createNewFile()
157+
145158
val fos = outputFile.outputStream()
146159
var cos = CipherOutputStream(fos, cipher)
147-
try {
148-
if (outputFile.exists()) {
149-
if (outputFile.absolutePath != DEF_ENCRYPTED_FILE_PATH) {
150-
erl.onFailure(MSG_OUTPUT_FILE_EXISTS,
151-
FileAlreadyExistsException(outputFile))
152-
return@doAsync
153-
}
154-
outputFile.delete()
155-
}
156-
outputFile.createNewFile()
157160

161+
try {
158162
fos.write(iv)
159163
fos.write(salt)
160164
cos = CipherOutputStream(fos, cipher)
161165

162166
val buffer = ByteArray(8192)
163-
var wrote = 0
164-
while ({ wrote = input.read(buffer); wrote }() > 0) {
165-
cos.write(buffer)
166-
erl.onProgress(wrote)
167+
if (input.available() <= buffer.size) {
168+
cos.write(input.readBytes())
169+
} else {
170+
var wrote = 0
171+
while ({ wrote = input.read(buffer); wrote }() > 0) {
172+
cos.write(buffer)
173+
erl.onProgress(wrote)
174+
}
167175
}
168176

169177
erl.onSuccess(outputFile as T)
@@ -185,7 +193,9 @@ class ECrypt : AnkoLogger {
185193
fun <T> decrypt(input: T, password: String, erl: ECryptResultListener,
186194
outputFile: File = File(DEF_DECRYPTED_FILE_PATH)) {
187195
doAsync {
188-
196+
if (password.trim().isNullOrBlank()) {
197+
erl.onFailure("Password is null or blank.", InvalidKeyException())
198+
}
189199
when (input) {
190200

191201
is String -> {
@@ -259,19 +269,21 @@ class ECrypt : AnkoLogger {
259269
}
260270

261271
is InputStream -> {
272+
273+
if (outputFile.exists()) {
274+
if (outputFile.absolutePath != DEF_DECRYPTED_FILE_PATH) {
275+
erl.onFailure(MSG_OUTPUT_FILE_EXISTS,
276+
FileAlreadyExistsException(outputFile))
277+
return@doAsync
278+
}
279+
outputFile.delete()
280+
}
281+
outputFile.createNewFile()
282+
262283
val cis = CipherInputStream(input, cipher)
263284
val fos = outputFile.outputStream()
264-
try {
265-
if (outputFile.exists()) {
266-
if (outputFile.absolutePath != DEF_DECRYPTED_FILE_PATH) {
267-
erl.onFailure(MSG_OUTPUT_FILE_EXISTS,
268-
FileAlreadyExistsException(outputFile))
269-
return@doAsync
270-
}
271-
outputFile.delete()
272-
}
273-
outputFile.createNewFile()
274285

286+
try {
275287
val iv = ByteArray(cipher.blockSize)
276288
val salt = ByteArray(SALT_BYTES_LENGTH)
277289

@@ -292,10 +304,14 @@ class ECrypt : AnkoLogger {
292304
cipher.init(Cipher.DECRYPT_MODE, key, ivParams)
293305

294306
val buffer = ByteArray(8192)
295-
var wrote = 0
296-
while ({ wrote = cis.read(buffer); wrote }() > 0) {
297-
fos.write(buffer)
298-
erl.onProgress(wrote)
307+
if (cis.available() <= buffer.size) {
308+
fos.write(cis.readBytes())
309+
} else {
310+
var wrote = 0
311+
while ({ wrote = cis.read(buffer); wrote }() > 0) {
312+
fos.write(buffer)
313+
erl.onProgress(wrote)
314+
}
299315
}
300316

301317
erl.onSuccess(outputFile as T)

easycrypt/src/main/java/com/pvryan/easycrypt/extensions/DataTypeExtensions.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package com.pvryan.easycrypt.extensions
1717

1818
import android.util.Base64
1919
import com.pvryan.easycrypt.ECrypt
20+
import java.security.spec.InvalidKeySpecException
2021
import java.util.regex.Pattern
2122
import javax.crypto.SecretKeyFactory
2223
import javax.crypto.spec.PBEKeySpec
@@ -59,6 +60,7 @@ fun String.hexToByteArray(): ByteArray {
5960
return data
6061
}
6162

63+
@Throws(InvalidKeySpecException::class)
6264
fun ECrypt.getKey(password: String = String(), salt: ByteArray): SecretKeySpec {
6365

6466
val pbeKeySpec: PBEKeySpec = PBEKeySpec(

0 commit comments

Comments
 (0)