0

有人可以帮助将下面 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;
}
4

2 回答 2

3

每个平台上的加密文本现在显示为:

X/aopyVDDva/wJ3If/5i73JGjwth9eH45Opxaaswgb8Kx3ynh8BhBTFRrHT0/i3y

这是有效的 C# 代码

注意:从https://github.com/Pakhee/Cross-platform-AES-encryption提取的 C# 代码

namespace CryptoTest
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string iv = "4QesEr03HwE5H1C+ICw7SA=="; // origional iv 128 bits
            string key = "ovA6siPkyM5Lb9oNcnxLz676K6JK6FrJKk4efEUWBzg="; // origional key 256 bits
            string message = "Meet me at the secret location at 8pm";

            CryptLib cryptLib = new CryptLib();
            string encryptedText = cryptLib.encrypt(message, key, iv);

            string originalMessage = cryptLib.decrypt(encryptedText, key, iv);
            Debug.WriteLine(originalMessage);
        }
    }

    public class CryptLib
    {
        UTF8Encoding _enc;
        RijndaelManaged _rcipher;
        byte[] _key, _pwd, _ivBytes, _iv;

        private enum EncryptMode { ENCRYPT, DECRYPT };

        public CryptLib()
        {
            _enc = new UTF8Encoding();
            _rcipher = new RijndaelManaged();
            _rcipher.Mode = CipherMode.CBC;
            _rcipher.Padding = PaddingMode.PKCS7;
            _rcipher.KeySize = 256;
            _rcipher.BlockSize = 128;
            _key = new byte[32];
            _iv = new byte[_rcipher.BlockSize / 8]; //128 bit / 8 = 16 bytes
            _ivBytes = new byte[16];
        }

        private String encryptDecrypt(string _inputText, string _encryptionKey, EncryptMode _mode, string _initVector)
        {
            string _out = "";// output string
            _pwd = Encoding.UTF8.GetBytes(_encryptionKey);
            _ivBytes = Encoding.UTF8.GetBytes(_initVector);
            int len = _pwd.Length;
            if (len > _key.Length)
            {
                len = _key.Length;
            }
            int ivLenth = _ivBytes.Length;
            if (ivLenth > _iv.Length)
            {
                ivLenth = _iv.Length;
            }
            Array.Copy(_pwd, _key, len);
            Array.Copy(_ivBytes, _iv, ivLenth);
            _rcipher.Key = _key;
            _rcipher.IV = _iv;
            if (_mode.Equals(EncryptMode.ENCRYPT))
            {
                //encrypt
                byte[] plainText = _rcipher.CreateEncryptor().TransformFinalBlock(_enc.GetBytes(_inputText), 0, _inputText.Length);
                _out = Convert.ToBase64String(plainText);
            }
            if (_mode.Equals(EncryptMode.DECRYPT))
            {
                //decrypt
                byte[] plainText = _rcipher.CreateDecryptor().TransformFinalBlock(Convert.FromBase64String(_inputText), 0, Convert.FromBase64String(_inputText).Length);
                _out = _enc.GetString(plainText);
            }
            _rcipher.Dispose();
            return _out;// return encrypted/decrypted string
        }

        public string encrypt(string _plainText, string _key, string _initVector)
        {
            return encryptDecrypt(_plainText, _key, EncryptMode.ENCRYPT, _initVector);
        }

        public string decrypt(string _encryptedText, string _key, string _initVector)
        {
            return encryptDecrypt(_encryptedText, _key, EncryptMode.DECRYPT, _initVector);
        }
    }
}

这是工作的iOS代码

    #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 *dEncrypt = [self doCipher:dData key:sKey iv:sIv context:kCCEncrypt];
        NSData *base64 = [dEncrypt base64EncodedDataWithOptions:0];
        NSString *sBase64 = [[NSString alloc] initWithData:base64 encoding:NSUTF8StringEncoding];
        NSLog(@"Base64 String: %@",sBase64);

        NSData *dDecrypt= [self decrypt:dEncrypt key:sKey iv:sIv];
        NSString *sDecrypt = [[NSString alloc] initWithData:dDecrypt encoding:NSUTF8StringEncoding];
        NSLog(@"Decrypted Data: %@",sDecrypt);
    }

    - (NSData *)doCipher:(NSData *)plainText
                     key:(NSString *)key
                      iv:(NSString *)iv
                 context:(CCOperation)encryptOrDecrypt
    {
        NSUInteger dataLength = [plainText length];

        size_t buffSize = dataLength + kCCBlockSizeAES128;
        void *buff = malloc(buffSize);

        size_t numBytesEncrypted = 0;

        NSData *dIv = [iv dataUsingEncoding:NSUTF8StringEncoding];
        NSData *dKey = [key dataUsingEncoding:NSUTF8StringEncoding];

        CCCryptorStatus status = CCCrypt(encryptOrDecrypt,
                                         kCCAlgorithmAES128,
                                         kCCOptionPKCS7Padding,
                                         dKey.bytes, kCCKeySizeAES256,
                                         dIv.bytes,
                                         [plainText bytes], [plainText length],
                                         buff, buffSize,
                                         &numBytesEncrypted);
        if (status == kCCSuccess) {
            return [NSData dataWithBytesNoCopy:buff length:numBytesEncrypted];
        }

        free(buff);
        return nil;
    }
