/// <summary> /// Decrypts the specified encrypted e-KYC response data received from UIDAI. /// </summary> /// <param name="kycInfo">The encrypted e-KYC data.</param> /// <returns>The decrypted e-KYC data.</returns> /// <exception cref="ArgumentNullException"><paramref name="kycInfo"/> or <see cref="EncryptedKycInfo.InfoValue"/> is null.</exception> public DecryptedKycInfo Decrypt(EncryptedKycInfo kycInfo) { ValidateNull(kycInfo, nameof(kycInfo)); ValidateNull(kycInfo.InfoValue, nameof(EncryptedKycInfo.InfoValue)); var iv = new byte[kycInfo.OaepLabel.Count]; Buffer.BlockCopy(kycInfo.OaepLabel.Array, kycInfo.OaepLabel.Offset, iv, 0, iv.Length); // Decrypt Key var oaep = new OaepEncoding(new RsaEngine(), new Sha256Digest(), iv); oaep.Init(false, DotNetUtilities.GetRsaKeyPair(KuaKey.GetRSAPrivateKey()).Private); var key = oaep.ProcessBlock(kycInfo.EncryptedKey.Array, kycInfo.EncryptedKey.Offset, kycInfo.EncryptedKey.Count); // Decrypt Data var cipher = CipherUtilities.GetCipher(SymmetricAlgorithm); var parameter = new ParametersWithIV(new KeyParameter(key), iv, 0, 16); cipher.Init(false, parameter); var data = cipher.DoFinal(kycInfo.EncryptedData.Array, kycInfo.EncryptedData.Offset, kycInfo.EncryptedData.Count); return(new DecryptedKycInfo { InfoValue = data }); }
/// <summary> /// When overridden in a descendant class, deserializes the object from an XML according to Aadhaar API specification. /// </summary> /// <param name="element">An instance of <see cref="XElement"/>.</param> /// <exception cref="ArgumentNullException"><paramref name="element"/> is null.</exception> protected override void DeserializeXml(XElement element) { ValidateNull(element, nameof(element)); IsAuthentic = element.Attribute("ret").Value[0] == AadhaarHelper.YesUpper; ResponseCode = element.Attribute("code").Value; Timestamp = DateTimeOffset.Parse(element.Attribute("ts").Value, CultureInfo.InvariantCulture); ErrorCode = element.Attribute("err")?.Value; if (!string.IsNullOrWhiteSpace(ErrorCode)) { return; } IsDecryptionByKsa = element.Attribute("ko").Value == "KSA"; if (!IsDecryptionByKsa) { var encryptedKycInfo = new EncryptedKycInfo { InfoValue = Convert.FromBase64String(element.Element("kycRes").Value) }; var decryptedKycInfo = encryptedKycInfo.Decrypt(Decryptor); element = decryptedKycInfo.ToXml(); } var authBytes = Convert.FromBase64String(element.Element("Rar").Value); using (var stream = new MemoryStream(authBytes)) base.DeserializeXml(XElement.Load(stream)); TimeToLive = DateTimeOffset.ParseExact(element.Attribute("ttl").Value, AadhaarHelper.TimestampFormat, CultureInfo.InvariantCulture); var uidData = element.Element("UidData"); Resident = new PersonalInfo { AadhaarNumber = uidData.Attribute("uid").Value, Demographic = new Demographic { Address = new Address(uidData.Element("Poa")), Identity = new Identity(uidData.Element("Poi")) }, Photo = Convert.FromBase64String(uidData.Element("Pht").Value) }; var localAddress = uidData.Element("LData"); if (localAddress != null) { Resident.Demographic.ILAddress = new Address(localAddress); Resident.Demographic.LanguageUsed = (IndianLanguage?)int.Parse(localAddress.Attribute("lang").Value); } EAadhaar = new AadhaarDocument { Content = uidData.Element("Prn").Value }; }