我正在使用c#进行AES Decryption
,因为解密后的数据在解密后的最后几个字符中显示了一些垃圾数据。
我使用下面的代码进行解密
+ (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)));
}
从C#收到数据: 0Nwr30kSkJxGCYiFg8TUrfW51587SUkS8lQ7Uno7gglxjVGW5gR4MA + isTknjXzK
Key uses for encryption : e64f9fa01f0418
来自iOS的解密数据:{“Successeed”: “abcdefghijklmnopqrstuvwx!...``〜
预期的解密数据:{“Successeed”: “ABCDEFGHIJKLMNOPQRSTUVWXYZ”}
答案 0 :(得分:1)
加密数据包含十六进制227d
。
数据十六进制表示:
7b22537563636565646564223a226162636465666768696a6b6c6d6e6f707172737475767778797a 227d
字符串表示:
{"Succeeded":"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);