In order to thank you for all members' support of our website, we are holding new events - Getting Free Samples by Paying Freight only.
Want to get free samples? Please check this post for more details.

How to generate DES key without setting it in advance

Algorithm School

Moderator: UNKNwYSHSA

Marcat
Posts: 8
Joined: Wed Dec 21, 2016 11:50 pm
Points :104
Contact:

How to generate DES key without setting it in advance

Post by Marcat » Thu Feb 02, 2017 7:19 pm

I would like to generate DES Key inside my applet and use it to encrypt and decrypt data.
However I don't know how to generate a DESKey if I don't set it by myself.

My code is as belows. In this case, I gor error SW_UNINITIALIZED_KEY. Is there any solution to solve this problem.

Code: Select all

DESKey tdesKey;
Cipher desCipher;
byte[] ENC_RESULT;
//  Set DESKey by myself
//byte[] KeyEnc ={(byte)0x51, (byte)0xF5, (byte)0x64, (byte)0x86, (byte)0xE5,(byte)0x04, (byte)0xD6, (byte)0x28,(byte)0x01, (byte)0x45, (byte)0x2A, (byte)0x13, (byte)0x04,(byte)0xE2, (byte)0x5F, (byte)0x7B};

tdesKey = (DESKey) KeyBuilder.buildKey(KeyBuilder.TYPE_DES, KeyBuilder.LENGTH_DES3_2KEY, true);
//tdesKey.setKey(KeyEnc, (short) 0);
desCipher = Cipher.getInstance(Cipher.ALG_DES_ECB_NOPAD, false);
desCipher.init(tdesKey, Cipher.MODE_ENCRYPT);
desCipher.doFinal(Data, (short) 0, (short) Data.length, ENC_RESULT, (short) 0);

Tazmania09
Posts: 25
Joined: Mon Nov 30, 2015 3:40 am
Points :178
Contact:

Re: How to generate DES key without setting it in advance

Post by Tazmania09 » Thu Feb 02, 2017 8:23 pm

Here is a method to generate a random DES key.

Code: Select all

RandomData rand = RandomData.getInstance(RandomData.ALG_SECURE_RANDOM);
short lenBytes = (short) (KeyBuilder.LENGTH_DES3_3KEY/8);
byte[] buffer = JCSystem.makeTransientByteArray(lenBytes, JCSystem.CLEAR_ON_DESELECT);
DESKey key = (DESKey) KeyBuilder.buildKey(KeyBuilder.TYPE_DES, KeyBuilder.LENGTH_DES3_3KEY, false);

rand.generateData(buffer, (short) 0, lenBytes);
key.setKey(buffer, (short) 0);


tay00000
Posts: 112
Joined: Tue Sep 27, 2016 10:58 am
Points :1498
Contact:

Re: How to generate DES key without setting it in advance

Post by tay00000 » Mon Feb 06, 2017 9:14 am

Remember to wipe the buffer once you have called the setKey() or by overwriting data or generating another random to lose the data and prevent accidental leak of key material.

Marcat
Posts: 8
Joined: Wed Dec 21, 2016 11:50 pm
Points :104
Contact:

Re: How to generate DES key without setting it in advance

Post by Marcat » Tue Feb 07, 2017 2:33 am

tay00000 wrote:Remember to wipe the buffer once you have called the setKey() or by overwriting data or generating another random to lose the data and prevent accidental leak of key material.


Thank you very much for your kind tip. I will pay attention to it.

Post Reply Previous topicNext topic

Who is online

Users browsing this forum: No registered users and 1 guest

JavaCard OS : Disclaimer