Click here to Skip to main content
15,891,424 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all,
I am using aladin etoken for encryption.
I have stored DES key in etoken.
My code is running nicely when i m giving plain text with 40 characters.
It encrypt and decrypt perfectly.
but when i m giving characters more or less than 40.it is failing for me
I m using PKCS11 code. Dot net dll comes with aladin sdk only.
my code is as below.


C#
public void encryptDES(byte[] src)
       {
           try
           {
               PKCS11.Object[] hkey = new PKCS11.Object[0];
               PKCS11.Initialize("etpkcs11.dll");

               PKCS11.Slot[] sl = new PKCS11.Slot[0];
               sl = PKCS11.GetSlotList(false);
               PKCS11.Slot sl1 = new PKCS11.Slot();
               sl1 = sl[0];

               sc = PKCS11.OpenSession(sl1, 6);

               int a = sc.Login(1, "Inspira@123");
               Console.WriteLine("Sucessfully Logged In");


               PKCS11.Mechanism[] mh1 = new PKCS11.Mechanism[500];
               mh1 = PKCS11.GetMechanismList(sl1);
               PKCS11.Mechanism mch2 = new PKCS11.Mechanism();


               byte[] IV = { 12, 13, 14, 15, 16, 17, 18, 19 };
               mch2.id = 289;
               mch2.iv = IV;
               mch2.length = IV.Length;
               PKCS11.Attribute[] pDESTemplate = new PKCS11.Attribute[2];

               pDESTemplate[0] = new PKCS11.Attribute(PKCS11.CKA_KEY_TYPE, 19);
               pDESTemplate[1] = new PKCS11.Attribute(PKCS11.CKA_CLASS, 4);

               hkey = sc.FindObjects(pDESTemplate);
               PKCS11.Object bj = new PKCS11.Object();
               bj = hkey[0];


               //Encrypt
               byte[] enct = sc.Encrypt(mch2, bj, src);
               File.WriteAllBytes("D:\\Encrypted.txt", enct);
               Console.WriteLine("Encryped text :" + ASCIIEncoding.ASCII.GetString(enct));

               Console.ReadLine();
             //  sc.Close();


           }
           catch (Exception ex)
           {
               Console.WriteLine("Error:" + ex.Message + ex.InnerException);
           }
       }



if anyone has any idea about this please help me..
It happens with tripledes also.
and also i m not able to find error code list for pkcs 11 standard by which i can come to know what is actually the error is.

Thanks in advance,
God bless u all .:)
Posted

1 solution

DES is a block level cipher. You must use a padding mechanish such as CBC,CFB8, OFB8 or PKCS5 etc.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900