I want to encode a string with rsa/ecb/pkcs1 padding mode with a given public key (the public key is a string) in java.
我想在Java中用rsa/ecb/pkcs1填充模式用给定的公钥(公钥是一个字符串)来编码一个字符串。
I also want to present the results in UTF-8 Format
how to do it?
我也想用UTF-8格式呈现成绩,怎么办呢?
更多回答
There are a lot of examples out there. What's the problem you're having? Don't forget to encode the ciphertext with base64 or hex if you want to have it in a string.
有很多例子。你有什么问题吗?如果您想将密文放在字符串中,不要忘记使用Base64或十六进制对其进行编码。
The results will be essentially random byte and not all such bytes are a valid UTF-8 encoding. If you need UTF-8 you need to use an encoding on the random output, Base64 and Hexadecimal are commonly used.
结果基本上是随机字节,并不是所有这样的字节都是有效的UTF-8编码。如果你需要UTF-8,你需要对随机输出使用编码,通常使用Base64和十六进制。
i have done this code:
我已经完成了以下代码:
String pub = "MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEA4IJZLsjlx+o4RSvafaAcReoNnzrI0UXu7kZyXPe31ql32X9AvhC6QQIUmLkr1Evm0zP/SgVG9YX3DSqBUgPo04iv1I1/wNKwAf1/uH9EiiqdpczefyxxnzJiKUTcx2/4mA4E4QxCIL5JsZb78WoYZrd2kToW/WD01MnSFiCgSyjGdd812GY2EVzfvlv8kYuti3icMUyitEfHhtw8cAWI6/nVrRPNs0e5NsvtZJ0nfrXsfQDR0C7+ivQK+fQabi8oRGsbTZceAvVlqVE669zoIwIFLcB+eYXTxbka4E7veUMpaF9w//HdwVS2y/2jJiI+16qPStQQPIKQ4Cucoif7/UHfIBuVGVJ5MIVyK7NC7TV/lyoXmyo7ZcnVZnI7rZcw5/qZcqaZ0VCrzvHijwTK7100hOOjiarvRa2OJGXHLIeAUlbrHOXEXS6ah2glPhLDEg6Qzp/lKVSISolal7q73qyhF483P9jXn3hefSLA9J1/1LgeajWvuVkxuw+dy2Tlv7oUpNBkX47/TOho5qttr1y9K3hD5Q87RAJPdBtFdDbY8qUPxoiBsTbUWjVoEjJf2YAsLTJIIi2ZISkbD/VdrtZnS73QSJkJReOMNT9XYNGDJvwNIrRcNGFKlJcX6qq+ozGNsDkrt0ObxAD7YCTjAYQVTlbQOaTu5DbGxGDNCoMCAwEAAQ==";
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
byte[] keyBytes = Base64.getDecoder().decode(pub.getBytes("UTF-8"));
PKCS1EncodedKeySpec KeySpec = new PKCS1EncodedKeySpec(keyBytes);
RSAPublicKey publicKey = (RSAPublicKey)keyFactory.generatePublic((java.security.spec.KeySpec) KeySpec);
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] cipherData = cipher.doFinal(text.getBytes("UTF-8"));
return cipherData;
But it doesnot work..
it is said that Invalid DER: object is not integer
但它不工作..这就是说,无效DER:对象不是整数
For someone still looking for a solution:
对于仍在寻找解决方案的人:
String pub = "MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEA4IJZLsjlx+o4RSvafaAcReoNnzrI0UXu7kZyXPe31ql32X9AvhC6QQIUmLkr1Evm0zP/SgVG9YX3DSqBUgPo04iv1I1/wNKwAf1/uH9EiiqdpczefyxxnzJiKUTcx2/4mA4E4QxCIL5JsZb78WoYZrd2kToW/WD01MnSFiCgSyjGdd812GY2EVzfvlv8kYuti3icMUyitEfHhtw8cAWI6/nVrRPNs0e5NsvtZJ0nfrXsfQDR0C7+ivQK+fQabi8oRGsbTZceAvVlqVE669zoIwIFLcB+eYXTxbka4E7veUMpaF9w//HdwVS2y/2jJiI+16qPStQQPIKQ4Cucoif7/UHfIBuVGVJ5MIVyK7NC7TV/lyoXmyo7ZcnVZnI7rZcw5/qZcqaZ0VCrzvHijwTK7100hOOjiarvRa2OJGXHLIeAUlbrHOXEXS6ah2glPhLDEg6Qzp/lKVSISolal7q73qyhF483P9jXn3hefSLA9J1/1LgeajWvuVkxuw+dy2Tlv7oUpNBkX47/TOho5qttr1y9K3hD5Q87RAJPdBtFdDbY8qUPxoiBsTbUWjVoEjJf2YAsLTJIIi2ZISkbD/VdrtZnS73QSJkJReOMNT9XYNGDJvwNIrRcNGFKlJcX6qq+ozGNsDkrt0ObxAD7YCTjAYQVTlbQOaTu5DbGxGDNCoMCAwEAAQ==";
Cipher cipher= Cipher.getInstance("RSA");
KeyFactory kf = KeyFactory.getInstance("RSA");
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(Base64.getDecoder().decode(publicKey));
PublicKey pb = kf.generatePublic(keySpec);
cipher.init(Cipher.ENCRYPT_MODE,pb);
byte[] output = cipher.doFinal(input.getBytes());
Assuming you're using a valid RSA key, you'll need to:
假设您使用的是有效的RSA密钥,您需要:
Convert your public key from a string to an actual public key object
//This code is incorrect. You'll need bouncy castle for PKCS1
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
byte[] keyBytes = Base64().getDecoder.decode(publicKey.getBytes()); //assuming base64 encoded key
PKCS1EncodedKeySpec KeySpec = new PKCS1EncodedKeySpec(keyBytes);
RSAPublicKey publicKey = (RSAPublicKey)keyFactory.generatePublic(KeySpec);
Get the bytes of your plain text
- Encrypt using your public key
- Encode to a readable format.
Check out this answer for steps 1-3: RSA Encrypt/Decrypt in Java. Remember to use the correct algorithm spec, in your case PKCS1
请查看步骤1-3:Java中的RSA加密/解密的答案。记住使用正确的算法规范,在您的例子中是PKCS1
Chances are your cipher text will not use only UTF-8 characters so you'll probably want to use Base 64 encoded text to display your cipher text. Base 64 is able to display all those wonky characters as ascii values.
您的密文可能不会只使用UTF-8字符,因此您可能希望使用Base64编码的文本来显示密文。BASE 64能够将所有这些不可靠的字符显示为ASCII值。
Simply use: Base64.getEncoder().encodeToString(cipherTextBytes)
只需使用:Base64.getEncoder().encodeToString(cipherTextBytes)
更多回答
Never use getBytes() without defining a charset like "UTF-8" to prevent any errors when the (system's) default charsets differ.
在没有定义类似“UTF-8”的字符集的情况下,切勿使用getBytes(),以防止在(系统)的默认字符集不同时出现任何错误。
My problem is that I don t want to generate a key. My public key is in this form : lyoXmyo7ZcnVZnI7rZcw5.... how can I use this public key in this form ?
我的问题是我不想生成密钥。我的公钥格式为:lyoXmyo7ZcnVZnI7rZcw5...如何在此表单中使用此公钥?
@Marzo You'll first have to figure out what find of format this public key is in. Ask the people you've got this from and if they don't know, ask for the code that generated it. You should be able to go from there.
@Marzo您首先必须找出此公钥的格式。问问你从哪里得到这条消息的人,如果他们不知道,问一下生成它的代码。你应该可以从那里出发。
I have the modulus and the exponent of the key but i cannot initialize my key from my java code
我有密钥的模数和指数,但我无法从Java代码初始化我的密钥
@venture I can t understand the line PKCS1EncodedKeySpec(res) what is res ?
@Venture我不能理解一行PKCS1EncodedKeySpec(Res)什么是res?
我是一名优秀的程序员,十分优秀!