使用自定义键BouncyCastle的RSA J2ME [英] BouncyCastle J2ME RSA using custom keys

查看:154
本文介绍了使用自定义键BouncyCastle的RSA J2ME的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用BouncyCastle的J2ME / RIM加密在我的黑莓应用。

I would like to use BouncyCastle J2ME/RIM Crypto in my Blackberry Application.

我遇到的问题是,我想生成发送的关键黑莓C#.NET程序加密的公钥。

The issue i'm having is that I would like to generate the public key for encryption from a C#.NET program that sends the key to the BlackBerry.

是否有可能使用原始字符串加密邮件?此外,我还需要了解其他常见的变量,如模等?道歉,但我完全新的加密算法。

Is it possible to encrypt a message using a raw string? Also, do I need to know other common variable such as modulo etc? Apologies but i'm completely new to cryptography algorithms.

我需要BouncyCastle的这个或可在以上RIM加密做什么?

Do I need BouncyCastle for this or can the above be done with RIM Crypto?

谢谢,
康纳尔

Thanks, Conor

推荐答案

这是RSA公钥由两部分组成,而不是一个像我想。

An RSA public key consists of two components, not just one like I thought.

有是指数。这些都是数字,但我将它们传递到黑莓从.NET客户端为Base64字符串和德code它们变成字节数组当由RIM加密功能,因为他们采取字节数组作为参数一起使用。

There is the Exponent and Modulus. These are both number but I pass them to the Blackberry from .NET client as Base64 strings and decode them into byte arrays when be used by the RIM Crypto function as they take Byte arrays as parameters.

byte[] exponent = Base64InputStream.decode("exponent base64 string");
byte[] modulus = Base64InputStream.decode("modulus base64 string");

NoCopyByteArrayOutputStream cipherUserData = new NoCopyByteArrayOutputStream();             
RSACryptoSystem cryptoSystem = new RSACryptoSystem(1024);

// Create Public key using your variables from before
RSAPublicKey publicKey = new RSAPublicKey( cryptoSystem, exponent, modulus);

// Encryption engine objects
RSAEncryptorEngine eEngine = new RSAEncryptorEngine(publicKey);
PKCS1FormatterEngine fEngine = new PKCS1FormatterEngine(eEngine);
BlockEncryptor cryptoStream = new BlockEncryptor(fEngine, cipherUserData);  


// Read the user data and encrypt while doing so. Remember, cryptoStream writes its data to
// cipherUserData so this is where the encrypted version of userData will end up.
cryptoStream.write( userData, 0, userData.length );

cryptoStream.close();
cipherUserData.close();

String encryptedUserData = new String(cipherUserData.toByteArray());

这是pretty很多都在那里过它的乡亲,这是简单,但我花了很长时间才从API文档得到这样的:)

That's pretty much all there is too it folks, it's straightforward but it took me a long time to get this from the API docs :)

重要提示
RSA是有限的加密目的,你只能加密消息< =密钥大小。
这是117个字节为1024位RSA和RSA 2048字节245。要加密较大的消息被接受的方法是加密使用AES或类似然后加密的RSA公钥AES密钥的消息。您将在发送密文AES,也包含密钥的RSA密文解密AES密文。

Important note RSA is limited for encryption purposes in that you can only encrypt a message that <= key size. That is 117 bytes for 1024 Bit RSA and 245 bytes for 2048 RSA. To encrypt larger messages the accepted way is to encrypt the message using AES or similar then encrypt the AES key with the RSA public key. You will the send the AES ciphertext and also the RSA ciphertext containing the key to decrypt the AES ciphertext.

我已经写了上面修修补补和阅读日。我希望它可以帮助别人获得比他们的目标更快。 :)

What I have written above took days of tinkering and reading. I hope it helps somebody achieve their goal faster than that. :)

这篇关于使用自定义键BouncyCastle的RSA J2ME的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