// ĠCopyright Ġ( c ) ĠMicrosoft ĠCorporation . ĠAll Ġrights Ġreserved Ċ Ċ package Ġcom . microsoft . did . sdk . backup . container . j we Ċ Ċ import Ġcom . microsoft . did . sdk . backup . container . Protection Method Ċ import Ġcom . microsoft . did . sdk . backup . content . Protected Backup Data Ċ import Ġcom . microsoft . did . sdk . backup . content . Un protected Backup Data Ċ import Ġcom . microsoft . did . sdk . crypto . protocols . j ose . j we . J we Token Ċ import Ġcom . microsoft . did . sdk . util . cont ro lf low . Bad Password Exception Ċ import Ġcom . microsoft . did . sdk . util . cont ro lf low . Failed Decrypt Exception Ċ import Ġcom . nim bus ds . j ose . Encryption Method Ċ import Ġcom . nim bus ds . j ose . J WE Algorithm Ċ import Ġcom . nim bus ds . j ose . J WE Header Ċ import Ġcom . nim bus ds . j ose . j wk . Oct et Sequence Key Ċ import Ġkotlinx . serialization . encode ToString Ċ import Ġkotlinx . serialization . json . Json Ċ import Ġjavax . crypto . spec . SecretKey Spec Ċ Ċ class ĠJ we Password Protection Method ( ĊĠĠĠ Ġval Ġpassword : ĠString Ċ ) Ġ: ĠProtection Method () Ġ{ ĊĊĠĠĠ Ġoverride Ġfun Ġwrap ( un protected Backup Data : ĠUn protected Backup Data , Ġserializer : ĠJson ): ĠProtected Backup Data Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġdata Ġ= Ġserializer . encode ToString ( un protected Backup Data ) ĊĠĠĠĠĠĠĠ Ġval Ġtoken Ġ= ĠJ we Token ( data ) ĊĠĠĠĠĠĠĠ Ġval Ġheaders Ġ= ĠJ WE Header . Builder ( J WE Algorithm . PB ES 2 _ HS 5 1 2 _ A 2 5 6 KW , ĠEncryption Method . A 2 5 6 CBC _ HS 5 1 2 ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. contentType ( un protected Backup Data . type ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. build () ĊĠĠĠĠĠĠĠ Ġval Ġsecret Key Ġ= ĠOct et Sequence Key . Builder ( ĊĠĠĠĠĠĠĠĠĠĠĠ Ġpassword . toByteArray () ĊĠĠĠĠĠĠĠ Ġ). build () ĊĠĠĠĠĠĠĠ Ġtoken . encrypt ( secret Key , Ġheaders ) ĊĠĠĠĠĠĠĠ Ġreturn ĠJ we Password Protected Backup Data ( token ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġunwrap ( protected Backup Data : ĠProtected Backup Data , Ġserializer : ĠJson ): ĠUn protected Backup Data Ġ{ ĊĠĠĠĠĠĠĠ Ġif Ġ( protected Backup Data Ġ! is ĠJ we Password Protected Backup Data ) Ġthrow ĠFailed Decrypt Exception (" Protection Ġparam aters Ġdo Ġnot Ġmatch Ġbackup Ġcontents ") ĊĠĠĠĠĠĠĠ Ġif Ġ( password . isEmpty ()) Ġthrow ĠBad Password Exception (" Password Ġcan 't Ġbe Ġempty ") ĊĊĠĠĠĠĠĠĠ Ġval Ġsecret Key Ġ= ĠSecret KeySpec ( ĊĠĠĠĠĠĠĠĠĠĠĠ Ġpassword . toByteArray (), ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ" RAW " ĊĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠĠĠ Ġval Ġdata Ġ= Ġprotected Backup Data . j we Token . decrypt ( privateKey Ġ= Ġsecret Key ) ĊĠĠĠĠĠĠĠ Ġreturn Ġserializer . decode FromString ( Un protected Backup Data . serializer (), ĠString ( data )) ĊĠĠĠ Ġ} Ċ }