1

Edit2 这个问题已经解决了。答案是关于我的另一个问题。感谢 Hot Licks。

-------- 单独编辑----------------------------- --------

Edit1: 我确定我的密文和密钥是正确的。因为我的同事用C++解密它没有问题。顺便说一下,它是ECB模式。

我为这个问题努力了一个星期,但我仍然无法解决它。我现在真的不知道该怎么办。

我正在开发一个可可应用程序,我的服务器返回一个通过 AES 加密的密文给我。我必须解密这个密文以获得一些信息才能进行下一步。

我的密文是:

NSString *aesString = @"8DFE2F9A9384573FA0FFAE17C3BEF4CC2BA056CC5CBBFAB57AC78C83AF4EF8A48EA1728D904E87613845377821E01E07CC6C9DEA35516DC595421E0FFC7CAA7A19671E713BB74D84D1DD4FB2C972DD8FACF5D74AD32662E992EA13D2417ABCAEE4137098322394BA76BADBAF4C1DB344704BBF9ED8A1513FF4B2E766526C98A2808B7AE67D1866CB9B489764E70662B1499D8D467A8817D1304AAB8F92EBDDD3E871CC1374CAE96A1428F0C2AFCB3F43E705CDD7649BDE5A363D59125980D0ACBBE32879B96AB15E93F74C08D435B787A8EE734E2773E16AE8F4CB4A5DDAD989F92CF1C609F0F8B81FEDB67F974AE583548B13C86B6FE1925A33A47CBDA8ED54C20F2D8020650BA7FABBF8CBAC00493B6DE423880EF1A1AF8CDDC457C064CD6150AA30D34456E422EA707C313C7FBB428CE1C4E534EA8EDD8C21B62D526522606E6944C4058E631DAFC6F9A539F9A4DFD8AFECCEF51A8A4904A3EFA0A76F3CA55AB56C726B0787D9EBB261CA91F5ABBF985096B327A6269399AC11A23CAFB0B3A6B3FB4AA357FC1644C89B7DEE396C51AF734738E598B765B2384EB8931F4B65D9F4B6C73EE3F1F6C5AA17871D14335153E4058AB9BC0FCAD35DBDB924D00B0A71B985E20CDF530FA5479E2DD8F14D269CE59DA365BAC03FCCB037963E7E3F175A09DD7EFC66E450DF5A16E7233EA55009E4891EF238D003D5837F077C12167428AD19D3DA45D569AA252D5FFD736E134AD3B5654FD82506822BE9B78731C9CA5EE56685153E657E8B385013FF14BBE5A1F5938801E94FE498495C2D94C84A937E1C36A4667A16DFECED471C3902B4B3D1DAA9F2C72A09AF736EF51EBE39F0006667D60DDC9EC20C5C29CC8933E5FB9E293560DB6B152DF4864BD9CF02B0D40D5B24D0326F1D7E251A0CBC692B68FA37212A57F34588D18D6F4F7E59D997A8C4B5E452FE883645B90BD7EE3A4B38754616CA7A9D430620036DBEAD2EC03BEBB5C69E92F9A33951C22E83E68DD85350C7";

我的关键是:

NSString *key = @"810B16E3541EF7A4";

我使用nicerobot'answer来解密我的密文并尝试许多其他方法。我希望有人能告诉我下一步该怎么做。

4

3 回答 3

0

AES accepts an initialization vector. What are you using for that?

If the data is in Hex or Base64, I don't see enough bits in your key for AES-128 (the shortest kind). If the key is the Ascii text of the hex string, that would be 128 bits but that seems a bit hokey. Such things happen but it would not be my first choice.

AES encrypts in multiple, not completely compatible modes, CBC and ECB are two I have worked with.

Anyway, I monkeyed around with this for fifteen minutes and I can’t get anything out of that but trash. I have to agree with HotLicks who suggested that you start with an encrypt/decrypt that runs within the same program.

Good luck, /Bob Bryan

于 2012-08-08T00:51:25.053 回答
0

使用以下代码将加密的字符串文本转换为 NSData 对象。

NSData* stringData = [encryptedStringText dataUsingEncoding:NSUTF8StringEncoding];

为 NSData 创建如下所示的类别,然后使用它对 NSData 内容进行 AES 解密。

NSData* unencryptedData = [stringData AES256DecryptWithKey:keyText];

将您的 NSData 对象转换为 NSString。

NSString* unencryptedStringText = [NSString stringWithUTF8String:[unencryptedData bytes]];

#pragma mark -
#pragma mark NSData Encryption Category

// ---------------------------------------------------------
// NSData Category to add Encryption
// ---------------------------------------------------------

@interface NSData (NSDataAESEncryption)
- (NSData *)AES256EncryptWithKey:(NSString *)key;
- (NSData *)AES256DecryptWithKey:(NSString *)key;
@end



@implementation NSData (NSDataAESEncryption)

- (NSData *)AES256EncryptWithKey:(NSString *)key {

    char keyPtr[kCCKeySizeAES256+1]; 

    bzero(keyPtr, sizeof(keyPtr)); 

    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];

    NSInteger dataLength = [self length];

    size_t bufferSize = dataLength + kCCBlockSizeAES128;
    void *buffer = malloc(bufferSize);

    size_t numBytesEncrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
                                          keyPtr, kCCKeySizeAES256,
                                          NULL ,
                                          [self bytes], dataLength, 
                                          buffer, bufferSize, 
                                          &numBytesEncrypted);
    if (cryptStatus == kCCSuccess) {
        return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
    }

    free(buffer); 

    return nil;
}

- (NSData *)AES256DecryptWithKey:(NSString *)key {

    char keyPtr[kCCKeySizeAES256+1]; 

    bzero(keyPtr, sizeof(keyPtr)); 

    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];

    NSInteger dataLength = [self length];

    size_t bufferSize = dataLength + kCCBlockSizeAES128;
    void *buffer = malloc(bufferSize);

    size_t numBytesDecrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
                                          keyPtr, kCCKeySizeAES256,
                                          NULL ,
                                          [self bytes], dataLength, 
                                          buffer, bufferSize, 
                                          &numBytesDecrypted);

    if (cryptStatus == kCCSuccess) {
        return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
    }

    free(buffer);

    return nil;
}


@end
于 2012-08-07T14:31:31.440 回答
-1

尝试这个

+ (NSData *)doCipher:(NSData *)dataIn
              iv:(NSData *)iv
             key:(NSData *)symmetricKey
         context:(CCOperation)encryptOrDecrypt // kCCEncrypt or kCCDecrypt
           error:(NSError **)error
{
    CCCryptorStatus ccStatus   = kCCSuccess;
    size_t          cryptBytes = 0;
    NSMutableData  *dataOut    = [NSMutableData dataWithLength:dataIn.length + kCCBlockSizeAES128];

    ccStatus = CCCrypt( encryptOrDecrypt,
                   kCCAlgorithmAES128,
                   0, //kCCOptionPKCS7Padding,
                   symmetricKey.bytes,
                   kCCKeySizeAES128,
                   iv.bytes,
                   dataIn.bytes,
                   dataIn.length,
                   dataOut.mutableBytes,
                   dataOut.length,
                   &cryptBytes);

    if (ccStatus == kCCSuccess) {
        dataOut.length = cryptBytes;
    }
    else {
        if (error) {
            *error = [NSError errorWithDomain:@"kEncryptionError"
                                     code:ccStatus
                                 userInfo:nil];
        }
        dataOut = nil;
    }

return dataOut;
}
于 2016-01-12T09:55:07.933 回答