-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCipherExtensions.kt
98 lines (82 loc) · 3.33 KB
/
CipherExtensions.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package co.temy.android.ktx
import android.util.Base64
import java.security.Key
import javax.crypto.Cipher
import javax.crypto.spec.IvParameterSpec
/**
* Initializes Cipher [instance] for encryption and encrypts data using the key.
*
* @param data the data to encrypt
* @param key the key to encrypt data with
* @param useInitializationVector encrypt data using initialization vector generated by system. Vector will be added
* as a prefix to the result of encryption, separated with [IV_SEPARATOR]. Result example - "aaaaaa]bbbbbb", where `aaaaaa`
* is an created Initialization Vector and `bbbbbb` the encrypted to data. `false` by default.
*
*/
fun Cipher.encrypt(data: String, key: Key?, useInitializationVector: Boolean = false): String {
init(Cipher.ENCRYPT_MODE, key)
var result = ""
if (useInitializationVector) {
val ivString = Base64.encodeToString(iv, Base64.DEFAULT)
result = ivString + IV_SEPARATOR
}
val bytes = doFinal(data.toByteArray())
result += Base64.encodeToString(bytes, Base64.DEFAULT)
return result
}
/**
* Initializes Cipher [instance] for decryption and decrypts data using the key.
*
* @param data the data to decrypt
* @param key the key to decrypt data with
* @param useInitializationVector decrypt data using initialization vector. Vector must be added
* as a prefix to the encryption data, separated with [IV_SEPARATOR]. Data example - "aaaaaa]bbbbbb", where `aaaaaa`
* is an Initialization Vector and `bbbbbb` the data to decrypt. `false` by default.
*/
fun Cipher.decrypt(data: String, key: Key?, useInitializationVector: Boolean = false): String {
val encodedString: String
if (useInitializationVector) {
val split = data.split(IV_SEPARATOR.toRegex())
if (split.size != 2) throw IllegalArgumentException("Passed data is incorrect. There was no IV specified with it.")
val ivString = split[0]
encodedString = split[1]
val ivSpec = IvParameterSpec(Base64.decode(ivString, Base64.DEFAULT))
init(Cipher.DECRYPT_MODE, key, ivSpec)
} else {
encodedString = data
init(Cipher.DECRYPT_MODE, key)
}
val encryptedData = Base64.decode(encodedString, Base64.DEFAULT)
val decodedData = doFinal(encryptedData)
return String(decodedData)
}
/**
* Initializes Cipher [instance] for wrapping and wraps(encrypts) a key with another key.
*/
fun Cipher.wrapKey(keyToBeWrapped: Key, keyToWrapWith: Key?): String {
init(Cipher.WRAP_MODE, keyToWrapWith)
val decodedData = wrap(keyToBeWrapped)
return Base64.encodeToString(decodedData, Base64.DEFAULT)
}
/**
* Initializes Cipher [instance] for unwrapping and unwraps(decrypts) a key with another key.
* Requires wrapped key algorithm and type.
*/
fun Cipher.unWrapKey(wrappedKeyData: String, algorithm: String, wrappedKeyType: Int, keyToUnWrapWith: Key?): Key {
val encryptedKeyData = Base64.decode(wrappedKeyData, Base64.DEFAULT)
init(Cipher.UNWRAP_MODE, keyToUnWrapWith)
return unwrap(encryptedKeyData, algorithm, wrappedKeyType)
}
/**
* For key wrapping
*/
const val TRANSFORMATION_RSA = "RSA/ECB/PKCS1Padding"
/**
* For file encryption / decryption
*/
const val TRANSFORMATION_AES_FILE = "AES/CTR/NoPadding"
/**
* For data encryption / decryption
*/
const val TRANSFORMATION_AES_DATA = "AES/CBC/PKCS7Padding"
const val IV_SEPARATOR = "]"