简体   繁体   English

RSA将iOS转移到Java

[英]RSA transfer iOS to Java

I want to encrypt a String in Objective-C (iOS) and decrypt it after HTTP Transfer in Java. 我想在Objective-C(iOS)中加密一个字符串,并在Java中进行HTTP传输后解密它。

The Public Key was successfully transmitted from Java to iOS (at least it is recognied in the Keychain) 公钥已成功从Java传输到iOS(至少在钥匙串中可以识别)

In ObjC I try to send the text "Hallo" using this: 在ObjC中,我尝试使用以下命令发送文本“ Hallo”:

NSData* myData = [@"Hallo" dataUsingEncoding:NSUTF8StringEncoding];
OSStatus status = noErr;    
size_t cipherBufferSize;
uint8_t *cipherBuffer;
cipherBufferSize = SecKeyGetBlockSize(publicKey);
cipherBuffer = malloc(cipherBufferSize);    
size_t dataLength = [self length];
uint8_t* intDataToEncrypt = (uint8_t*)[self bytes];
if (cipherBufferSize < dataLength) {
    printf("Could not encrypt.  Packet too large.\n");
    return NULL;
}
status = SecKeyEncrypt(    publicKey,
                       kSecPaddingNone,
                       intDataToEncrypt,
                       dataLength,
                       cipherBuffer,
                       &cipherBufferSize
                       );
NSData *encryptedData = [NSData dataWithBytes:cipherBuffer length:cipherBufferSize];

"encryptedData" is then send base64 encoded via HTTP Post to a Java Servlet which uses this (BouncyCastle is used and the data converted back from Base64 first, the key is read from a file): 然后通过HTTP Post将“ encryptedData”发送的base64编码发送到使用它的Java Servlet(使用BouncyCastle,首先从Base64转换回数据,然后从文件中读取密钥):

PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(b64.decodeBuffer(key));
PrivateKey privateKey = kf.generatePrivate(keySpec);
Cipher cipher = Cipher.getInstance("RSA/NONE/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] myData = cipher.doFinal(byteData, 0, 256);

The output is completely messed up when trying to use as new String(myData, "UTF-8"); 尝试用作新的String(myData,“ UTF-8”);时,输出将完全混乱。 I tried sending only the "Hallo" without encryption this works, so it seems base64 is ok. 我尝试仅发送“ Hallo”而不进行加密,这有效,因此看来base64可以。 When I encrypt the same phrase in Java directly the output differs in 2 Bytes from the ObjC encryption. 当我直接用Java加密同一个短语时,输出与ObjC加密相差2字节。

In the End I want to use OAEP Padding instead of none... 最后,我想使用OAEP填充而不是没有...

I hope someone can help with this, the iOS Security Framework seems really to be very bad 我希望有人可以提供帮助,iOS安全框架似乎真的很糟糕

The Problem was not in the RSA part but the Base64 encoding and transfer. 问题不在于RSA部分,而在于Base64编码和传输。 All "+" in the String were received as " " due to HTTP(S) transfer (of course, I should have thought of that). 由于HTTP(S)传输,字符串中的所有“ +”都被接收为“”(当然,我应该想到这一点)。

Replacing them with "-" as proposed by the unofficial Base64URL version solves the problem. 非官方的Base64URL版本建议使用“-”代替它们来解决该问题。 (also all "/" with "_") (也所有带有“ _”的“ /”)

This also explain why only 2 bytes were different - those were the "+" symbol bytes. 这也解释了为什么只有2个字节不同-那些都是“ +”符号字节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM