ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-11-08 19:27:43  阅读:318  来源: 互联网

标签:uint aes ios java android


所有,

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

我正在通过蓝牙与设备通信的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默认情况下将字节显示为带符号值.但实际上,这些实际上是相等的.为了更加清楚,我将添加一个小表,其中包含您提供的示例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");

标签:uint,aes,ios,java,android
来源: https://codeday.me/bug/20191108/2009839.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有