Android上的RSA解密 - iPhone

时间:2012-12-08 11:11:57

标签: android iphone objective-c encryption rsa

我正在尝试实现一个系统,其中A生成RSA密钥对并将公钥发送给B.然后生成AES密钥并使用公钥对其进行加密,然后将结果发送回A.然后解密AES密钥使用其RSA私钥,使用AES密钥加密数据并将其发送给B,然后B可以使用AES密钥对其进行解密。

我已经将这一切都在Android方面工作了,但是我无法让iPhone端玩球(我是Objective C的新手,所以这可能就是原因!)

最初,我在使用RSA私钥解密AES密钥时收到错误9809,这无效转换为一般错误。研究错误指向填充(我正在使用PKCS1填充)是问题,切换到无填充允许iPhone客户端成功解密,但解密的AES密钥与Android客户端上生成的密钥不同。

目标C对我来说是一个新手,我确信我只是犯了一个小学生错误,有人能指出我正确的方向吗?

iPhone RSA密钥对生成

static const unsigned char _encodedRSAEncryptionOID[15] = {

    /* Sequence of length 0xd made up of OID followed by NULL */
    0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
    0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00

};

NSData * publicTag = [publicKeyIdentifier dataUsingEncoding:NSUTF8StringEncoding];

// Now lets extract the public key - build query to get bits
NSMutableDictionary * queryPublicKey = [[NSMutableDictionary alloc] init];

[queryPublicKey setObject:(__bridge id) kSecClassKey
                   forKey:(__bridge id) kSecClass];
[queryPublicKey setObject:publicTag
                   forKey:(__bridge id) kSecAttrApplicationTag];
[queryPublicKey setObject:(__bridge id) kSecAttrKeyTypeRSA
                   forKey:(__bridge id) kSecAttrKeyType];
[queryPublicKey setObject:[NSNumber numberWithBool:YES]
                   forKey:(__bridge id) kSecReturnData];

CFTypeRef pk;
OSStatus err = SecItemCopyMatching((__bridge CFDictionaryRef)queryPublicKey, &pk);

NSData* publicKeyBits = (__bridge_transfer NSData*)pk;

if (err != noErr) {
    return nil;
}

// OK - that gives us the "BITSTRING component of a full DER
// encoded RSA public key - we now need to build the rest

unsigned char builder[15];
NSMutableData * encKey = [[NSMutableData alloc] init];
int bitstringEncLength;

// When we get to the bitstring - how will we encode it?
if  ([publicKeyBits length ] + 1  < 128 )
    bitstringEncLength = 1 ;
else
    bitstringEncLength = (([publicKeyBits length ] +1 ) / 256 ) + 2 ;

// Overall we have a sequence of a certain length
builder[0] = 0x30;    // ASN.1 encoding representing a SEQUENCE
// Build up overall size made up of -
// size of OID + size of bitstring encoding + size of actual key
size_t i = sizeof(_encodedRSAEncryptionOID) + 2 + bitstringEncLength +
[publicKeyBits length];
size_t j = encodeLength(&builder[1], i);
[encKey appendBytes:builder length:j +1];

// First part of the sequence is the OID
[encKey appendBytes:_encodedRSAEncryptionOID
             length:sizeof(_encodedRSAEncryptionOID)];

// Now add the bitstring
builder[0] = 0x03;
j = encodeLength(&builder[1], [publicKeyBits length] + 1);
builder[j+1] = 0x00;
[encKey appendBytes:builder length:j + 2];

// Now the actual key
[encKey appendData:publicKeyBits];

// Now translate the result to a Base64 string
Base64* base64 = [[Base64 alloc] init];
NSString* ret = [base64 encode:encKey];

return ret;

重新创建公钥,生成AES密钥并在Android上加密 (注意getBytes(...)getString(...)只做一些base64 encoding.decoding)

KeyGenerator keyGen  = KeyGenerator.getInstance("AES");
keyGen.init(256, new SecureRandom());
SecretKey secretKey = keyGen.generateKey();

byte[] publicKeyBytes = getBytes(publicKey.getKey());
PublicKey rsaKey = KeyFactory.getInstance("RSA")
    .generatePublic(new X509EncodedKeySpec(publicKeyBytes));

Cipher cipher = Cipher.getInstance(RSA);
cipher.init(Cipher.ENCRYPT_MODE, rsaKey);

String keyEncoded = getString(key);

return getString(encryptedKeyBytes));

在iPhone上解密AES密钥

Base64* base64 = [[Base64 alloc] init];
NSData* cipherText = [base64 decode:textBase64];

const uint8_t *cipherBuffer = (const uint8_t*)[cipherText bytes];

size_t cipherBufferSize = strlen((char *) cipherBuffer);

uint8_t *plainBuffer = (uint8_t *)calloc(SecKeyGetBlockSize(publicKey), sizeof(uint8_t));
size_t plainBufferSize = SecKeyGetBlockSize(publicKey);

OSStatus status = SecKeyDecrypt(privateKey,
                       kSecPaddingPKCS1,
                       &cipherBuffer[0],
                       cipherBufferSize,
                       &plainBuffer[0],
                       &plainBufferSize
                       );

NSData* finalData = [[NSData alloc] initWithBytes:plainBuffer length:plainBufferSize];
NSString *result = [base64 encode:finalData];

return result;
编辑:我想我已经缩小了一点,下面的代码来自解密我的代码的AES关键部分:

NSData* cipherText = [base64 decode:text];
NSLog(@"cipherText %@", cipherText);
const uint8_t *cipherBuffer = (const uint8_t*)[cipherText bytes];
NSLog(@"cipherBuffer %s", cipherBuffer);

size_t cipherBufferSize = strlen((char *) cipherBuffer);
NSLog(@"cipherBufferSize %zd", cipherBufferSize);

在控制台中生成以下输出:

cipherText <31226275 cc56069a e96b7f6f 0fbee853 32d07de6 436755c9 e27b88a6 04176947     d57f7108 de68e5b8 49595e9f 09bceb30 1d615927 c205f205 eb644fa7 bff6c02b 885605de eb5bd4ee 473bb4d3 df768017 24552706 ea67f347 2952614e ad63f3c6 eb0022d3 a0513afa 0e59ba63 cb5c9787 a40ecad4 a866fdc7 26b60cc2 088a3499 a84c0595 fb1c2be8 5c85b88d 7856b4bd 655f6fec 905ca221 d6bb03c0 7329410b b235ef8f 1ef97a64 7fabb280 90118ff7 4b1e91f6 162134fc 5cbf962e 813e39e7 993b0fb9 e3c4b30c ef6a7b90 9d64c41a 1211ab34 c2c52235 d2ec3b65 d1314cee 70eafe65 f4a6c5e4 660cf889 4540a784 d14cc5a8 49a12c43 c76f7f03 5fbcd44f>
cipherBuffer 1"buÃVöÈkoæËS2–}ÊCgU…‚{à¶iG’qfihÂ∏IY^ü  ºÎ0aY'¬ÚÎdOßøˆ¿+àVfiÎ[‘ÓG;¥”flvÄ$U'ÍgÛG)RaN≠cÛ∆Î
cipherBufferSize 97

然而,偶尔会出现预期的密码缓冲区大小为256,并且解密工作完美!我知道我一定错过了一些明显的东西吗?

1 个答案:

答案 0 :(得分:0)

您的问题在于strlen函数,该函数通常不适用于二进制数据,它仅适用于表示文本的二进制数据,并以零值字节(\0)结束。相反,你应该使用密文的实际大小。

因此,如果密文包含零值字节,或者如果密码没有直接跟随零值字节,那么您的代码块将会失败。