- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我正在为 ios 和 Android 开发一个应用程序。我对加密任务相对较新,在过去的 3 天里,我一直把头撞在墙上,因为我无法运行 RSA 加密。
两个客户端都从 Java 服务器接收公钥。在 android 中我(显然,因为它与服务器端的代码几乎相同)没有问题,但 ios 部分似乎根本不兼容。我想用公钥加密一小段数据(aes key ),这就是我在 Java 中这样做的方式:
try {
String publickey = "MCwwDQYJKoZIhvcNAQEBBQADGwAwGAIRAK+dBpbOKw+1VKMWoFxjU6UCAwEAAQ==";
byte[] bArr = Crypto.base64Decode(publicKey, false);
KeyFactory keyFactory = KeyFactory.getInstance("RSA", "BC");
EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicKey);
PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);
Cipher cipher = Cipher.getInstance("RSA/NONE/PKCS1Padding", "BC");
cipher.init(1,publicKey);
int cipherBlockSize = cipher.getBlockSize();
ByteArrayOutputStream bArrOut = new ByteArrayOutputStream();
bArrOut.flush();
int pos = 0;
Log.i("ContentBufferLength", contentBuffer.length+"");
while (true) {
if (cipherBlockSize > contentBuffer.length - pos) {
cipherBlockSize = contentBuffer.length - pos;
}
Log.i("CipherBlockSize", cipherBlockSize+"");
byte[] tmp = cipher.doFinal(contentBuffer, pos, cipherBlockSize);
bArrOut.write(tmp);
pos += cipherBlockSize;
if (contentBuffer.length <= pos) {
break;
}
}
bArrOut.flush();
encryptedBuffer = bArrOut.toByteArray();
bArrOut.close();
} catch (Exception ex) {
throw ex;
}
// Log.i("Encrypted Buffer Length", encryptedBuffer.length+"");
return encryptedBuffer;
这是我的(不能正常工作的)ios代码,从这里借来的:
http://blog.wingsofhermes.org/?p=75和苹果加密练习。
-(NSString* )encryptWithPublicKey:(NSString*)key input:(NSString*) input {
const size_t BUFFER_SIZE = 16;
const size_t CIPHER_BUFFER_SIZE = 16;
//const uint32_t PADDING = kSecPaddingNone;
const uint32_t PADDING = kSecPaddingPKCS1;
static const UInt8 publicKeyIdentifier[] = "de.irgendwas.app";
NSData *publicTag;
publicTag = [[NSData alloc] initWithBytes:publicKeyIdentifier length:sizeof(publicKeyIdentifier)];
NSMutableDictionary *publicKey2 = [[NSMutableDictionary alloc] init];
[publicKey2 setObject:kSecClassKey forKey:kSecClass];
[publicKey2 setObject:kSecAttrKeyTypeRSA forKey:kSecAttrKeyType];
[publicKey2 setObject:publicTag forKey:kSecAttrApplicationTag];
SecItemDelete((CFDictionaryRef)publicKey2);
NSData *strippedPublicKeyData = [NSData dataFromBase64String:key];
unsigned char * bytes = (unsigned char *)[strippedPublicKeyData bytes];
size_t bytesLen = [strippedPublicKeyData length];
size_t i = 0;
if (bytes[i++] != 0x30)
[Exception raise:FAILURE function:__PRETTY_FUNCTION__ line:__LINE__ description:@"Could not set public key."];
/* Skip size bytes */
if (bytes[i] > 0x80)
i += bytes[i] - 0x80 + 1;
else
i++;
if (i >= bytesLen)
[Exception raise:FAILURE function:__PRETTY_FUNCTION__ line:__LINE__ description:@"Could not set public key."];
if (bytes[i] != 0x30)
[Exception raise:FAILURE function:__PRETTY_FUNCTION__ line:__LINE__ description:@"Could not set public key."];
/* Skip OID */
i += 15;
if (i >= bytesLen - 2)
[Exception raise:FAILURE function:__PRETTY_FUNCTION__ line:__LINE__ description:@"Could not set public key."];
if (bytes[i++] != 0x03)
[Exception raise:FAILURE function:__PRETTY_FUNCTION__ line:__LINE__ description:@"Could not set public key."];
/* Skip length and null */
if (bytes[i] > 0x80)
i += bytes[i] - 0x80 + 1;
else
i++;
if (i >= bytesLen)
[Exception raise:FAILURE function:__PRETTY_FUNCTION__ line:__LINE__ description:@"Could not set public key."];
if (bytes[i++] != 0x00)
[Exception raise:FAILURE function:__PRETTY_FUNCTION__ line:__LINE__ description:@"Could not set public key."];
if (i >= bytesLen)
[Exception raise:FAILURE function:__PRETTY_FUNCTION__ line:__LINE__ description:@"Could not set public key."];
strippedPublicKeyData = [NSData dataWithBytes:&bytes[i] length:bytesLen - i];
DLog(@"X.509 Formatted Public Key bytes:\n%@",[strippedPublicKeyData description]);
if (strippedPublicKeyData == nil)
[Exception raise:FAILURE function:__PRETTY_FUNCTION__ line:__LINE__ description:@"Could not set public key."];
CFTypeRef persistKey = nil;
[publicKey2 setObject:strippedPublicKeyData forKey:kSecValueData];
[publicKey2 setObject: (kSecAttrKeyClassPublic) forKey:kSecAttrKeyClass];
[publicKey2 setObject:[NSNumber numberWithBool:YES] forKey:kSecReturnPersistentRef];
OSStatus secStatus = SecItemAdd((CFDictionaryRef)publicKey2, &persistKey);
if (persistKey != nil) CFRelease(persistKey);
if ((secStatus != noErr) && (secStatus != errSecDuplicateItem))
[Exception raise:FAILURE function:__PRETTY_FUNCTION__ line:__LINE__ description:@"Could not set public key."];
SecKeyRef keyRef = nil;
[publicKey2 removeObjectForKey:kSecValueData];
[publicKey2 removeObjectForKey:kSecReturnPersistentRef];
[publicKey2 setObject:[NSNumber numberWithBool:YES] forKey:kSecReturnRef];
[publicKey2 setObject: kSecAttrKeyTypeRSA forKey:kSecAttrKeyType];
SecItemCopyMatching((CFDictionaryRef)publicKey2,(CFTypeRef *)&keyRef);
if (!keyRef)
[Exception raise:FAILURE function:__PRETTY_FUNCTION__ line:__LINE__ description:@"Could not set public key."];
uint8_t *plainBuffer;
uint8_t *cipherBuffer;
uint8_t *decryptedBuffer;
const char inputString[] = "1234";
int len = strlen(inputString);
// TODO: this is a hack since i know inputString length will be less than BUFFER_SIZE
if (len > BUFFER_SIZE) len = BUFFER_SIZE-1;
plainBuffer = (uint8_t *)calloc(BUFFER_SIZE, sizeof(uint8_t));
cipherBuffer = (uint8_t *)calloc(CIPHER_BUFFER_SIZE, sizeof(uint8_t));
decryptedBuffer = (uint8_t *)calloc(BUFFER_SIZE, sizeof(uint8_t));
strncpy( (char *)plainBuffer, inputString, len);
size_t plainBufferSize = strlen((char *)plainBuffer);
size_t cipherBufferSize = CIPHER_BUFFER_SIZE;
NSLog(@"SecKeyGetBlockSize() public = %lu", SecKeyGetBlockSize(keyRef));
// Error handling
// Encrypt using the public.
OSStatus status = noErr;
status = SecKeyEncrypt(keyRef,
PADDING,
plainBuffer,
plainBufferSize,
&cipherBuffer[0],
&cipherBufferSize
);
NSLog(@"encryption result code: %ld (size: %lu)", status, cipherBufferSize);
return [[[NSString stringWithFormat:@"%s",cipherBuffer] dataUsingEncoding:NSUTF8StringEncoding] base64EncodedString];
}
出于测试目的和简单性,目前我尝试仅加密长度为 4 个字节的输入。这应该足够小以容纳一个 block 。公钥导入和加密过程似乎有效,但是与 android 方法相比,我总是收到更长的输出。
到目前为止,我遇到的唯一区别是 SecKeyGetBlockSize 返回 16
而在 java cipher.blocksize 返回 5。我认为其他 11 个字节是为 pkcs1 填充保留的,但是如何在 ios/objc
中强制执行相同的行为?
最佳答案
尝试将密文拆分为多个部分,使每个部分包含 16 个字符长,并分别对其进行解码。我也遇到了同样的问题,但这是在 PHP 中使用了很长时间,上面的技巧对我有用。
这可能会帮助您摆脱问题。
关于java - PKCS1-padding/RSA 加密 ios objc 和 java 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12526452/
我目前正在使用 Crypto++ 为数据块生成签名。我希望签名是 20 个字节(SHA 1 Hash),因为我的理解是签名只是一个加密的哈希。但是当检查 maxsignaturelength 和 si
我不是加密专家,所以我对这些事情的了解接近于零。我必须与使用 RSA 加密的系统进行互操作。使用他们的 key 时,我遇到了为相同的输入/ key 获取不同密码的问题。图书馆是https://code
我想利用 postman 来测试需要使用 RSA 加密对输入字段之一进行加密的 REST API。 我看到 postman 通过 require('crypto-js') 提供了功能使用 AES 加密
我正在尝试在我的 (Java) 应用程序中实现一个(简化的)类似 RSA 的验证过程。客户端发送一个请求(数据+私钥签名),服务器要么拒绝他的请求,要么处理它——取决于签名的有效性。 但是我不明白验证
我正在尝试在我的 (Java) 应用程序中实现一个(简化的)类似 RSA 的验证过程。客户端发送一个请求(数据+私钥签名),服务器要么拒绝他的请求,要么处理它——取决于签名的有效性。 但是我不明白验证
下面是我想要做的最小、完整且可验证的示例。 基本上我想实现一个集成一些 CUDA 代码的 OpenSSL RSA 引擎。 CUDA 部分应该执行模幂运算,但在本例中并不重要,因此我只是按顺序使用了 B
是否有任何非常简单的跨平台 C++ 库可以进行不对称加密?不需要高效,只要工作。我想它可能只是 .h 文件中的 3-4 个函数,它们可以执行任意精度的数学运算,仅此而已。 我相信在这里使用 OpenS
我使用以下命令创建了私钥和公钥, openssl genrsa -out privatekey.pem 1024 openssl req -new -x509 -key privatekey.pem
我在 .net 环境(所有版本)中工作并使用 vb.net。我想根据密码生成 RSA 公钥和私钥。 我对 RSA 算法的理解仅限于使用 .net 提供的类,即 System.Security.Cryp
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 7年前关闭。 Improve thi
我有 ssh-keygen 生成的 id_rsa.pub key 。 如何以编程方式将 id_rsa.pub 文件转换为 RSA DER 格式的 key ? 最佳答案 如果使用 ssh-keygen
我在 JWT(JSON Web Token)方案的帮助下实现了一个登录系统。基本上,在用户登录/登录后,服务器对 JWT 进行签名并将其传递给客户端。 然后客户端在每个请求中返回 token ,服务器
我使用的是 WAS 6.1,我的服务器过去可以正常启动,但突然间无法正常工作.. 当我尝试在我的本地 RSA 中启动我的服务器时。我收到以下错误 我已经重新启动了系统,RSA,杀死了所有 Java 进
我正在开发一个支付网关,他们有一个正在运行的 Java 演示,但我想用 php 来实现它。 支付网关使用 3DES 和随机生成的 key 来加密有效负载。该 key 使用支付网关的公钥通过 RSA 进
这是此处未回答问题的副本:Using an RSA Public Key to decrypt a string that was encrypted using RSA Private Key 您可
我一直无法将 RSA 加密字节编码为字符串,然后在另一端(远程主机)将它们恢复为字节,另一端抛出“错误数据”异常。 我试过谷歌,没有运气。 到目前为止我的代码: 客户: array^Test=Enco
我需要一些帮助来解决我的问题。 问题:我想用 Android 平台的公共(public) RSA key 加密一个数字 (A),然后用私钥在 PHP 服务器上解密它。在每个平台上,我都可以加密和解密数
当我尝试将参数传递给函数时出现以下错误。 error[E0243]: wrong number of type arguments: expected 1, found 0 --> src/mai
我尝试将公共(public) RSA key 加载到我的程序中。我在 C 中使用 openssl 库。 key 在 header crypt.h 文件中定义: #define PUBLIC_KEY
Bouncy CaSTLe 加密库中有两种不同的密码可以传递给 PKCS1Encoding:NativeRSAEngine 和 RSAEngine。这两个变体之间有区别吗? 编辑: 正如 Maarte
我是一名优秀的程序员,十分优秀!