如何使用stringByAppendingFormat传递加密的NSString?(How to pass an encrypted NSString with stringByAppendingFormat?)
我创建了一个as.net web api来接收用户身份验证的凭据。 在我的iOS应用程序中,我将用户名和密码发送到网址。 我只想加密密码。
如何发送加密密码,然后我可以在asp.net中解密?
我的代码:
NSString *username = Username.text; NSString *password = Password.text; NSData *dataToEnc = [Password.text dataUsingEncoding:NSUTF8StringEncoding]; NSData *encryptedData = [dataToEnc AES256EncryptWithKey:password]; NSURL *urlFormat = [NSURL URLWithString:[API_LOGIN stringByAppendingFormat:@"%@/%@",username, encryptedData]]; NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:urlFormat cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:60]; NSData *returnData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:nil error:nil]; NSString *responseString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
这不起作用。
I created an as.net web api to receive users credentials to authenticate. In my iOS app I am sending the username and password to the url. I only want to encrypt the password.
How to send an encrypted password that I can then decrypt it in asp.net?
My code:
NSString *username = Username.text; NSString *password = Password.text; NSData *dataToEnc = [Password.text dataUsingEncoding:NSUTF8StringEncoding]; NSData *encryptedData = [dataToEnc AES256EncryptWithKey:password]; NSURL *urlFormat = [NSURL URLWithString:[API_LOGIN stringByAppendingFormat:@"%@/%@",username, encryptedData]]; NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:urlFormat cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:60]; NSData *returnData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:nil error:nil]; NSString *responseString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
This does not work.
原文:https://stackoverflow.com/questions/27108588
最满意答案
最后一个参数是无符号的。 所以通过做-100 * sizeof(源),你会得到一个巨大的数字(那将环绕,即溢出)。
这相当于做“4,294,967,196 * sizeof(source)”。
编辑:其实这是错的我刚才意识到。 它将执行-100 * sizeof(源),然后将其转换为无符号。 例如,如果sizeof(source)为4,那么它将-400转换为无符号并给你0xFFFFFE70(4,294,966,896)。
the last parameter is unsigned. so by doing -100 * sizeof( source ) you'll get a huge number (That will wrap around, ie overflow).
This is equivalent to doing "4,294,967,196 * sizeof( source )".
Edit: Actually thats wrong I just realised. It will do -100 * sizeof( source ) and then convert it to an unsigned. For example if sizeof( source ) is 4 then it will convert -400 to unsigned and give you 0xFFFFFE70 (4,294,966,896).
相关问答
更多-
TCP/IP模型是一个________。[2023-10-02]
a -
C11说: (C11,7.24.2.1p2)“memcpy函数将s2指向的对象中的n个字符复制到s1指向的对象中。” &a + 1本身是指向整数加法的有效指针,但&a + 1不是指向对象的指针,因此调用会调用未定义的行为。 C11 says: (C11, 7.24.2.1p2) "The memcpy function copies n characters from the object pointed to by s2 into the object pointed to by s1." &a + 1 ...
-
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
一些网址可供参考: https : //github.com/nodejs/node/ https://github.com/nodejs/node/blob/master/src/stream_wrap.cc https://github.com/nodejs/node/blob/master/src/stream_base.cc https://github.com/libuv/libuv/blob/v1.x/src/unix/stream.c https://github.com/libuv/libu ...
-
C memcpy相反(C memcpy in reverse)[2022-08-21]
这适用于反向复制int s: void reverse_intcpy(int *restrict dst, const int *restrict src, size_t n) { size_t i; for (i=0; i < n; ++i) dst[n-1-i] = src[i]; } 就像memcpy() , dst和src指向的区域不能重叠。 如果你想在原地逆转: void reverse_ints(int *data, size_t n) { size ... -
最后一个参数是无符号的。 所以通过做-100 * sizeof(源),你会得到一个巨大的数字(那将环绕,即溢出)。 这相当于做“4,294,967,196 * sizeof(source)”。 编辑:其实这是错的我刚才意识到。 它将执行-100 * sizeof(源),然后将其转换为无符号。 例如,如果sizeof(source)为4,那么它将-400转换为无符号并给你0xFFFFFE70(4,294,966,896)。 the last parameter is unsigned. so by doing ...
-
C带符号整数的攻击(C signed-integer-based attacks)[2023-12-25]
例如,FreeBSD的getpeername函数有一个错误。 为了说明这一点,我们void copyFromKernel(char* dest, int size)一个函数void copyFromKernel(char* dest, int size) ,它从有限的内存区size字节中进行复制。 正如你可能已经知道的那样,memcpy函数是这样声明的: void * memcpy ( void * destination, const void * source, size_t num ); 其中size ... -
你的程序不太正确。 您需要删除对memcpy的调用以避免偶然的,难以诊断的错误。 从realloc手册页 realloc()函数将ptr指向的内存块大小更改为字节大小。 内容在从区域开始到新旧尺寸的最小范围内保持不变 所以,你不需要在realloc之后调用memcpy 。 事实上,这样做是错误的,因为您之前的堆单元可能已经在realloc调用中被释放。 如果它被释放,它现在指向内存不可预知的内存。 Your program is not quite correct. You need to remove t ...
-
我会说memcpy不是这样做的方法。 但是,找到最佳方法很大程度上取决于数据在内存中的存储方式。 首先,您不想获取目标变量的地址。 如果它是局部变量,您将强制它到堆栈而不是让编译器选择将它放在处理器寄存器中。 仅此一点可能非常昂贵。 最通用的解决方案是逐字节读取数据并对结果进行算术组合。 例如: uint16_t res = ( (((uint16_t)char_array[high]) << 8) | char_array[low]); 32位情况下的表达式有点复杂,因 ...
-
您取消引用char *,导致char,然后将该1字节char转换为int,而不是整个4字节地址(如果这是32位计算机,则64位是8个字节)。 4472832是十六进制的444000。 在一个小端机器上,你抓住最后的00。 *((unsigned int*)myData) 应导致显示正确的号码。 You dereference a char *, resulting in a char, and then cast that 1-byte char to an int, not the entire 4 b ...