iOS 和 Android AES 加密(Java 中没有 UINT)

iOS and Android AES Encryption (No UINT in Java)

全部,

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

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

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

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 默认情况下将一个字节显示为有符号值。但实际上它们实际上是平等的。为了更清楚地说明,我将在您提供的示例 IV 数组的最后 5 个值中添加一点 table。

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

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

作为替代方案,我发现了一个 small website 包含有符号与无符号 type-bit/hex 转换器,它也可以解决问题。 (确保你 select 是 char 类型,否则带符号的值将不正确)


所以在代码的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");