首页 \ 问答 \ 如何使用stringByAppendingFormat传递加密的NSString?(How to pass an encrypted NSString with stringByAppendingFormat?)

如何使用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
更新时间:2022-04-09 16:04

最满意答案

最后一个参数是无符号的。 所以通过做-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).

相关问答

更多
  • 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 ...
  • 一些网址可供参考: 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 ...
  • 这适用于反向复制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 ...
  • 例如,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 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)