CKR_KEY_SIZE_RANGE Error n. 98 at DES3_ECB encrypt

9/11/2015 11:42:34 AM
Gravatar
Total Posts 1

CKR_KEY_SIZE_RANGE Error n. 98 at DES3_ECB encrypt

Hello,

I have downloaded <span data-scayt_word="NCryptoki" data-scaytid="2">NCryptoki trial version. I would like to encrypt simple string using <span data-scayt_word="DES3" data-scaytid="11">DES3 but I receive Error n.98 (<span data-scayt_word="CKR_KEY_SIZE_RANGE" data-scaytid="35">CKR_KEY_SIZE_RANGE )

Here is my code: 

            <span data-scayt_word="CryptokiCollection" data-scaytid="54">CryptokiCollection <span data-scayt_word="template_createDES3key" data-scaytid="56">template_createDES3key = new <span data-scayt_word="CryptokiCollection" data-scaytid="55">CryptokiCollection();
            template_createDES3key.Add(new <span data-scayt_word="ObjectAttribute" data-scaytid="57">ObjectAttribute(ObjectAttribute.CKA_CLASS, CryptokiObject.CKO_SECRET_KEY));
            template_createDES3key.Add(new <span data-scayt_word="ObjectAttribute" data-scaytid="58">ObjectAttribute(ObjectAttribute.CKA_KEY_TYPE, Key.CKK_DES3));
            template_createDES3key.Add(new <span data-scayt_word="ObjectAttribute" data-scaytid="59">ObjectAttribute(ObjectAttribute.CKA_LABEL, "my <span data-scayt_word="TDES" data-scaytid="61">TDES key "+DateTime.Now));
            template_createDES3key.Add(new <span data-scayt_word="ObjectAttribute" data-scaytid="62">ObjectAttribute(ObjectAttribute.CKA_PRIVATE, true));
            template_createDES3key.Add(new <span data-scayt_word="ObjectAttribute" data-scaytid="63">ObjectAttribute(ObjectAttribute.CKA_ENCRYPT, true));
            template_createDES3key.Add(new <span data-scayt_word="ObjectAttribute" data-scaytid="66">ObjectAttribute(ObjectAttribute.CKA_DECRYPT, true));

            session.GenerateKey(Mechanism.DES3_KEY_GEN, <span data-scayt_word="template_createDES3key" data-scaytid="71">template_createDES3key);

            string message = "Hello World";
            byte[] <span data-scayt_word="plaintext" data-scaytid="73">plaintext = Encoding.ASCII.GetBytes(message);

            <span data-scayt_word="CryptokiCollection" data-scaytid="75">CryptokiCollection <span data-scayt_word="template_getDes3Key" data-scaytid="80">template_getDes3Key = new <span data-scayt_word="CryptokiCollection" data-scaytid="76">CryptokiCollection();
            template_getDes3Key.Add(new <span data-scayt_word="ObjectAttribute" data-scaytid="81">ObjectAttribute(ObjectAttribute.CKA_CLASS, CryptokiObject.CKO_SECRET_KEY));
            template_getDes3Key.Add(new <span data-scayt_word="ObjectAttribute" data-scaytid="82">ObjectAttribute(ObjectAttribute.CKA_KEY_TYPE, Key.CKK_DES3));

            <span data-scayt_word="CryptokiCollection" data-scaytid="83">CryptokiCollection objects = session.Objects.Find(<span data-scayt_word="template_getDes3Key" data-scaytid="84">template_getDes3Key, 1);
            <span data-scayt_word="SecretKey" data-scaytid="88">SecretKey <span data-scayt_word="secretKey" data-scaytid="90">secretKey = (<span data-scayt_word="SecretKey" data-scaytid="89">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 <span data-scayt_word="des3cbc" data-scaytid="92">des3cbc = new Mechanism(Mechanism.CKM_DES3_ECB, iv);
            <span data-scayt_word="var" data-scaytid="95">var res = session.EncryptInit(<span data-scayt_word="des3cbc" data-scaytid="96">des3cbc, <span data-scayt_word="secretKey" data-scaytid="97">secretKey);
            byte[] <span data-scayt_word="plaintext_encrypted" data-scaytid="102">plaintext_encrypted = session.Encrypt(<span data-scayt_word="plaintext" data-scaytid="98">plaintext);

            // decrypt
            session.DecryptInit(<span data-scayt_word="des3cbc" data-scaytid="104">des3cbc, <span data-scayt_word="secretKey" data-scaytid="105">secretKey);
            byte[] <span data-scayt_word="plaintext_decrypted" data-scaytid="109">plaintext_decrypted = session.Decrypt(<span data-scayt_word="plaintext_encrypted" data-scaytid="106">plaintext_encrypted);

 

Thanks
Paul

9/24/2015 4:31:14 PM
Gravatar
Total Posts 300
Ugo Chirico http://www.ugochirico.com

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.