不同输出的object-c和c# 3DES加密

本文关键字:3DES 加密 object-c 输出 | 更新日期: 2023-09-27 18:14:41

我有以下问题,我想把我的字符串加密到我的web服务,并在那里解密它。但是当我用object-c加密时,我得到了不同的输出有人能告诉我我做错了什么吗?

我的加密对象-c:

+ (NSString*)encryptData:(NSData*)inputData 
{
    NSData * key = [@"FeljWsN+uaEB9+jqDnPRQeIi" dataUsingEncoding:NSUTF8StringEncoding];
    NSMutableData* outputData = [NSMutableData dataWithLength:(inputData.length + kCCBlockSize3DES)];
    size_t outLength;
    CCCryptorStatus result = CCCrypt(kCCEncrypt,                // CCOperation op
                                     kCCAlgorithm3DES,          // CCAlgorithm alg
                                     kCCOptionPKCS7Padding,     // CCOptions options
                                     key.bytes,                 // const void *key
                                     key.length,                // size_t keyLength
                                     nil,                       // const void *iv
                                     inputData.bytes,           // const void *dataIn
                                     inputData.length,          // size_t dataInLength
                                     outputData.mutableBytes,   // void *dataOut
                                     outputData.length,         // size_t dataOutAvailable
                                     &outLength);               // size_t *dataOutMoved
    if (result != kCCSuccess)
        return nil;
    [outputData setLength:outLength];
    NSString * outputString = [outputData base64EncodingWithLineLength:0];
    return outputString;
}

我用c#加密:

public string Encrypt(string toEncrypt)
{
    string retValue = "";
    try
    {
        byte[] keyArray;
        byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
        keyArray = UTF8Encoding.UTF8.GetBytes("FeljWsN+uaEB9+jqDnPRQeIi");
        TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
        tdes.Key = keyArray;
        tdes.Mode = CipherMode.ECB;
        tdes.Padding = PaddingMode.PKCS7;
        ICryptoTransform cTransform = tdes.CreateEncryptor();
        byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
        retValue = Convert.ToBase64String(resultArray, 0, resultArray.Length);
    }
    catch 
    {
    }
    return retValue;
}

object - c:

NSData * _secretData = [@"Christiaan" dataUsingEncoding:NSUTF8StringEncoding];
NSString * encryptedString = [TripleDES encryptData: _secretData];

给我"Lp3x2fm2jrK1ftsHoZN9cg=="和c#

Encryption encryptionSet = new Encryption();
string encryptedString = encryptionSet.Encrypt("Christiaan");

给我"Lp3x2fm2jrJ7ghY9SXRlUw=="

不同输出的object-c和c# 3DES加密

需要在kCCOptionPKCS7Padding:

后面添加"| kCCOptionECBMode"
CCCryptorStatus result = CCCrypt(kCCEncrypt,                // CCOperation op
                                kCCAlgorithm3DES,           // CCAlgorithm alg
                                kCCOptionPKCS7Padding | kCCOptionECBMode, // CCOptions options
                                key.bytes,                 // const void *key
                                key.length,                // size_t keyLength
                                nil,                       // const void *iv
                                inputData.bytes,           // const void *dataIn
                                inputData.length,          // size_t dataInLength
                                outputData.mutableBytes,   // void *dataOut
                                outputData.length,         // size_t dataOutAvailable
                                &outLength);               // size_t *dataOutMoved

注意:这是从问题中提取出来的,并代表OP在这里发布。