如何生成这种字符串模式作为加密密钥

时间:2013-05-06 09:54:36

标签: ios nsstring nsarray

您好我正在尝试生成这种NSString模式,我的真实string是“abcdefgh”我想通过使用

获取相应的字节
NSData *data=[mystring dataUsingEncoding:NSUTF8StringEncoding]
NSLog(@"%d",[data bytes]);  

将这些值复制到byte array中,并将总数组大小限制为24bit

然后我想创建以下模式

"abcdefgh efghabcd hgfedcba"

如何生成这种密钥?

请帮帮我

1 个答案:

答案 0 :(得分:0)

我认为这会奏效。 reverseString的逻辑取自this answer

#import "NSString+CustomCrypto.h"

@implementation NSString (CustomCrypto)

- (NSString *)reversedString{

    NSMutableString *reversedString = [NSMutableString stringWithCapacity:[self length]];

    [self enumerateSubstringsInRange:NSMakeRange(0,[self length])
                                 options:(NSStringEnumerationReverse | NSStringEnumerationByComposedCharacterSequences)
                              usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
                                  [reversedString appendString:substring];
                              }];

    return [NSString stringWithString:reversedString];
}

- (NSString *)splicedString{

    NSInteger index = [self length]/2;

    NSString *subString = [self substringFromIndex:index];
    NSString *secondString = [self substringToIndex:index];

    return [subString stringByAppendingString:secondString];
}

- (NSString *)customCryptoString{

    NSString *splicedString = [self splicedString];
    NSString *reversedString = [self reversedString];

    return [NSString stringWithFormat:@"%@ %@ %@",self, splicedString, reversedString];
}

调用字符串

的类别方法
NSLog(@"%@",[@"abcdefgh" customCryptoString]);
  

输出:abcdefgh efghabcd hgfedcba