【发布时间】: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 函数相同的结果;字节数组具有相同的值;只是签名而不是未签名。