<link>http://ncryptoki.com/forum.aspx</link> <description /> <docs>http://www.rssboard.org/rss-specification</docs> <generator>mojoPortal Forum module</generator> <item> <title>Re: CKR_KEY_SIZE_RANGE Error n. 98 at DES3_ECB encrypt Hi Paul,

DES3 is a block cipher. This means that you must encrypt block of plaintexts with the same size of the key.

You are trying to encrypt the string "Hello World" that have different size.

Also, the iv is not correct. For DES3 it would be 24 bytes.

]]>
http://ncryptoki.com/Forums/Thread.aspx?pageid=9&t=252~-1#post946 plocatel http://ncryptoki.com/Forums/Thread.aspx?pageid= Thu, 24 Sep 2015 14:31:14 GMT CKR_KEY_SIZE_RANGE Error n. 98 at DES3_ECB encrypt Hello,

I have downloaded NCryptoki trial version. I would like to encrypt simple string using DES3 but I receive Error n.98 (CKR_KEY_SIZE_RANGE )

Here is my code: 

            CryptokiCollection template_createDES3key = new CryptokiCollection();
            template_createDES3key.Add(new ObjectAttribute(ObjectAttribute.CKA_CLASS, CryptokiObject.CKO_SECRET_KEY));
            template_createDES3key.Add(new ObjectAttribute(ObjectAttribute.CKA_KEY_TYPE, Key.CKK_DES3));
            template_createDES3key.Add(new ObjectAttribute(ObjectAttribute.CKA_LABEL, "my TDES key "+DateTime.Now));
            template_createDES3key.Add(new ObjectAttribute(ObjectAttribute.CKA_PRIVATE, true));
            template_createDES3key.Add(new ObjectAttribute(ObjectAttribute.CKA_ENCRYPT, true));
            template_createDES3key.Add(new ObjectAttribute(ObjectAttribute.CKA_DECRYPT, true));

            session.GenerateKey(Mechanism.DES3_KEY_GEN, template_createDES3key);

            string message = "Hello World";
            byte[] plaintext = Encoding.ASCII.GetBytes(message);

            CryptokiCollection template_getDes3Key = new CryptokiCollection();
            template_getDes3Key.Add(new ObjectAttribute(ObjectAttribute.CKA_CLASS, CryptokiObject.CKO_SECRET_KEY));
            template_getDes3Key.Add(new ObjectAttribute(ObjectAttribute.CKA_KEY_TYPE, Key.CKK_DES3));

            CryptokiCollection objects = session.Objects.Find(template_getDes3Key, 1);
            SecretKey secretKey = (SecretKey)objects[0];

            // encrypt
            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_ECB, iv);
            var res = session.EncryptInit(des3cbc, secretKey);
            byte[] plaintext_encrypted = session.Encrypt(plaintext);

            // decrypt
            session.DecryptInit(des3cbc, secretKey);
            byte[] plaintext_decrypted = session.Decrypt(plaintext_encrypted);

 

Thanks
Paul

]]>
http://ncryptoki.com/Forums/Thread.aspx?pageid=9&t=252~-1#post945 plocatel http://ncryptoki.com/Forums/Thread.aspx?pageid= Fri, 11 Sep 2015 09:42:34 GMT