- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 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 中是可选的,因此您必须尽量使用 short
和 byte
并且您也需要将所有值都转换为这些类型以防止有符号/无符号问题。
最后,我为您提供了您程序的改进版本:
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/
我只想使用这 3 种模式从 openSSL 测试 AES: key 长度为 128,192 和 256,但我的解密文本与我的输入不同,我不知道为什么。此外,当我传递一个巨大的输入长度(比如说 1024
最近我终于(在 stackoverflow 的用户@WhozCraig 的帮助下)开始在 CBC 模式下使用 AES。现在,我想做完全相同的事情,但使用 AES IGE。我查看了 openssl-1.
网络设备已经配置了 snmpv3 用户,使用 AES192 作为隐私协议(protocol)。但是当执行以下命令时 snmpwalk -v3 -l authPriv -u user -a SHA -A
我在 c# 中使用 AES 算法进行加密和解密。我使用 AesCryptoServiceProvider 类进行加密和解密。 这是我在代码中的设置 AesCryptoServiceProvider r
我正在尝试使用具有不同 key 大小的 openssl 的 AES_decrypt 函数来解密密文。我能够成功解密 key 大小 = 128 的消息。这是我的代码 mydecrypt.c #inclu
如何在 AES-128、AES-192 和 AES-256 之间切换。我目前的实现仅使用 AES-128 Cipher cipher = Cipher.getInstance("AES/CBC/NoP
我的问题是我想在一个线图上叠加一个散点图,这两个图的颜色随着一个变量而变化。我只想保留一种颜色的图例。如果我使用 scale_colour_discrete(guide = "none") 它们都将消
我想用 C# 编写一个可以打开 KeePass 的程序1.x kdb 文件。我下载了源代码并尝试移植密码数据库读取功能。数据库内容已加密。加密 key 通过以下方式获得: 用户输入密码; 计算密码的
我只想将ruby代码迁移到Java 这是我的 ruby 代码 require 'openssl' require 'base64' key = '7c54367a45b37a192abc2cd7f45
我正在使用 AES 的 PyCrypto 实现,并且我正在尝试使用 24 字节 key 加密一些文本(24 字节)。 aes_ecb = AES.new('\x00'*24, AES.MODE_ECB
有人比较这些加密算法的优缺点吗? 最佳答案 使用 AES。 更多详细信息: DES 是七十年代的旧“数据加密标准”。它的 key 大小对于适当的安全性而言太短(56 个有效位;这可以被暴力破解,如 m
我在 iOS 中加密一个 NSString,编码和解码都很好: NSString *stringtoEncrypt = @"This string is to be encrypted"; NSStr
我正在尝试使用 nVidia CUDA 在 CTR 模式下实现 AES-256。我已经成功地为 key 扩展编写了 CPU 代码,现在我需要实现实际的 AES-256 算法。根据维基百科,我见过一些代
我正在 Contiki OS 中研究 AES 安全性。我有 AES 库,它支持两种类型的加密/解密: 即时 固定键 在即时中,当我使用 key 加密数据时,会生成新 key 和加密数据。这个新生成的
关于 AES 有很多问题,但我有以下问题。我目前正在使用以下 AES 实现来加密数据 byte [] PRFkey = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
有没有人一起比较这些加密算法的优缺点? 最佳答案 使用 AES。 更多细节: DES 是七十年代的旧“数据加密标准”。它的 key 大小对于适当的安全性来说太短了(56 位有效位;这可以被强制执行,正
我的团队需要开发一种解决方案,以在用 Java 编写的 Android 应用程序的上下文中加密二进制数据(存储为 byte[])。加密后的数据将通过多种方式传输和存储,在此过程中不排除出现数据损坏的情
我在客户端使用 CryptoJS AES 算法加密文本,我在服务器端用 java 解密它,但出现异常。 JS代码: var encrypted = CryptoJS.AES.encrypt("Mess
我之所以问这个问题,是因为 2 天来我已经阅读了很多关于加密 AES 加密的帖子,就在我以为我明白了的时候,我意识到我根本没有明白。 这篇文章是最接近我的问题的,我有完全相同的问题但没有得到解答: C
我想知道 AES 加密后的数据大小,这样我就可以避免缓冲我的 AES 后数据(在磁盘或内存上)主要是为了知道大小。 我使用 128 位 AES 和 javax.crypto.Cipher 和 java
我是一名优秀的程序员,十分优秀!