【发布时间】:2014-06-27 17:47:32
【问题描述】:
任何人都可以分享一个关于如何在目标 C 中实现 CBCBlockCipherMac 的示例代码。这是我走了多远,它给出了与 java 实现不同的结果。
const unsigned char key[16] = "\x1\x2\x3\x4\x5\x6\x7\x8\x9\x0\x1\x2\x3\x4\x5\x6";
const unsigned char data[14] = "\x54\x68\x69\x73\x69\x73\x6d\x79\x73\x74\x72\x69\x6e\x67";
CMAC_CTX *ctx = CMAC_CTX_new();
ret = CMAC_Init(ctx, key, sizeof(key), EVP_des_ede3(), 0);
printf("CMAC_Init = %d\n", ret);
ret = CMAC_Update(ctx, data, sizeof(data));
printf("CMAC_Update = %d\n", ret);
size_t size;
//unsigned int size;
unsigned char tag[4];
ret = CMAC_Final(ctx, tag, &size);
printf("CMAC_Final = %d, size = %u\n", ret, size);
CMAC_CTX_free(ctx);
printf("expected: 391d1520\n"
"got: ");
size_t index;
for (index = 0; index < sizeof(tag) - 1; ++index) {
printf("%02x", tag[index]);
if ((index + 1) % 4 == 0) {
printf(" ");
}
}
printf("%02x\n", tag[sizeof(tag) - 1]);
我的java代码是这样的
String *data = "Thisismystring";
String *keyString = "1234567890123456";
bytes[]mac = new byte[4];
CBCBlockCipherMac macCipher = new CBCBlockCipherMac(DESedeEngine);
DESedeParameters keyParameter = new DESedeParameters(keyString.getBytes());
DESedeEngine engine = new DESedeEngine();
engine,init(true, keyParameter);
byte[] dataBytes = data.getBytes();
macCipher.update(dataBytes,0,data.length());
macCipher.doFinal(mac,0);
byte[] macBytesEncoded = Hex.encode(mac);
String macString = new String(macBytesEncoded);
这给了我“391d1520”。但是目标c给了我“01000000”
【问题讨论】:
-
你能显示你得到的输出吗,多几行 Java 代码也不会伤害,只是为了和 C 代码比较?
-
@owlstead...用更多的 java 代码编辑了我的问题。谢谢
-
我不知道
01000000是什么,但它可能是MAC函数的实际输出的可能性很小,MAC函数的输出应该与随机无法区分。现在任何值都可以通过随机生成,但是将单个 1 位和 63 位设置为零,不...
标签: objective-c authentication encryption message cbc-mac