RSA/ECB/OAEPWithSHA-256AndMGF1Padding
SapiZonk opened this issue · 3 comments
please help me
i have string public key like this :
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxB6yO278INXCCq3ejro0o9zTfoAqerp9MIX9EbLmzQHu+386VaqEzJIg3cwO1WpIh+14VkG6Caxepqi7M+YCysuX7YlZnsSnt2zOEFdlS/LCjtoiDkuJt6Z+i5+Rdi4/Xf+UYoBCDvjlSzidjBHvPJnEmjf4+iNBnk+/jbKVhd+8kODiyoI+jux+w67zCnAOqNntohPc3IKikInP39t1UVRluoNa74bzoL8VRcPJCURXu57/qnV9/B2J9284iAgTzBK9S0UXU5TU1K3klg67GevTNgoIyDH4EbioLayGojOIbBs6ULyT/klKoAQ4jhdqSE3+WSDT55osbYRSoQVqjwIDAQAB
i want to encrypt some string with public key,
any example code to do RSA/ECB/OAEPWithSHA-256AndMGF1Padding in C#?
@SapiZonk I don't believe BC C# supports the ECB encryption mode in conjunction with RSA-OAEP; are you planning on specifying multiple blocks of data to encrypt with RSA? Is there a particular protocol you're looking to support? Most typically, RSA is combined with a symmetric cipher (such as AES-GCM) to encrypt data larger than a single block.
@cipherboy i just want to convert Java Code to C#
this is a Java code i want to convert:
public static final String encryptRSA(String str, String key) {
if (str == null || str.length() == 0) {
return null;
}
PublicKey generatePublicKey = generatePublicKey(key);
Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding");
cipher.init(1, generatePublicKey);
Charset UTF_8 = StandardCharsets.UTF_8;
byte[] bytes = str.getBytes(UTF_8);
return Base64.encodeToString(cipher.doFinal(bytes), 0);
}
public static final PublicKey generatePublicKey(String str) {
byte[] decode = Base64.decode(str, 0);
PublicKey generatePublic = KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decode));
return generatePublic;
}
(@cipherboy: edited for formatting)
@SapiZonk Aha, I see. I think you can just use the BC C# algorithm specifier RSA/None/OAEPWithSHA-256AndMGF1Padding
here :-)
See this note in the JDK:
/**
* RSA cipher implementation. Supports RSA en/decryption and signing/verifying
* using both PKCS#1 v1.5 and OAEP (v2.2) paddings and without padding (raw RSA).
* Note that raw RSA is supported mostly for completeness and should only be
* used in rare cases.
*
* Objects should be instantiated by calling Cipher.getInstance() using the
* following algorithm names:
* . "RSA/ECB/PKCS1Padding" (or "RSA") for PKCS#1 v1.5 padding.
* . "RSA/ECB/OAEPwith<hash>andMGF1Padding" (or "RSA/ECB/OAEPPadding") for
* PKCS#1 v2.2 padding.
* . "RSA/ECB/NoPadding" for rsa RSA.
*
* We only do one RSA operation per doFinal() call. If the application passes
* more data via calls to update() or doFinal(), we throw an
* IllegalBlockSizeException when doFinal() is called (see JCE API spec).
* Bulk encryption using RSA does not make sense and is not standardized.
*
* Note: RSA keys should be at least 512 bits long
*
* @since 1.5
* @author Andreas Sterbenz
*/
IOW, it is not really doing ECB mode and the ECB mode is a misnomer, and the above BC identifier should suffice. Let me know if you have problems!