1

我正在尝试加密 iPhone 上的数据并将加密的文本发送到 Web 服务以供他们解密。如果解密有效,那么它将返回 xml 中的名字作为确认工作。这是我的 Xcode

注意:“key”在 xcode 和 web 服务中是相同的

我要加密的信息:

NSString *fnameencrypted = [[NSString alloc] AES256EncryptWithKey:f_name.text withKey:key]];    
NSString *lnameencrypted = [[NSString alloc] AES256EncryptWithKey:l_name.text withKey:key]];

NSString 方法

-(NSString *)AES256EncryptWithKey:(NSString *)plaintext withKey:(NSString *)key{
    NSData *plainData = [plaintext dataUsingEncoding:NSASCIIStringEncoding];
    NSData *encryptedData = [plainData AES256EncryptWithKey:key];  
    NSString *encryptedString = [encryptedData base64Encoding];
    return encryptedString;
}

编辑

加密方式

-(NSData *)AES256EncryptWithKey:(NSString *)key{
    char keyPtr[kCCKeySizeAES256 + 1]; // room for terminator (unused)
    bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)

    // fetch key data
    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSASCIIStringEncoding];

    NSUInteger dataLength = [self length];

    //See the doc: For block ciphers, the output size will always be less than or 
    //equal to the input size plus the size of one block.
    //That's why we need to add the size of one block here
    size_t bufferSize = dataLength + kCCBlockSizeAES128;
    void *buffer = malloc( bufferSize );

    size_t numBytesEncrypted = 0;

    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionECBMode + kCCOptionPKCS7Padding,
                                      keyPtr, kCCKeySizeAES256,
                                      NULL /* initialization vector (optional) */,
                                      [self 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];
    }

    free(buffer); //free the buffer
    return nil;
}

这是我的网络服务代码

private static string Decrypt(string encryptedText, string completeEncodedKey, int keySize)
{
    RijndealManaged aesEncryption = new RijndealManaged();
    aesEncryption.KeySize = keySize; //keySize is 256
    aesEncryption.BlockSize = 128;
    aesEncryption.Mode = CipherMode.ECB;
    aesEncryption.Padding = PaddingMode.PKCS7;
    aesEncryption.IV = Convert.FromBase64String(ASCIIEncoding.ACSII.GetString(Convert.FromBase64String(completeEncodedString)).Split(',')[0]);
    aesEncryption.Key = Convert.FromBase64String(ASCIIEncoding.ACSII.GetString(Convert.FromBase64String(completeEncodedString)).Split(',')[1]);
    ICryptoTransform decrypto = aesEncryption.CreateDecryptor();
    byte[] encryptedBytes = Convert.FromBase64CharArray(encryptedText.ToCharArray(), 0, encryptedText.Length);
    return ASCIIEncoding.ASCII.GetString(decrypto.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length);
}

此代码不起作用,因为它返回

<response><return>0</return></response>
4

0 回答 0