【问题标题】:AES algorithm implementation in iOSiOS中的AES算法实现
【发布时间】:2012-09-01 09:46:14
【问题描述】:

大家好,我正在努力将 AES 算法 .net 代码转换为目标 c。我经历了不同的 API,但结果总是客户端提供的不同 .net 代码我不是 .net 开发人员,所以我很挣扎。任何建议或代码 sn-p 表示赞赏.net 代码如下。

 Public Function AES_Encrypt(ByVal input As String, ByVal pass As String) As String
            Dim AES As New System.Security.Cryptography.RijndaelManaged
            Dim Hash_AES As New System.Security.Cryptography.MD5CryptoServiceProvider
            Dim encrypted As String = ""
            Try
                Dim hash(31) As Byte
                Dim temp As Byte() =     Hash_AES.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(pass))
                Array.Copy(temp, 0, hash, 0, 16)
                Array.Copy(temp, 0, hash, 15, 16)
                AES.Key = hash
                AES.Mode = Security.Cryptography.CipherMode.ECB
              Dim DESEncrypter As System.Security.Cryptography.ICryptoTransform = AES.CreateEncryptor
            Dim Buffer As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(input)
            encrypted = Convert.ToBase64String(DESEncrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))
        Catch ex As Exception
        End Try
        Return encrypted
    End Function
 
 
 
    Public Function AES_Decrypt(ByVal input As String, ByVal pass As String) As String
        Dim AES As New System.Security.Cryptography.RijndaelManaged
        Dim Hash_AES As New System.Security.Cryptography.MD5CryptoServiceProvider
        Dim decrypted As String = ""
        Try
            Dim hash(31) As Byte
            Dim temp As Byte() = Hash_AES.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(pass))
            Array.Copy(temp, 0, hash, 0, 16)
            Array.Copy(temp, 0, hash, 15, 16)
            AES.Key = hash
            AES.Mode = Security.Cryptography.CipherMode.ECB
            Dim DESDecrypter As System.Security.Cryptography.ICryptoTransform = AES.CreateDecryptor
            Dim Buffer As Byte() = Convert.FromBase64String(input)
            decrypted = System.Text.ASCIIEncoding.ASCII.GetString(DESDecrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))
 
        Catch ex As Exception
        End Try
        Return decrypted
    End Function*

【问题讨论】:

  • 你为什么要负责开发你根本不懂的加密软件?
  • 这段代码在 .net 开发人员中很受欢迎 stackoverflow.com/questions/5987186/… 所以我想知道可能有人已经在目标 c 中做过这件事,可能会分享他的想法。
  • 这个 .NET 代码非常糟糕。它使用的是 ECB 模式,永远不应该用于此类数据,并且具有弱密码到密钥算法。您是否有其他不那么不安全的选择?虽然可以转换为 ObjC,但我会先找到更好的代码。
  • @RobNapier 感谢您的帮助。我知道欧洲央行是不安全的,但我们的客户在他的服务器上使用这个代码,所以我们必须实现这个。此外,这段代码正在运行,我检查了我自己。
  • 我所说的“损坏”是指非常不安全,并不是说它不会来回转换数据。

标签: iphone objective-c ios aes


【解决方案1】:

在 iOS 中,您可以使用 CommonCrypto API:

size_t numBytesEncrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
                                      keyPtr, keySize,
                                      NULL /* initialization vector (optional) */,
                                      [someData bytes], dataLength, /* input */
                                      buffer, bufferSize, /* output */
                                      &numBytesEncrypted);
if (cryptStatus == kCCSuccess) {
    //the returned NSData takes ownership of the buffer and will free it on deallocation
    return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
}

然后解密:

size_t numBytesDecrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
                                      keyPtr, keySize,
                                      NULL /* initialization vector (optional) */,
                                      [self bytes], dataLength, /* input */
                                      buffer, bufferSize, /* output */
                                      &numBytesDecrypted);

if (cryptStatus == kCCSuccess) {
    //the returned NSData takes ownership of the buffer and will free it on deallocation
    return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
}

【讨论】:

  • 感谢您的代码。我没有实现任何 API,但到目前为止还没有运气。你能提供一点上面的解释,以便我可以用上面的.net代码映射吗?
猜你喜欢
  • 2020-01-12
  • 1970-01-01
  • 1970-01-01
  • 2012-11-10
  • 1970-01-01
  • 2012-07-12
  • 2012-10-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多