简体   繁体   English

使用公钥进行RSA解密

[英]RSA decryption with a public key

I've got some decryption problems in my Android project. 我的Android项目中存在一些解密问题。

I'm getting a string signed with a private key and I have to verify(decrypt) it with a public key. 我得到了一个用私钥签名的字符串,我必须用一个公钥验证(解密)它。 I'd like to get exactly the same result as if I were using a PHP function - openssl_public_decrypt ( http://php.net/manual/pl/function.openssl-public-decrypt.php ) 我想获得与使用PHP函数完全相同的结果-openssl_public_decrypt( http://php.net/manual/pl/function.openssl-public-decrypt.php

I have to do this in my Java project, so I can use Java libs (eg BouncyCastle, or something else, any recommendations? ) 我必须在Java项目中执行此操作,因此我可以使用Java库(例如BouncyCastle或其他任何建议吗?)

Any ideas how to solve this? 任何想法如何解决这个问题?

Ok, here's my code. 好的,这是我的代码。 I'm getting the public key like this 我正在得到这样的公钥

PEMReader reader = new PEMReader(new InputStreamReader(ctx
                .getAssets().open("pubkey.pem")));
        Object obj;
        while ((obj = reader.readObject()) != null) {
             if (obj instanceof RSAPublicKey) {
                pubKey = (RSAPublicKey) obj;
                return pubKey;
            }
        }

And I always get the public key without any problems. 而且我总是可以毫无问题地获得公钥。

Cipher c = Cipher.getInstance("RSA/NONE/NoPadding", "SC");
c.init(Cipher.DECRYPT_MODE, pubKey);
byte[] result = c.doFinal(data_to_decrypt.getBytes());

And as a result(after converting bytes to string) I get 022c06571c6a263b389fcd93159cb311abb880bddf51b7c916dd1ae... 结果(将字节转换为字符串后)我得到022c06571c6a263b389fcd93159cb311abb880bddf51b7c916dd1ae...

where php functions returns sd8dsa348acvcx87|00454|OK|15000|CDE and this is a correct output. 其中php函数返回sd8dsa348acvcx87|00454|OK|15000|CDE ,这是正确的输出。

Java has got the Java Cryptography Extension Framework, which is just designed for these things. Java已经获得了Java密码学扩展框架,该框架只是为这些目的而设计的。

BouncyCastle is a Cryptography Provider for this framework. BouncyCastle是此框架的密码学提供程序。 This means, it provides your Java Cryptography Extension with implementations of cryptography algorithms. 这意味着,它为Java密码学扩展提供了加密算法的实现。

You'll find the basic classes for this in the packages java.security and javax.crypto 您可以在包java.securityjavax.crypto找到用于此的基本类

To decrypt your message with a public key you could try the following: 要使用公共密钥解密消息,您可以尝试以下操作:

// Use RSA/NONE/NoPadding as algorithm and BouncyCastle as crypto provider
Cipher asymmetricCipher = Cipher.getInstance("RSA/NONE/NoPadding", "BC");

// asume, that publicKeyBytes contains a byte array representing
// your public key
X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicKeyBytes);

KeyFactory keyFactory;
keyFactory = KeyFactory.getInstance(publicKeySpec.getFormat());
Key key = keyFactory.generatePublic(publicKeySpec);

// initialize your cipher
asymmetricCipher.init(Cipher.DECRYPT_MODE, key);
// asuming, cipherText is a byte array containing your encrypted message
byte[] plainText = asymmetricCipher.doFinal(cipherText);

Please note, that this example is very basic and lacks several try catch blocks. 请注意,该示例非常基础,并且缺少几个try catch块。 Also, you should not use an asymmetric cipher without padding as this makes you vulnerable to replay attacks. 另外,不应该使用没有填充的非对称密码,因为这会使您容易受到重放攻击。 You may also encounter issues with the key length. 您可能还会遇到密钥长度问题。 In some Java packages, the maximum allowed key length is restricted. 在某些Java软件包中,允许的最大密钥长度受到限制。 This may be solved by using the unlimited strength policy files. 这可以通过使用无限强度策略文件来解决。

I hope, this helps you in getting started with the Java cryptography. 希望这可以帮助您开始使用Java密码学。

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

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