gpt4 book ai didi

使用 AES 的 Java 卡加密返回 6F00

转载 作者:行者123 更新时间:2023-11-30 06:10:02 31 4
gpt4 key购买 nike

我是 Java 卡编程的新手。我看到了一个关于 Java 卡的 AES 加密方法的示例,并且能够创建和安装小程序。当我尝试与小程序通信时,它返回一条 6F00 错误消息。我做错了什么?

这是我的代码示例:

private void SendData(APDU apdu) 
{

aesKeyTrial= (AESKey) KeyBuilder.buildKey(KeyBuilder.TYPE_AES, KeyBuilder.LENGTH_AES_128, false);
byte[] outBuffer;
outBuffer = new byte[16];
byte[] trialKey;
trialKey = new byte[16];
byte[] input = {(byte)0x44,(byte)0x22,(byte)0x33,(byte)0x44,(byte)0x55,(byte)0x66,(byte)0x77,(byte)0x88,(byte)0x99,0x10,(byte)0xA2, 0x35, (byte)0x5E,0x15,0x16,0x14};
byte[] key = {0x2d, 0x2a, 0x2d, 0x42, 0x55, 0x49, 0x4c, 0x44, 0x41, 0x43, 0x4f, 0x44, 0x45, 0x2d, 0x2a, 0x2d};
byte[] buffer = apdu.getBuffer();
short len = (short) input.length;


aesKeyTrial.setKey(trialKey,(short)0);
if(len<=0||len%16!=0)
{
ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
}
aesCipher = Cipher.getInstance(Cipher.ALG_AES_BLOCK_128_CBC_NOPAD, false);
aesCipher.init(aesKeyTrial, Cipher.MODE_ENCRYPT);
aesCipher.doFinal(input, ISO7816.OFFSET_CDATA, len, outBuffer, (short)0);
Util.arrayCopyNonAtomic(input, (short)0, buffer, (short)0, (short)len);
apdu.setOutgoing();
apdu.setOutgoingLength(len);
apdu.sendBytes((short) 0, (short)len);
}

我要加密的是“输入”。

最佳答案

正如您在回答中所说,您的代码的问题是您向 doFinal (byte[] inBuff, short inOffset, short inLength,byte[] outBuff, short outOffset) 传递了错误的参数 方法。

但是,您使用的示例代码看起来真的很糟糕。我称它写得不好是有原因的:

1- 它在多次调用的方法中使用了动态变量。动态变量是那些使用 new 关键字创建的变量。这些变量将分配在 EEPROM 中。在 EEPROM 中写入真的比在 RAM 中写入要慢,并且在它之前也有有限的删除-写入操作 wear out ,因此强烈建议在不需要在不同 CAD(读卡器) session 之间共享数据的情况下使用 RAM 而不是 EEPROM。也因为Java Card中没有强制/自动垃圾收集器,所以不要重复定义不同的动态变量,而是使用一个全局变量(类变量或实例变量而不是局部变量)。

2- 由于 int 数据类型在 JavaCards 中是可选的,因此您必须尽量使用 shortbyte 并且您也需要将所有值都转换为这些类型以防止有符号/无符号问题。

最后,我为您提供了您程序的改进版本:

package soQusetion;

import javacard.framework.*;
import javacard.security.AESKey;
import javacard.security.KeyBuilder;
import javacardx.crypto.Cipher;

public class SOQ1 extends Applet {

Cipher aesCipher;
AESKey aesKey;

//Two different types of memory for different usage. each one has 16 (= 0x10) byte capacity.
private byte[] volatileMem;
private byte[] nonVolatileMem;

//INS value for APDU command
public static final byte INS_SET_KEY = 0x10;
public static final byte INS_ENCRYPT = 0x20;
public static final byte INS_DECRYPT = 0x30;

public static void install(byte[] bArray, short bOffset, byte bLength) {
new SOQ1();
}

protected SOQ1() {
volatileMem = JCSystem.makeTransientByteArray((short) 0x10, JCSystem.CLEAR_ON_DESELECT);
nonVolatileMem = new byte[(short) 0x10];
aesCipher = Cipher.getInstance(Cipher.ALG_AES_BLOCK_128_ECB_NOPAD, false);
aesKey = (AESKey) KeyBuilder.buildKey(KeyBuilder.TYPE_AES, KeyBuilder.LENGTH_AES_128, false);
register();
}

public void process(APDU apdu) {
if (selectingApplet()) {
return;
}
byte[] buffer = apdu.getBuffer();
apdu.setIncomingAndReceive();

if (buffer[ISO7816.OFFSET_LC] != (byte) 0x10) {
ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
}

switch (buffer[ISO7816.OFFSET_INS]) {
case INS_SET_KEY:
aesKey.setKey(buffer, ISO7816.OFFSET_CDATA);
break;
case INS_ENCRYPT:
aesCipher.init(aesKey, Cipher.MODE_ENCRYPT);
aesCipher.doFinal(buffer, ISO7816.OFFSET_CDATA, (short) 0x10, volatileMem, (short) 0x00);
Util.arrayCopyNonAtomic(volatileMem, (short) 0x00, buffer, (short) 0x00, (short) 0x10);
apdu.setOutgoingAndSend((short) 0x00, (short) 0x10);
break;
case INS_DECRYPT:
aesCipher.init(aesKey, Cipher.MODE_DECRYPT);
aesCipher.doFinal(buffer, ISO7816.OFFSET_CDATA, (short) 0x10, volatileMem, (short) 0x00);
Util.arrayCopyNonAtomic(volatileMem, (short) 0x00, buffer, (short) 0x00, (short) 0x10);
apdu.setOutgoingAndSend((short) 0x00, (short) 0x10);
break;
default:
ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
}

}
}

它的工作原理如下:

Select Applet begin...
Select Applet successful.
//Loading AES Key APDU Command
Send: 00 10 00 00 10 00 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF 00
Recv: 90 00
//Encrypt APDU Command
Send: 00 20 00 00 10 00 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF 00
Recv: 62 F6 79 BE 2B F0 D9 31 64 1E 03 9C A3 40 1B B2 90 00
//Decrypt APDU Command
Send: 00 30 00 00 10 62 F6 79 BE 2B F0 D9 31 64 1E 03 9C A3 40 1B B2 00
Recv: 00 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF 90 00

欢迎提出任何使上述程序更高效的建议。

关于使用 AES 的 Java 卡加密返回 6F00,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36734616/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com