/// <summary> /// Create Session Key /// </summary> /// <param name="bdk">Base Derivation Key</param> /// <param name="ksn">Key Serial Number</param> /// <param name="dukptVariant">DUKPT variant used to determine session key creation method</param> /// <returns>Session Key</returns> private static BigInteger CreateSessionKey(string bdk, string ksn, DUKPTVariant dukptVariant) { BigInteger ksnBigInt = ksn.HexToBigInteger(); BigInteger ipek = CreateIpek(ksnBigInt, bdk.HexToBigInteger()); BigInteger sessionKey; if (dukptVariant == DUKPTVariant.Data) { sessionKey = CreateSessionKeyDEK(ipek, ksnBigInt); } else { sessionKey = CreateSessionKeyPEK(ipek, ksnBigInt); } return(sessionKey); }
/// <summary> /// Decrypt data using TDES DUKPT. /// </summary> /// <param name="bdk">Base Derivation Key</param> /// <param name="ksn">Key Serial Number</param> /// <param name="encryptedData">Data to decrypt</param> /// <param name="variant">DUKPT transaction key variant</param> /// <returns>Decrypted data</returns> /// <exception cref="ArgumentNullException">Thrown for null or empty parameter values</exception> public static byte[] Decrypt(string bdk, string ksn, byte[] encryptedData, DUKPTVariant variant) { if (string.IsNullOrEmpty(bdk)) { throw new ArgumentNullException(nameof(bdk)); } if (string.IsNullOrEmpty(ksn)) { throw new ArgumentNullException(nameof(ksn)); } if (encryptedData == null) { throw new ArgumentNullException(nameof(encryptedData)); } return(Transform("TripleDES", false, CreateSessionKey(bdk, ksn, variant), encryptedData.ToBigInteger()).GetBytes()); }