于 2014-10-10T08:54:22.767 回答
1

给定的 base64 字符串qd+SGaij5KSBScuLs3TpJS/3Dew8fHTudcs/5MG7Q1kqcrZzZycMgTcQuEEEED5f是一个双向包。

首先它必须进行 base64 解码(这在 C# 中很容易),然后必须使用正确的算法对其进行解密。根据给定的参数,它希望使用 AES 或 Rijndael 算法(此处的示例代码),您必须在其中放入 base64 解码字节、密钥和 IV。

在那之后,你希望得到你想要的数据。

所以我自己写了一个例子,我可以用 C# 加密和解密指定的消息。

但检索到的加密消息与 iOS 生成的消息不同

您的 iOS 消息:qd+SGaij5KSBScuLs3TpJS/3Dew8fHTudcs/5MG7Q1kqcrZzZycMgTcQuEEEED5f 我的 C# 消息:wC2dn+QVhOhf+NqZVskopLdh2XDAdG3SmczrKF+TjQsGfxu07BEhRdiDqkbGY80O

我只是做了一些小改动,然后再深入一点。我看到的一件事是您在 iOS 代码中说密钥的长度为 256 位。但是您提供的密钥只有 128 位的长度。在这种情况下,C# 似乎只是切换回 128 位密钥长度(因为在下面的代码中更改密钥大小不会改变加密字节)。但也许 iOS 中的 CCCrypt 会有所不同。因此,要么在您的 iOS 代码中采用密钥大小,要么提供 256 位密钥,然后再次比较 iOS 和 C# 的结果。

例子:

var message = @"Meet me at the secret location at 8pm";
var iv = @"4QesEr03HwE5H1C+ICw7SA==";
var key = @"ovA6siPkyM5Lb9oNcnxLz676K6JK6FrJKk4efEUWBzg=";

byte[] encryptedBytes;

using (var aesCryptoProvider = new AesCryptoServiceProvider())
{
    aesCryptoProvider.BlockSize = 128;
    aesCryptoProvider.KeySize = 256;
    aesCryptoProvider.IV = Convert.FromBase64String(iv);
    aesCryptoProvider.Key = Convert.FromBase64String(key);
    aesCryptoProvider.Padding = PaddingMode.PKCS7;
    aesCryptoProvider.Mode = CipherMode.CBC;

    using (var encryptor = aesCryptoProvider.CreateEncryptor())
    using (var memoryStream = new MemoryStream())
    using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
    using (var streamWriter = new StreamWriter(cryptoStream, Encoding.UTF8))
    {
        streamWriter.Write(message);
        streamWriter.Close();
        encryptedBytes = memoryStream.ToArray();
    }
}

var encryptedMessage = Convert.ToBase64String(encryptedBytes);
Console.WriteLine(encryptedMessage);

using (var aesCryptoProvider = new AesCryptoServiceProvider())
{
    aesCryptoProvider.BlockSize = 128;
    aesCryptoProvider.KeySize = 256;
    aesCryptoProvider.IV = Convert.FromBase64String(iv);
    aesCryptoProvider.Key = Convert.FromBase64String(key);
    aesCryptoProvider.Padding = PaddingMode.PKCS7;
    aesCryptoProvider.Mode = CipherMode.CBC;

    using (var decryptor = aesCryptoProvider.CreateDecryptor())
    using (var memoryStream = new MemoryStream(Convert.FromBase64String(encryptedMessage)))
    using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
    using (var streamReader = new StreamReader(cryptoStream, Encoding.UTF8))
    {
        Console.WriteLine(streamReader.ReadToEnd());
    }
}
于 2014-10-08T09:33:29.183 回答