【问题标题】:iOS and Android AES Encryption (No UINT in Java)iOS 和 Android AES 加密(Java 中没有 UINT)
【发布时间】:2018-12-28 23:46:43
【问题描述】:

全部,

我是加密新手,所以我不确定我需要分享哪些信息才能获得帮助;但是当我了解更多关于如何很好地提出这个问题时,我会编辑这个问题:)

我正在通过蓝牙与设备通信的 iOS 和 android 应用程序上执行 AES 加密。我正在使用 AES CTR 加密,它在 iOS 上已完全实现和运行。我遇到的问题是,当我将 IV 等项目转换为字节数组时; java字节是有符号的,而swift字节是无符号的,所以我可以在Java上加密和解密我的字符串;这与我在 iOS 中看到的结果不同。

其他人如何处理这个 unsigned int 问题?我觉得我做错了一些直截了当的事情。我真的不确定要发布什么代码。对于 android,我使用十六进制字符串到字节转换函数,我在堆栈溢出时找到了它们,它们工作正常......它们只是有符号而不是无符号,因此值与 iOS 中的无符号字节数组不同。

iOS 实现:

let aesPrivateKey = "********************************"

print("MacAddress:-> \(macAddress)")

var index = 0

let aesPrivateKeyStartIndex = aesPrivateKey.startIndex
let macAddressStartIndex = macAddress.startIndex

//Perform an XOR to get the device key
var deviceKeyArray: Array<Character> = Array(repeating: "?", count: 32)
for _ in macAddress {
    let nextPrivateKeyIndex = aesPrivateKey.index(aesPrivateKeyStartIndex, offsetBy: index)
    let nextMacAddressIndex = macAddress.index(macAddressStartIndex, offsetBy: index)

    let nextPrivateKeyString = String(aesPrivateKey[nextPrivateKeyIndex])
    let nextMacAddressString = String(macAddress[nextMacAddressIndex])

    let nextPrivateKeyByte = Int(nextPrivateKeyString, radix: 16)
    let nextMacAddressByte = Int(nextMacAddressString, radix: 16)

    let nextCombinedByte = nextPrivateKeyByte! ^ nextMacAddressByte!

    let nextCombinedString = nextCombinedByte.hexString

    deviceKeyArray[index] = nextCombinedString[nextCombinedString.index(nextCombinedString.startIndex, offsetBy: 1)]

    index+=1
}
while(index < 32) {

    let nextPrivateKeyIndex = aesPrivateKey.index(aesPrivateKeyStartIndex, offsetBy: index)
    deviceKeyArray[index] = aesPrivateKey[nextPrivateKeyIndex]
    index += 1
}

//Convert the device key to a byte array
let deviceKey = "0x" + String(deviceKeyArray)
let deviceKeyByte = Array<UInt8>(hex: deviceKey)

//Convert the password to a byte array
let passwordByte : Array<UInt8> = password.bytes

//Convert the initialization vector to a byte array
let aesIVHex = "0x" + AESIV
let aesIVByte = Array<UInt8>(hex: aesIVHex)

//Encrypt the password
var encrypted = [Unicode.UTF8.CodeUnit]()
do{
    encrypted = try AES(key: deviceKeyByte, blockMode: CTR(iv: aesIVByte)).encrypt(passwordByte)
}
catch{
    print(error)
}

print("The Encrypted Password Data: \(encrypted)")

let encryptedData = encrypted.toHexString()

//Write password to bluetooth and check result
UserDefaultUtils.setObject(encryptedData as AnyObject, key: userDefaults.password)
DeviceLockManager.shared().isEncrypted = false.
DeviceLockManager.share().setVerifyPasswordForDevice(isGunboxDevice:true)

Android 实现:

System.out.println("ble_ Password:"+str_password+"\nble_ AesKey:"+aesDeviceKey+"\nble_ AesIV:"+aesIV);

