DES3 encryption error 145

5/14/2014 10:52:10 AM
Gravatar
Total Posts 1

DES3 encryption error 145

Hi Ugo,

I have similar problem as descriped here http://www.ncryptoki.com/Forums/Thread.aspx?pageid=9&mid=22&ItemID=1&thread=29

but using initializion vector doesn't help.

 

Here i my code:

CryptokiCollection templatePub = new CryptokiCollection();
templatePub.Add(new ObjectAttribute(ObjectAttribute.CKA_CLASS, CryptokiObject.CKO_SECRET_KEY));
templatePub.Add(new ObjectAttribute(ObjectAttribute.CKA_KEY_TYPE, Key.CKK_DES3));
templatePub.Add(new ObjectAttribute(ObjectAttribute.CKA_TOKEN, false));
templatePub.Add(new ObjectAttribute(ObjectAttribute.CKA_ENCRYPT, false));
templatePub.Add(new ObjectAttribute(ObjectAttribute.CKA_VALUE_LEN, 32));
Key symetricKey = session.GenerateKey(Mechanism.DES3_KEY_GEN, templatePub);


byte[] iv = new byte[16] {45,76,7,9,76,56,89,6,43,26,89,46,5,7,99,35};
Mechanism des3cbc = new Mechanism(Mechanism.CKM_DES3_CBC, iv);
session.EncryptInit(des3cbc, symetricKey);
byte[] encryptedMsg = session.Encrypt(msg);   // here is thrown exception with error 145

5/17/2014 10:00:33 AM
Gravatar
Total Posts 300
Ugo Chirico http://www.ugochirico.com

Re: DES3 encryption error 145

DES3 is 24 bytes long, not 32 as you wrote.

you should write:

 

templatePub.Add(new ObjectAttribute(ObjectAttribute.CKA_VALUE_LEN, 24));