Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# AES解密在iOS上不工作在Android使用的FBEncryptior库上工作_C#_Android_Ios_Encryption_Aes - Fatal编程技术网

C# AES解密在iOS上不工作在Android使用的FBEncryptior库上工作

C# AES解密在iOS上不工作在Android使用的FBEncryptior库上工作,c#,android,ios,encryption,aes,C#,Android,Ios,Encryption,Aes,我正在用c做AES解密,因为解密后的数据在解密后的最后几个字符显示了一些垃圾数据 我使用下面的代码进行解密 + (NSData*)decryptData:(NSData*)data key:(NSData*)key iv:(NSData*)iv; { NSData* result = nil; // setup key unsigned char cKey[FBENCRYPT_KEY_SIZE]; bzero(cKey, sizeof(cKey)); [key getBytes:cKey

我正在用c做AES解密,因为解密后的数据在解密后的最后几个字符显示了一些垃圾数据

我使用下面的代码进行解密

+ (NSData*)decryptData:(NSData*)data key:(NSData*)key iv:(NSData*)iv;
{
   NSData* result = nil;

// setup key
unsigned char cKey[FBENCRYPT_KEY_SIZE];
bzero(cKey, sizeof(cKey));
[key getBytes:cKey length:FBENCRYPT_KEY_SIZE];

// setup iv
char cIv[FBENCRYPT_BLOCK_SIZE];
bzero(cIv, FBENCRYPT_BLOCK_SIZE);
NSLog(@"CIV2 : %s",cIv);

if (iv) {
    [iv getBytes:cIv length:FBENCRYPT_BLOCK_SIZE];
}

// setup output buffer
size_t bufferSize = [data length] + FBENCRYPT_BLOCK_SIZE;
void *buffer = malloc(bufferSize);

// do decrypt
size_t decryptedSize = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt,
                                      FBENCRYPT_ALGORITHM,  //kCCAlgorithmAES128
                                      kCCOptionPKCS7Padding,
                                      cKey,                    //16
                                      FBENCRYPT_KEY_SIZE,   //16
                                      cKey,
                                      [data bytes],
                                      [data length],
                                      buffer,
                                      bufferSize,
                                      &decryptedSize);

if (cryptStatus == kCCSuccess) {

    result = [NSData dataWithBytesNoCopy:buffer length:decryptedSize];
} else {
    free(buffer);
    NSLog(@"[ERROR] failed to decrypt| CCCryptoStatus: %d", cryptStatus);
}

return result;
}

若您在调试中看到结果,它将显示最后一些字符吃垃圾,但解密完整的数据

NSData* data = [self decryptData:encryptedData
                             key:[keyString dataUsingEncoding:NSUTF8StringEncoding]
                              iv:nil];

if (data) {
    return [[NSString alloc] initWithData:data
                                  encoding:NSUTF8StringEncoding];
}

数据正在返回零值

下面是C代码

public RijndaelManaged GetRijndaelManaged(String secretKey)
{
    var keyBytes = new byte[16];
    var secretKeyBytes = Encoding.UTF8.GetBytes(secretKey);
    Array.Copy(secretKeyBytes, keyBytes, Math.Min(keyBytes.Length, secretKeyBytes.Length));
    return new RijndaelManaged
    {
        Mode = CipherMode.CBC,
        Padding = PaddingMode.PKCS7,
        KeySize = 128,
        BlockSize = 128,
        Key = keyBytes,
        IV = keyBytes
    };
}

public byte[] Encrypt(byte[] plainBytes, RijndaelManaged rijndaelManaged)
{
    return rijndaelManaged.CreateEncryptor()
        .TransformFinalBlock(plainBytes, 0, plainBytes.Length);
}

public byte[] Decrypt(byte[] encryptedData, RijndaelManaged rijndaelManaged)
{
    return rijndaelManaged.CreateDecryptor()
        .TransformFinalBlock(encryptedData, 0, encryptedData.Length);
}

/// <summary>
/// Encrypts plaintext using AES 128bit key and a Chain Block Cipher and returns a base64 encoded string
/// </summary>
/// <param name="plainText">Plain text to encrypt</param>
/// <param name="key">Secret key</param>
/// <returns>Base64 encoded string</returns>
public String Encrypt(String plainText, String key)
{
    var plainBytes = Encoding.UTF8.GetBytes(plainText);
    return Convert.ToBase64String(Encrypt(plainBytes, GetRijndaelManaged(key)));
}

