登录
首页 >  Golang >  Go问答

在哪里可以找到与 Go 中的 EncryptRSAOAEP() 功能等效的 Java 功能?

来源:stackoverflow

时间:2024-03-22 12:12:41 226浏览 收藏

在 Java 中,可以使用 `javax.crypto.Cipher` 类进行带有 OAEP 填充的 RSA 加密。该类提供与 Go 中 `EncryptRSAOAEP()` 函数类似的功能。Java 代码示例如下: ```java import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.spec.OAEPParameterSpec; import javax.crypto.spec.MGF1ParameterSpec; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.Provider; import java.security.SecureRandom; import java.util.Base64; public class JavaEncryptRSAOAEP { public static void main(String[] args) throws Exception { // Generate a 2048-bit RSA key pair KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); keyPairGenerator.initialize(2048); KeyPair keyPair = keyPairGenerator.generateKeyPair(); // Create a cipher instance with OAEP padding Cipher cipher = Cipher.getInstance("RSA/None/OAEPWithSHA256AndMGF1Padding"); OAEPParameterSpec oaepParams = new OAEPParameterSpec("SHA-256", "MGF1", MGF1ParameterSpec.SHA256, null); cipher.init(Cipher.ENCRYPT_MODE, keyPair.getPublic(), oaepParams); // Encrypt a message String message = "Hello, world!"; byte[] ciphertext = cipher.doFinal(message.getBytes()); // Encode the ciphertext in Base64 String encodedCiphertext = Base64.getEncoder().encodeToString(ciphertext); // Print the encrypted message System.out.println("Encrypted message: " + encodedCiphertext); } } ```

问题内容

在哪里可以找到与 java 中 google go 的 encryptrsa-oaep() 等效的函数?

从上面的链接中,go 中给出了以下代码示例:

secretMessage := []byte("send reinforcements, we're going to advance")
label := []byte("orders")

// crypto/rand.Reader is a good source of entropy for randomizing the
// encryption function.
rng := rand.Reader

ciphertext, err := EncryptOAEP(sha256.New(), rng, &test2048Key.PublicKey, secretMessage, label)
if err != nil {
    fmt.Fprintf(os.Stderr, "Error from encryption: %s\n", err)
    return
}

// Since encryption is a randomized function, ciphertext will be
// different each time.
fmt.Printf("Ciphertext: %x\n", ciphertext)

我的问题:如何在 java 中执行上述操作?


解决方案


您可以使用 javax.crypto.cipher 进行带有 oaep 填充的 rsa 加密

cipher cipher = cipher.getinstance("rsa/none/oaepwithsha1andmgf1padding", "bc"); // creating cipher instance with rsa algorithm and oaep padding
// random key generation for rsa
securerandom random = new securerandom();
keypairgenerator generator = keypairgenerator.getinstance("rsa", "bc");

generator.initialize(386, random);

keypair pair = generator.generatekeypair();
key pubkey = pair.getpublic();

key privkey = pair.getprivate();
// initializing cipher with key and encrypt/decrypt mode
cipher.init(cipher.encrypt_mode, pubkey, random);
// encrypts the text
byte[] ciphertext = cipher.dofinal(input);

注意: 您需要为此添加 bouncycastle 依赖项。还将 bouncy castle 提供程序添加到安全性中。

2882​​23686673

以上就是《在哪里可以找到与 Go 中的 EncryptRSAOAEP() 功能等效的 Java 功能?》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>