cryptdecrypt API not able to decrypt 3Des Encrypted data on Windows 11

Ritu Varkey 41 Reputation points
2023-02-13T07:10:02.4333333+00:00

My application runs on Windows desktop. Algorithm used is Triple DES. The Data is encrypted on the server using Bouncycastle java API. The application is able to decrypt the data on Windows 10 but fails to decrypt correctly on windows 11. It decrypts the first part of the data correctly the remaining part is remains encrypted.

Java Code - Encryption


static final int BLOCK_SIZE_3DES = 8;
public byte[] encryptDESede(byte[] data, byte[] key, byte[] initVec) throws StandardException {  
//add paddingif req
if(data.length % BLOCK_SIZE_3DES != 0) {     
 byte[] padding = new byte[BLOCK_SIZE_3DES - (data.length % BLOCK_SIZE_3DES)];     
 for (int i = 0; i < padding.length; i++) 
   padding[i] = (byte) padding.length;     
 data = Bytes.join(new byte[][]{data, padding});   
}  


 BlockCipher engine = new DESedeEngine(); 
CBCBlockCipher cbc = new CBCBlockCipher(engine);  
 BufferedBlockCipher cipher = new BufferedBlockCipher(cbc);   cipher.init(true, new ParametersWithIV(new KeyParameter(key), initVec)); 
byte[] out = new byte[data.length];   cipher.processBytes(data, 0, data.length, out, 0);   
return out;
}

C++ Decryption Code

bool Decipher(CData & _Data) const {

	DWORD FinalSize = _Data.size();
	//check that size is multiple of 8
	if ( _Data.size() % 8) {
		return false;
	}
	
    BOOL Result = CryptDecrypt(m_Key, 0, TRUE, 0, _Data.data(), &FinalSize);
	
	DWORD err = GetLastError();
	if (Result == FALSE && err != NTE_BAD_DATA)
		return false;

	if ((Result == FALSE) && (err == NTE_BAD_DATA)) {
		CData EmptyBlob;
		Encipher(EmptyBlob);
	}
	else
	{
		// Return ciphered data and final size
		_Data.resize(FinalSize);
	}

	return true;
}
Windows for business | Windows Client for IT Pros | User experience | Other
Developer technologies | C++
{count} votes

2 answers

Sort by: Most helpful
  1. Limitless Technology 44,766 Reputation points
    2023-02-13T17:08:20.42+00:00

    Hello there,

    This API is deprecated. New and existing software should start using Cryptography Next Generation APIs. Microsoft may remove this API in future releases. This might be the reason that this is working on Windows 10 but not on Windows 11.

    https://learn.microsoft.com/en-us/windows/win32/api/wincrypt/nf-wincrypt-cryptencrypt

    Hope this resolves your Query !!

    --If the reply is helpful, please Upvote and Accept it as an answer–

    0 comments No comments

  2. Limitless Technology 44,766 Reputation points
    2023-02-13T17:08:32.2433333+00:00

    Double post

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.