我需要在 iPhone 上加密一个字符串并将其发送到 .Net 以使用 Triple DES 进行解密。我能够在 iPhone 和 .Net 上加密/解密,但在两个平台上我得到不同的结果。
我在这里使用与 .Net 和 iPhone 之间的 AES 加密/解密相同的代码
我唯一改变.net的是加密算法,所以它说AesCryptoServiceProvider,我把TripleDesServiceProvider
和.net一样,我唯一改变的是加密算法,所以在它说 kCCAlgorithmAES128 的地方,我把 kCCAlgorithm3DES
我错过了什么?
[更新]
感谢您的回复。
如果我留在同一个平台上,我可以毫无问题地进行加密/解密,但如果我在 iPhone 中加密并在 .net 中解密,则会出现问题,因为在每个平台上使用相同的输入会有不同的结果。
正如 Overslacked 所说,我认为问题与盐有关,但我找不到该算法在每个平台中使用的任何 sha 或 md5 文档,也找不到任何参数来自定义它。
这是我在 iPhone 中实际使用的代码:
int main(int argc, char *argv[]){
NSString * _secret = @"hello";
NSString * _key = @"1234567890";
_out = [self doCipher:_secret key:_key context:kCCEncrypt];
NSLog(@"encrypted data in str: %@", _out);
_outDecrypted = [StringEncryption doCipher:_out key:_key context:kCCDecrypt];
NSLog(@"decrypted data in str: %@", _outDecrypted);
}
+ (NSString *)doCipher:(NSString *)sTextIn key:(NSString *)sKey
context:(CCOperation)encryptOrDecrypt {
NSMutableData * dTextIn;
if (encryptOrDecrypt == kCCDecrypt) {
dTextIn = [[[NSData alloc] base64DecodeString:sTextIn ]mutableCopy];
}
else{
dTextIn = [[sTextIn dataUsingEncoding: NSASCIIStringEncoding]mutableCopy];
}
NSMutableData * dKey = [[sKey dataUsingEncoding:NSASCIIStringEncoding]mutableCopy];
[dKey setLength:24];
uint8_t *bufferPtr1 = NULL;
size_t bufferPtrSize1 = 0;
size_t movedBytes1 = 0;
uint8_t iv[kCCBlockSize3DES];
memset((void *) iv, 0x0, (size_t) sizeof(iv));
bufferPtrSize1 = ([sTextIn length] + kCCBlockSize3DES) & ~(kCCBlockSize3DES -1);
bufferPtr1 = malloc(bufferPtrSize1 * sizeof(uint8_t));
memset((void *)bufferPtr1, 0x00, bufferPtrSize1);
ccStatus = CCCrypt(encryptOrDecrypt, // CCOperation op
kCCAlgorithm3DES, // CCAlgorithm alg
kCCOptionPKCS7Padding, // CCOptions options
[dKey bytes], // const void *key
[dKey length], // size_t keyLength
iv, // const void *iv
[dTextIn bytes], // const void *dataIn
[dTextIn length], // size_t dataInLength
(void *)bufferPtr1, // void *dataOut
bufferPtrSize1, // size_t dataOutAvailable
&movedBytes1); // size_t *dataOutMoved
NSString * sResult;
if (encryptOrDecrypt == kCCDecrypt){
sResult = [[[ NSString alloc] initWithData:[NSData dataWithBytes:bufferPtr1
length:movedBytes1] encoding:NSASCIIStringEncoding] autorelease];
}
else {
NSData *dResult = [NSData dataWithBytes:bufferPtr1 length:movedBytes1];
sResult = [dResult base64EncodeData:dResult];
}
return sResult;
}
这是我用于 .net 的代码
class Program
{
static void Main(string[] args)
{
string key = "1234567890";
string secret = "hello";
string crypto = EncryptedString.EncryptString(secret, key);
Console.WriteLine(crypto);
secret = EncryptedString.DecryptString(crypto, key);
Console.WriteLine(secret);
Main(null);
}
}
public class EncryptedString
{
public static string EncryptString(string plainSourceStringToEncrypt, string passPhrase)
{
//Set up the encryption objects
using (TripleDESCryptoServiceProvider acsp = GetProvider(Encoding.ASCII.GetBytes(passPhrase)))
{
byte[] sourceBytes = Encoding.ASCII.GetBytes(plainSourceStringToEncrypt);
ICryptoTransform ictE = acsp.CreateEncryptor();
//Set up stream to contain the encryption
MemoryStream msS = new MemoryStream();
//Perform the encrpytion, storing output into the stream
CryptoStream csS = new CryptoStream(msS, ictE, CryptoStreamMode.Write);
csS.Write(sourceBytes, 0, sourceBytes.Length);
csS.FlushFinalBlock();
//sourceBytes are now encrypted as an array of secure bytes
byte[] encryptedBytes = msS.ToArray(); //.ToArray() is important, don't mess with the buffer
String b64 = System.Text.ASCIIEncoding.ASCII.GetString(encryptedBytes);
return Convert.ToBase64String(encryptedBytes);
}
}
public static string DecryptString(string base64StringToDecrypt, string passphrase)
{
//Set up the encryption objects
using (TripleDESCryptoServiceProvider acsp = GetProvider(Encoding.Default.GetBytes(passphrase)))
{
byte[] RawBytes = Convert.FromBase64String(base64StringToDecrypt);
ICryptoTransform ictD = acsp.CreateDecryptor();
MemoryStream msD = new MemoryStream(RawBytes, 0, RawBytes.Length);
CryptoStream csD = new CryptoStream(msD, ictD, CryptoStreamMode.Read);
return (new StreamReader(csD)).ReadToEnd();
}
}
private static TripleDESCryptoServiceProvider GetProvider(byte[] key)
{
TripleDESCryptoServiceProvider result = new TripleDESCryptoServiceProvider();
result.Mode = CipherMode.CBC;
result.Padding = PaddingMode.PKCS7;
result.IV = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 };
result.Key = key;
return result;
}
}