crypto.BadPaddingException: data hash wrong (EKYC-Response)

574 views Asked by At

While decrypting the EKYC response using sefenet usb token i got error data hash wrong.please check it my below code

public byte[] decrypt(byte[] datatosegregate, bool fromUSBToken)
            {
                byte[][] result =null;
                try
                {
                    byte[][] verdata = Utilities.split(datatosegregate, 11);
                    byte[] versionNumber = verdata[0];
                    string verNumber = System.Text.Encoding.ASCII.GetString(versionNumber);
                    byte[] verData = null;
                    if (verNumber.Equals("VERSION_1.0"))
                    {
                        verData = verdata[1];
                    }
                    else
                    {
                        verData = datatosegregate;
                    }
                    byte[][] data = Utilities.split(verData, 294);
                    byte[] publickey = data[0];
                    byte[] publicKeyModulus = GetPublicKeyInfo(publickey);
                    byte[] mergedata = data[1];
                    byte[][] datafrompad = Utilities.split(mergedata, 32);
                    byte[] padding = datafrompad[0];
                    byte[] mergedatafromkey = datafrompad[1];
                    byte[][] datafromkey = Utilities.split(mergedatafromkey, 256);
                    byte[] encryptedsecret = datafromkey[0];
                    Console.WriteLine(encryptedsecret.Length);
                    byte[] encryptedmessage = datafromkey[1];
                    byte[] secreatkey = null;
                    if (fromUSBToken)
                    {
                        General.LogFile("USBToken Available", "decrypt", "Decryptor.cs");
                        CSPDec dec = new CSPDec();
                        secreatkey = dec.Decrypt(encryptedsecret, padding, publicKeyModulus);
                    }
                    else
                    {
                        General.LogFile("USBToken Not Available", "decrypt", "Decryptor.cs");
                        secreatkey = this.getkey(encryptedsecret, padding);
                    }
                    byte[] plaintext = this.decryptmessage(encryptedmessage, padding, secreatkey);
                    result = Utilities.split(plaintext, 32);

                    // Console.WriteLine(Encoding.UTF8.GetString(result[1]));

                    byte[] hashBytes = GenerateHash(result[1]);

                    if (Utilities.ParseByteArraytoHexString(hashBytes) != Utilities.ParseByteArraytoHexString(result[0]))
                        throw new System.Exception("File is Corrupt or invalid !");
                }
                catch (Exception ex)
                {
                    General.ErrorFile(ex.Message, "decrypt", "Decryptor.cs");
                }
                return result[1];
                }


   // CSPDec class
     public byte[] Decrypt(byte[] message,byte[] pad, byte[] modulus)
            {
                byte[] aeskey = null;
                try
                {
                    //Module module = Module.GetInstance(@"C:\WINDOWS\system32\aetpkss1.dll");
                    Module module = Module.GetInstance(@"C:\WINDOWS\system32\eTPKCS11.dll");
                    module.Initialize();
                    General.LogFile("module Initialized", "decrypt", "CSPDec.cs");
                    Slot[] slots = module.GetSlotList(true);

                    if (slots.Length == 0)
                    {
                        General.LogFile("No slot available", "decrypt", "CSPDec.cs");
                        //MessageBox.Show("No slot available");
                        return null;
                    }

                    Token token = null;
                    for (int i = 0; i < slots.Length; i++)
                    {
                        if (slots[i].SlotInfo.IsTokenPresent)
                            token = slots[i].Token;
                        General.LogFile("slot available" + token, "decrypt", "CSPDec.cs");
                    }

                    Session session = token.OpenSession(true);
                    General.LogFile("Open session" + token, "decrypt", "CSPDec.cs");
                    //PIN pin = new PIN();
                    //pin.ShowDialog();

                    // Executes the login passing the user PIN
                    //session.Login(UserType.USER,pin.Pin.ToCharArray());

                    string lstrTokenPassword = "#######";
                    General.LogFile("TokenPassword::" + lstrTokenPassword, "decrypt", "CSPDec.cs");
                session.Login(UserType.USER, lstrTokenPassword.ToCharArray());
                General.LogFile("Session Logged::" + UserType.USER, "decrypt", "CSPDec.cs");
                // Find RSA Private keys
                session.FindObjectsInit(new P11Attribute[] { new ObjectClassAttribute(CKO.PRIVATE_KEY), new KeyTypeAttribute(CKK.RSA) });
                General.LogFile("Session FindObjectsIni:", "decrypt", "CSPDec.cs");
                P11Object[] keyObjects = session.FindObjects(10);

                RSAPrivateKey rsaPrivateKey = null;
                General.LogFile("RSAPrivateKey keyObjects::" + keyObjects.Length.ToString(), "decrypt", "CSPDec.cs");
                for (int i = 0; i < keyObjects.Length; i++)
                {
                    rsaPrivateKey = (RSAPrivateKey)keyObjects[i];
                    General.LogFile("PrivateKey.Modulus.Value::" + rsaPrivateKey.Modulus.Value, "decrypt", "CSPDec.cs");
                    if (Utilities.CompareBytes(rsaPrivateKey.Modulus.Value, modulus))
                    {                        
                        General.LogFile("Utilities CompareBytes rsaPrivateKey", "decrypt", "CSPDec.cs");
                        break;
                    }
                }
                try
                {
                    byte[] lbtPrimary = rsaPrivateKey.Modulus.Value;
                    string lstrPrimary = System.Text.Encoding.UTF8.GetString(lbtPrimary);
                    General.LogFile("RSAPrivateKey PrivateKey::" + lstrPrimary.ToString(), "decrypt", "CSPDec.cs");
                }
                catch (Exception ex)
                {
                    General.ErrorFile(ex.Message, "Get PrivateKay", "CSPDec.cs");
                }
                finally
                {
                }
                if (rsaPrivateKey == null)
                {
                    General.LogFile(" No corresponding Private key found", "decrypt", "CSPDec.cs");
                    return null;
                }               
                Mechanism decryptMech = new Mechanism(CKM.RSA_X_509);               
                try
                {
                    session.DecryptInit(decryptMech, rsaPrivateKey);
                    General.LogFile("session.DecryptInit message::" + message, "decrypt", "CSPDec.cs");
                    byte[] dec = session.Decrypt(message);
                    General.LogFile("session.Decrypt" + dec.ToString(), "decrypt", "CSPDec.cs");
                    IAsymmetricBlockCipher cipher = new OaepEncoding(new RsaEngine(), new Sha256Digest(), pad);
                    Org.BouncyCastle.Math.BigInteger mod = new Org.BouncyCastle.Math.BigInteger(1, rsaPrivateKey.Modulus.Value);
                    Org.BouncyCastle.Math.BigInteger exp = new Org.BouncyCastle.Math.BigInteger("1", 16);
                    RsaKeyParameters p_Temp = new RsaKeyParameters(false, mod, exp);
                    General.LogFile("RsaKeyParameters" + dec.ToString(), "decrypt", "CSPDec.cs");
                    cipher.Init(false, p_Temp);
                    General.LogFile("RsaKeyParameters1" + dec.ToString(), "decrypt", "CSPDec.cs");
                    aeskey = cipher.ProcessBlock(dec, 0, dec.Length);
                    General.LogFile("cipher.ProcessBlock" + aeskey, "decrypt", "CSPDec.cs");
                }
                catch (Exception ex)
                {
                    General.ErrorFile(ex.Message, "decrypt", "CSPDec.cs");
                }
                finally
                {
                    session.Logout();
                    session.CloseSession();
                }
            }
            catch (Exception ex)
            {
                General.ErrorFile(ex.Message, "decrypt", "CSPDec.cs");
            }
            return aeskey;
        }
0

There are 0 answers