有人可以帮助将下面 iOS 代码生成的 base64 字符串解密为 C# 等效项。
我试图在 c# 中以“晚上 8 点在秘密地点与我会面”结束。
iOS 生成以下加密:qd+SGaij5KSBScuLs3TpJS/3Dew8fHTudcs/5MG7Q1kqcrZzZycMgTcQuEEEED5f
此 iOS 代码成功地按照 XCode 6 中的要求加密和解密数据。
预先感谢您的帮助和支持。
达伦
#import <CommonCrypto/CommonCryptor.h>
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *sData = @"Meet me at the secret location at 8pm";
NSString *sIv = @"4QesEr03HwE5H1C+ICw7SA==";
NSString *sKey = @"ovA6siPkyM5Lb9oNcnxLz676K6JK6FrJKk4efEUWBzg=";
NSData *dData = [sData dataUsingEncoding:NSUTF8StringEncoding];
NSData *dIv = [sIv dataUsingEncoding:NSUTF8StringEncoding];
NSData *dKey = [sKey dataUsingEncoding:NSUTF8StringEncoding];
NSData *dEncrypt = [self doCipher:dData iv:dIv key:dKey context:kCCEncrypt];
NSData *dDecrypt = [self doCipher:dEncrypt iv:dIv key:dKey context:kCCDecrypt];
NSString *sDecrypt = [[NSString alloc] initWithData:dDecrypt encoding:NSUTF8StringEncoding];
NSLog(@"Decrypted Data: %@",sDecrypt);
}
- (NSData *)doCipher:(NSData *)dataIn
iv:(NSData *)iv
key:(NSData *)symmetricKey
context:(CCOperation)encryptOrDecrypt
{
CCCryptorStatus ccStatus = kCCSuccess;
size_t cryptBytes = 0; // Number of bytes moved to buffer.
NSMutableData *dataOut = [NSMutableData dataWithLength:dataIn.length + kCCBlockSizeAES128];
ccStatus = CCCrypt( encryptOrDecrypt,
kCCAlgorithmAES128,
kCCOptionPKCS7Padding,
symmetricKey.bytes,
kCCKeySizeAES128,
iv.bytes,
dataIn.bytes,
dataIn.length,
dataOut.mutableBytes,
dataOut.length,
&cryptBytes);
if (ccStatus != kCCSuccess) {
NSLog(@"CCCrypt status: %d", ccStatus);
}
dataOut.length = cryptBytes;
NSString *base64 = [dataOut base64EncodedStringWithOptions:0];
NSLog(@"%@",base64);
return dataOut;
}