Saving Data on Android


Password-Based Key Derivation



Download 19,28 Mb.
Pdf ko'rish
bet35/292
Sana24.01.2022
Hajmi19,28 Mb.
#407089
1   ...   31   32   33   34   35   36   37   38   ...   292
Bog'liq
Bailey J., Dominguez A., Djermanovic D. - Saving Data on Android (1st Edition) - 2019

Password-Based Key Derivation 
Function
 or 
PBKDF2
.
PBKDF2 produces a key from a password by hashing it over many times with 
salt

This creates a key of a sufficient length and complexity, and the derived key will be 
unique even if two or more users in the system used the same password.
Saving Data on Android
Chapter 1: Using Files
raywenderlich.com
32


In this example, a 
String
 that represents the user’s password has been hardcoded at 
the top of 
EncryptedFileRepository.kt

val passwordString = "Swordfish"
.
Find 
encrypt
 and add the code inside the empty 
try
 block, replacing the 
TODO
:
// 1
 
val
 random = SecureRandom() 
// 2
 
val
 salt = ByteArray(
256

// 3
 
random.nextBytes(salt)
Here’s how it works:
1. Generate a random value using the 
SecureRandom
 class. This guarantees the 
output is difficult to predict as 
SecureRandom
 is a cryptograpically strong 
random number generator.
2. Create a 
ByteArray
 of 256 bytes to store the 
salt
.
3. Pass the salt to 
nextBytes
 which will fill the array with 256 random bytes.
Next, add the following code to the 
random.nextBytes
 call to 
salt
 the password.
// 4
 
val
 passwordChar = passwordString.toCharArray()  
// 5
 
val
 pbKeySpec = PBEKeySpec(passwordChar, salt, 
1324

256

//1324 
iterations
 
// 6
 
val
 secretKeyFactory = 
SecretKeyFactory.getInstance(
"PBKDF2WithHmacSHA1"

// 7
 
val
 keyBytes = 
secretKeyFactory.generateSecret(pbKeySpec).encoded 
// 8
 
val
 keySpec = SecretKeySpec(keyBytes, 
"AES"
)
Here’s how it works:
4. Convert the password into a character array.
5. Pass the password in 
char[]
 form, along with the salt, to 
PBEKeySpec
, as well as 
the number of iterations
1324
, and the size of the key, 
256
. Increasing the 
number of iterations also increases the time it would take to operate on a set of 
keys during a brute-force attack.
Saving Data on Android
Chapter 1: Using Files
raywenderlich.com
33


6. Generate an instance of a 
SecretKeyFactory
 using 

Download 19,28 Mb.

Do'stlaringiz bilan baham:
1   ...   31   32   33   34   35   36   37   38   ...   292




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish