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

iOS和Android AES加密(Java中没有UINT),第1张

概述所有,我是加密技术的新手,因此不确定要获得帮助需要共享的所有信息.但随着我对如何更好地提出这个问题的更多了解,我将编辑这个问题:)我正在通过蓝牙与设备通信的iOS和Android应用程序上执行AES加密.我正在使用AESCTR加密,它已在iOS上完全实现并正常运行.我遇到的问题是,当我将IV

所有,

我是加密技术的新手,因此不确定要获得帮助需要共享的所有信息.但随着我对如何更好地提出这个问题的更多了解,我将编辑这个问题:)

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

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

iOS实现:

let aesPrivateKey = "********************************"print("MacAddress:-> \(macAddress)")var index = 0let aesPrivateKeyStartIndex = aesPrivateKey.startIndexlet macAddressstartIndex = macAddress.startIndex//Perform an XOR to get the device keyvar 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 arraylet deviceKey = "0x" + String(deviceKeyArray)let deviceKeyByte = Array<UInt8>(hex: deviceKey)//Convert the password to a byte arraylet passwordByte : Array<UInt8> = password.bytes//Convert the initialization vector to a byte arraylet aesIVHex = "0x" + AESIVlet aesIVByte = Array<UInt8>(hex: aesIVHex)//Encrypt the passwordvar 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 resultUserDefaultUtils.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 resultdeviceManager.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个值.

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

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

作为替代,我发现一个small website包含一个带符号的与无符号的类型位/十六进制转换器,这也可以解决问题. (请确保选择任何一个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");
总结

以上是内存溢出为你收集整理的iOS和Android AES加密(Java中没有UINT)全部内容,希望文章能够帮你解决iOS和Android AES加密(Java中没有UINT)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1210075.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-04
下一篇2022-06-04

发表评论

登录后才能评论

评论列表(0条)

    保存