I have the following problem I would like to send my string encrqpted to my webservice and decrypt it there. But i got different output when I encrypt with object-c can somebody tell me what i'm doing wrong?
My encryption with object-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;
}
My encryption with 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];
gives me "Lp3x2fm2jrK1ftsHoZN9cg==" and C#
Encryption encryptionSet = new Encryption();
string encryptedString = encryptionSet.Encrypt("Christiaan");
gives me "Lp3x2fm2jrJ7ghY9SXRlUw=="
needed to add "| kCCOptionECBMode" behind the kCCOptionPKCS7Padding:
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
Note: this was extracted from the question and posted here on the OP's behalf.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.