언어 설정

Menu
Sites
Language
Password based decryption problem

hi i am using password based encryption mechansim for encrypting some string data i was successfully in encrypting the data by while decrypting the data i am getting the system error
please guide me 
i am attaching my sample code below
 

ByteBuffer password;
        String pwdbuff="harish";
            const int BUFFER_SIZE = 16;
            int length = pwdbuff.GetLength();
            int index = 0;
            result r = E_SUCCESS;
            ByteBuffer *pwd=StringUtil::StringToUtf8N(pwdbuff);
            r = password.Construct(BUFFER_SIZE);
            r = password.SetArray(( byte*)"harish", index, length);
            password.Flip();
            Pkcs05Schemes pkcs05Schemes;
            r = pkcs05Schemes.Construct(password, length);
            Pkcs05PbEs2Parameters pkcs05PbEs2Parameters;
            AlgorithmIdentifier keyDerivationFunction;
            AlgorithmIdentifier encryptionScheme;
            Pkcs05PbKdf2Parameters pkcs05PbKdf2Params;
            ByteBuffer saltValue;
            InitialVector initialVector;
            ByteBuffer initialVectorValue;
            int iterationCount = 100;
            int derivedKeyLength = 20;
            r = saltValue.Construct(16);
            r = saltValue.SetArray((byte*) "1234567890123456", 0, 16);
            saltValue.Flip();
            r = pkcs05PbKdf2Params.Construct(saltValue, iterationCount, derivedKeyLength);
            r = keyDerivationFunction.Construct(OID_PBKDF2, &pkcs05PbKdf2Params);
            r = initialVectorValue.Construct(BUFFER_SIZE);
            r = initialVectorValue.SetArray((const byte*) "12345678", index, length);
            initialVectorValue.Flip();
            r = initialVector.Construct(initialVectorValue);
            r = encryptionScheme.Construct(OID_AES_256_CBC, &initialVector);
            r = pkcs05PbEs2Parameters.Construct(keyDerivationFunction, encryptionScheme);
            ByteBuffer* pResult = null;
            AppLog("after pbes settings");
            ByteBuffer *pwdk=StringUtil::StringToUtf8N("harishK");
                pResult = pkcs05Schemes.EncryptionScheme2N(pkcs05PbEs2Parameters, *pwdk);
            AppLog("pResulr is %s",pResult->GetPointer());

//for decryption
            ByteBuffer *pResult1=pkcs05Schemes.DecryptionScheme2N(pkcs05PbEs2Parameters, *pResult);

            AppLog("pResult is %s if error is %s",pResult1->GetPointer(),GetLastResult())

Responses

7 댓글
Alex Ashirov

Hi,

Do you experience the same issue if use

r = encryptionScheme.Construct(OID_AES_128_CBC, &initialVector);

instead of

r = encryptionScheme.Construct(OID_AES_256_CBC, &initialVector);

?

harish kumar kavali

yes it is working for 128 bit but why it is not working for 256 are ther any changes to be done for 256 bit
pls help me regarding this issue

Regards 
Harish

Alex Ashirov

Hi Harish,

I have not tried this with 256 bit, but I know that this works with 128 bit. So, I don’t know the reason of the issue. Probably this is a bug.

harish kumar kavali

hi alex 
even for 128 bit , at the time of decryption i am getting different results 
consider this scenario scenario
i try to encrypt a random number like " 12425gyugh9890*L"":k "

after the decryption the results are like " 12425gyugh9890*L""%$"
it gives additional characters the original data after decryption 
may i know the reason
pls guide me

Alex Ashirov

Hi Harish,

Are you using escape characters like:

" 12425gyugh9890*L\"\":k "

instead of

" 12425gyugh9890*L"":k "

?

I have just tried with the escape characters and everything seems to be ok.

harish kumar kavali

Hi alex

actually the numbers are generated randomly, so i cannot modify that number
the number is generated using aes secure random


Regards 
harish
  

kimi

Hi,

OID_AES_256_CBC is 256 bits KEY encrpyiton that means key size must be 256 bits ie. 32 bytes.
make your derivedKeyLength = 32; Hope this solves your problem.

 

Kimi.