我试图加密我卡中导入的密钥并将其再次存储在同一个字节数组中。 下面的import_key函数接收apdu并提取消息KEY,然后尝试加密它(简单的DES加密)并将其存储在同一个地方。 似乎我已经完成了所有必要的工作,但不幸的是,每次取消加密部分时我都会收到6f00! 这是我的代码:
public class Encryptor extends Applet {
private byte[] KEY = new byte[128];
private byte key_len = (byte)0;
private byte[] ENC_KEY = new byte[32];
private byte enc_key_len = (byte)0;
Cipher MyCipher;
private byte[] TheDES_Key = new byte[24];
DESKey MyDES1Key = (DESKey) KeyBuilder.buildKey(KeyBuilder.TYPE_DES,KeyBuilder.LENGTH_DES, false);
byte ConfiguredKeyLength = 0;
{...}
private void import_key(APDU apdu) {
byte[] buffer = new byte[maximum_buffer_len];
buffer = apdu.getBuffer();
key_len = buffer[ISO7816.OFFSET_LC];
apdu.setIncomingAndReceive();
Util.arrayCopy(buffer, (short) (ISO7816.OFFSET_CDATA), KEY, (short) 0, (short) key_len);
Util.arrayCopyNonAtomic(ENC_KEY, (short) 0, TheDES_Key, (short) 0, (short) enc_key_len);
MyDES1Key.setKey(TheDES_Key, (short) 0);
MyCipher.init(MyDES1Key, Cipher.MODE_ENCRYPT);
byte[] CipheredData = JCSystem.makeTransientByteArray((short) 32,JCSystem.CLEAR_ON_DESELECT);
MyCipher.doFinal(KEY, (short)0,(short)key_len,CipheredData,(short)0);
Util.arrayCopyNonAtomic(CipheredData,(short)0,KEY,(short)0,(short)key_len);
return;
}
}
答案 0 :(得分:0)
在行中:
DESKey MyDES1Key = (DESKey) KeyBuilder.buildKey(KeyBuilder.TYPE_DES,KeyBuilder.LENGTH_DES, false);
您将MyDES1Key
的长度定义为LENGTH_DES
,8
字节,而在行中:
MyDES1Key.setKey(TheDES_Key, (short) 0);
您正在使用24
字节变量填充它!
换句话说,您必须将KeyBuilder.LENGTH_DES
替换为KeyBuilder.LENGTH_DES3_3KEY
。