/// <summary>
/// Decrypts a base64 encoded string using the given key (AES 128bit key and a Chain Block Cipher)
/// </summary>
/// <param name="encryptedText">Base64 Encoded String</param>
/// <param name="key">Secret Key</param>
/// <returns>Decrypted String</returns>
public String Decrypt(String encryptedText, String key)
{
    var encryptedBytes = Convert.FromBase64String(encryptedText);
    return Encoding.UTF8.GetString(Decrypt(encryptedBytes, GetRijndaelManaged(key)));
}
从iOS解密的数据:{“成功”: “abcdefghijklmnopqrstuvxx``~

应为解密数据:{“成功”: “abcdefghijklmnopqrstuvwxyz”}


加密数据包含十六进制
227d

数据十六进制表示法:
7B22537566365646564223A226162636465666768696A6C6D6E6F707172737475767778797A27D

字符串表示法:
{“成功”:“abcdefghijklmnopqrstuvwxyz”}

注:
最好使用全长密钥,对于AES 16、24或32字节,您的密钥是14字节,将使用一些未定义的方法来扩展它。此外,IV必须是锁大小,AES为16字节。您很幸运,实现之间的填充似乎相同(0x00),不要依赖运气

测试代码:

// Explicit null pad the key and IV to correct lengths
NSData *ivData  = [@"e64f9fa01f0418\x00\x00" dataUsingEncoding:NSUTF8StringEncoding];
NSData *keyData = [@"e64f9fa01f0418\x00\x00" dataUsingEncoding:NSUTF8StringEncoding];
NSString *base64EncryptedString = @"0Nwr30kSkJxGCYiFg8TUrfW51587SUkS8lQ7Uno7gglxjVGW5gR4MA+isTknjXzK";
NSData *encryptedData = [[NSData alloc] initWithBase64EncodedString:base64EncryptedString options:0];

NSMutableData *plainData = [NSMutableData dataWithLength: encryptedData.length];
size_t movedBytes = 0;

CCCryptorStatus ccStatus;
ccStatus = CCCrypt(kCCDecrypt,
                   kCCAlgorithmAES128,
                   kCCOptionPKCS7Padding,
                   keyData.bytes, kCCKeySizeAES128,
                   ivData.bytes,
                   encryptedData.bytes, encryptedData.length,
                   plainData.mutableBytes, plainData.length,
                   &movedBytes);

plainData.length = movedBytes;
NSLog(@"Data: %@",plainData);
NSString *decryptedString = [[NSString alloc] initWithData:plainData encoding:NSUTF8StringEncoding];
NSLog(@"String: %@",decryptedString);

这是可怕的不安全。IV不是一个秘密,而密钥是。因为你使用的密钥也作为IV,那么就没有实际的安全性。哦,谢谢,那么请建议我应该在代码中更改什么。创建一个随机IV,只需在加密数据的前面加上前缀,用于解密,它不需要保密。IV的问题是解决的lved…现在在解密数据获取开始时使用您的代码~ìïЈyÊwÒ…e,这个字符串。复制并粘贴代码作为起点。“在解密数据获取开始时”是什么意思?
// Explicit null pad the key and IV to correct lengths
NSData *ivData  = [@"e64f9fa01f0418\x00\x00" dataUsingEncoding:NSUTF8StringEncoding];
NSData *keyData = [@"e64f9fa01f0418\x00\x00" dataUsingEncoding:NSUTF8StringEncoding];
NSString *base64EncryptedString = @"0Nwr30kSkJxGCYiFg8TUrfW51587SUkS8lQ7Uno7gglxjVGW5gR4MA+isTknjXzK";
NSData *encryptedData = [[NSData alloc] initWithBase64EncodedString:base64EncryptedString options:0];

NSMutableData *plainData = [NSMutableData dataWithLength: encryptedData.length];
size_t movedBytes = 0;

CCCryptorStatus ccStatus;
ccStatus = CCCrypt(kCCDecrypt,
                   kCCAlgorithmAES128,
                   kCCOptionPKCS7Padding,
                   keyData.bytes, kCCKeySizeAES128,
                   ivData.bytes,
                   encryptedData.bytes, encryptedData.length,
                   plainData.mutableBytes, plainData.length,
                   &movedBytes);

plainData.length = movedBytes;
NSLog(@"Data: %@",plainData);
NSString *decryptedString = [[NSString alloc] initWithData:plainData encoding:NSUTF8StringEncoding];
NSLog(@"String: %@",decryptedString);