Saving Data on Android


Cipher  and encrypt the data to be  stored and place the data, the salt and the iv into  HashMap



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

Cipher
 and encrypt the data to be 
stored and place the data, the salt and the iv into 
HashMap
 
map
 to be returned by 
the method.
// 13
 
val
 cipher = Cipher.getInstance(
"AES/CBC/PKCS7Padding"

cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec) 
// 14
 
val
 encrypted = cipher.doFinal(plainTextBytes) 
// 15
 
map[
"salt"
] = salt 
map[
"iv"
] = iv 
map[
"encrypted"
] = encrypted
Here’s the explanation:
13. Create and initialize the 
Cipher
 using 
AES/CBC/PKCS7Padding
. This specifies 
AES encryption with cypher block chaining. PKCS7 refers to an established 
standard for padding data that doesn’t fit into the specified block size.
14. Encrypt the bytes of the data with the 
cipher
.
15. Place the 
salt

iv
, and 
encrypted
 bytes into a 
HashMap
.
The IV and salt are considered public and can be stored with your data. However, 
make sure they are not reused or sequentially incremented.
Now the encrypt method can be used to encrypt the text of a note before that text is 
written to a file. To finish off the encryption of the file before storing it, insert the 
following code into 
addNote
:
if
 (isExternalStorageWritable()) { 
  ObjectOutputStream(noteFileOutputStream(note.fileName)).use 
{ output -> 
    output.writeObject(encrypt(note.noteText.toByteArray())) 
  } 
}
Saving Data on Android
Chapter 1: Using Files
raywenderlich.com
35


The code above creates an 
ObjectOutputStream
 with 
use
 and utilizes it to write the 
encrypted message out to the file. Before you run the app again, you have to change 
the instance of the 
NoteRepository
 in 
MainActivity.kt
 to this:
private
 
val
 repo: NoteRepository 
by
 lazy 
{ EncryptedFileRepository(
this
) }
Run the app and use it to write an encrypted file. If you then find and open the file 
with Device File Explorer, you’ll see that it’s filled with unreadable data because the 
file is encrypted.
Now you must add a mechanism to decrypt the file.

Download 19,28 Mb.

Do'stlaringiz bilan baham:
1   ...   34   35   36   37   38   39   40   41   ...   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