public static string CalculateSecurePinVersion9(string pin, byte[] pinKey, string cvv2 = "000", string expiryDate = "0000") { if (pinKey == null) { throw new ArgumentNullException("pinKey"); } var pinBlockString = pin + cvv2 + expiryDate; var clearPinBlock = "2" + pinBlockString.Length + pinBlockString; const byte randomBytes = 0x0; var randomDigit = (int)((randomBytes * 10) / 128); randomDigit = Math.Abs(randomDigit); var pinPadLength = 16 - clearPinBlock.Length; for (var i = 0; i < pinPadLength; i++) { clearPinBlock += randomDigit; } var desEdeEngine = new DesEdeEngine(); var keyParameters = new DesEdeParameters(pinKey); desEdeEngine.Init(true, keyParameters); var clearPinBlockBytes = Hex.Decode(clearPinBlock); var encryptedPinBlockBytes = new byte[8]; desEdeEngine.ProcessBlock(clearPinBlockBytes, 0, encryptedPinBlockBytes, 0); var encodedEncryptedPinBlockBytes = Hex.Encode(encryptedPinBlockBytes); var securePin = Encoding.UTF8.GetString(encodedEncryptedPinBlockBytes, 0, encodedEncryptedPinBlockBytes.Length); return(securePin); }
public static byte[] decrypt(String encryptedPINBlock, byte[] pinKey) { DesEdeEngine engine = new DesEdeEngine(); DesEdeParameters keyParameters = new DesEdeParameters(pinKey); engine.Init(false, keyParameters); byte[] encryptedPINBlockBytes = Hex.Encode(Encoding.UTF8.GetBytes(encryptedPINBlock)); byte[] clearPINBlockBytes = new byte[8]; int res = engine.ProcessBlock(encryptedPINBlockBytes, 0, clearPINBlockBytes, 0); byte[] decodedClearPINBlockBytes = Hex.Decode(clearPINBlockBytes); AppUtils.zeroise(encryptedPINBlockBytes); AppUtils.zeroise(clearPINBlockBytes); return(decodedClearPINBlockBytes); }
public static byte[] encrypt(string clearPINBlock, byte[] pinKey) { DesEdeEngine engine = new DesEdeEngine(); DesEdeParameters keyParameters = new DesEdeParameters(pinKey); engine.Init(true, keyParameters); byte[] clearPINBlockBytes = Hex.Decode(clearPINBlock); byte[] encryptedPINBlockBytes = new byte[8]; int res = engine.ProcessBlock(clearPINBlockBytes, 0, encryptedPINBlockBytes, 0); byte[] encodedEncryptedPINBlock = Hex.Encode(encryptedPINBlockBytes); AppUtils.zeroise(clearPINBlockBytes); AppUtils.zeroise(encryptedPINBlockBytes); return(encodedEncryptedPINBlock); }