我需要哪种编码来解密此AES128数据

时间:2016-04-21 15:25:51

标签: ios objective-c nsstring aes nsdata

我使用此Objective-C类别来加密和解密我的字符串数据:https://gist.github.com/leeprobert/b83f07ca90ad657a1cd17f94b13138b1

我有一个像这样的NSString类别:

@implementation NSString (AESCrypt)

- (NSString *)AES128EncryptWithKey:(NSString *)key
{
    NSData *plainData = [self dataUsingEncoding:NSUTF8StringEncoding];
    NSData *encryptedData = [plainData AES128EncryptedDataWithKey:key];

    NSString *encryptedString = [encryptedData base64Encoding];

    return encryptedString;
}

- (NSString *)AES128DecryptWithKey:(NSString *)key
{
    NSData *plainData = [self dataUsingEncoding:NSUTF8StringEncoding];
    NSData *decryptedData = [plainData AES128DecryptedDataWithKey:key];

    NSString *decryptedString = [[NSString alloc] initWithData:decryptedData encoding:NSUTF8StringEncoding];

    return decryptedString;
}

@end

我的问题是,当我测试这个时,我会把垃圾拿出来:

NSString* stringToEncrypt = @"Mary had a little lamb";
        NSString* encryptedString = [stringToEncrypt AES128EncryptWithKey:eKey];
        NSString* decryptedString = [encryptedString AES128DecryptWithKey:eKey];

        NSLog(@"encryptedString = %@, decryptedString = %@", encryptedString, decryptedString);

这将记录为:

encryptedString = wLnJKED9oE4zC8dS9X7XskTs8kMTP59LUj8aatxW9+c=, decryptedString = (null)

我不相信我需要在加密过程中执行base64encoding。这部分是别人的代码。我正在尝试解密。

1 个答案:

答案 0 :(得分:2)

您在解密期间没有考虑Base64编码。而不是public class OrderByTagPredicate implements PredicateEvaluator { private static final Logger log = Logger.getLogger(OrderByTagPredicate.class.getSimpleName()); public OrderByTagPredicate() { super(); } @Override public Comparator<Row> getOrderByComparator(final Predicate predicate, final EvaluationContext context) { return new Comparator<Row>() { @Override public int compare(Row o1, Row o2) { // TODO Auto-generated method stub return 0; } }; } @Override public boolean canFilter(Predicate arg0, EvaluationContext arg1) { return true; } @Override public boolean canXpath(Predicate arg0, EvaluationContext arg1) { return true; } @Override public FacetExtractor getFacetExtractor(Predicate arg0, EvaluationContext arg1) { return null; } @Override public String[] getOrderByProperties(Predicate arg0, EvaluationContext arg1) { return null; } @Override public String getXPathExpression(Predicate arg0, EvaluationContext arg1) { return null; } @Override public boolean includes(Predicate arg0, Row arg1, EvaluationContext arg2) { return true; } @Override public boolean isFiltering(Predicate arg0, EvaluationContext arg1) { return false; } 您应该使用[self dataUsingEncoding:NSUTF8StringEncoding]

您是否想要使用Base64只取决于您正在做什么。这是获取NSData(加密输出/解密输入)并将其转换为带有小字符集([[NSData alloc] initWithBase64EncodedString:self options:0])的字符串的便捷方式。