ios java aes加密_ios和android aes加密(java中没有uint)

所有人,

我对加密还不太熟悉,所以我不确定是否需要共享所有信息才能获得帮助;但我将编辑此问题,以便进一步了解如何很好地提出此问题:)

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

其他人是如何处理这个无符号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 = 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(hex: deviceKey)

//Convert the password to a byte array

let passwordByte : Array = password.bytes

//Convert the initialization vector to a byte array

let aesIVHex = "0x" + AESIV

let aesIVByte = Array(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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值