byte[] encryptedData = encrypt(
        str_password.getBytes(),
        Utility.getInstance().hexStringToByteArray(aesDeviceKey),
        Utility.getInstance().hexStringToByteArray(aesIV));

String encryptedPassword = Utility.getInstance().bytesToHexString(encryptedData);
System.out.println("ble_ AES Encrypted password " + encryptedPassword);
byte[] decryptedData = decrypt(encryptedData, aesDeviceKey.getBytes(), aesIV.getBytes());
System.out.println("ble_ Cipher Decrypt:"+new String(decryptedData));

//Write password to bluetooth and check result
deviceManager.writePassword(encryptedPassword);
Utility.getInstance().sleep(100);
deviceManager.readPasswordResult();

在我调用函数之前,所有输入值都完全匹配:hextStringtoByteArray。此时,iOS 字节数组是无符号的,而 android 字节数组是有符号的。

这是该函数供参考:

public static byte[] hexStringToByteArray(String s){
    byte[] b = new byte[s.length() / 2];
    for (int i = 0; i < b.length; i++) {
        int index = i * 2;
        int v = Integer.parseInt(s.substring(index, index + 2), 16);
        b[i] = (byte) v;
    }
    return b;
}

样本 IV 字节数组:

iOS 与 Android:

43、34、95、101、57、150、75、100、250、178、194、70、253、236、92、70

43、34、95、101、57、-106、75、100、-6、-78、-62、70、-3、-20、92、70

【问题讨论】:

  • 在对字符串进行编码/解码时,有符号字节与无符号字节应该没有区别。但是,使用的字符集可能会(java 中的默认字符集可能因操作系统而异!)。因此,为了提供有关如何解决此问题的答案,您需要解释您是如何做事的(或者更好的是,向我们展示一些代码)。
  • 我将实现更改为直接使用 bigInteger 并获得与使用 hexStringToByteArray 函数相同的结果;字节数组具有相同的值;只是签名而不是未签名。

标签: java android ios aes uint


【解决方案1】:

您可能会注意到两个打印数组之间的差异,因为默认情况下 java 将一个字节显示为有符号值。但实际上这些实际上是相等的。为了更清楚,我将添加一个小表格,其中包含您提供的示例 IV 数组的最后 5 个值。

|----------------------------------------|
| hex      |  46 |  FD |  EC |  5C |  46 |
|----------------------------------------|
| unsigned |  70 | 253 | 236 |  92 |  70 |
|----------------------------------------|
| signed   |  70 | -3  | -20 |  92 |  70 |
|----------------------------------------|

所以它们实际上是相同的(按位计算),只是在被解释为不同值时打印不同。如果您想确保一切正确,我建议您在编程模式下使用计算器查看一些数字。通常有一种方法可以设置字节/字长,这样您就可以使用相同十六进制值的有符号和无符号解释(该值还应该有位表示)。

作为替代方案,我发现了一个small website,其中包含一个有符号与无符号类型位/十六进制转换器,它也可以解决问题。 (请确保您选择任一字符类型,否则签名值将不正确)


所以在代码的 IV 字节部分应该没有任何问题。但是,当您仅使用字节数组作为参数创建 String 时,可能会有一个。 e.i:

byte[] decryptedData = decrypt(encryptedData, aesDeviceKey.getBytes(), aesIV.getBytes());
System.out.println("ble_ Cipher Decrypt:" + new String(decryptedData));

因为很可能使用的字符集不是 UTF-8。 (您可以通过调用Charset#defaultCharset 来确定,并检查其值)。替代方案是:

new String(decryptedData, StandardCharsets.UTF_8)

或:

new String(decryptedData, "UTF-8");

【讨论】:

  • 只是想添加我在 android 中的填充设置不正确的评论;接受无符号字节与有符号字节没有区别,这让我走上了正确的道路。
猜你喜欢
  • 2011-11-22
  • 1970-01-01
  • 2017-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多