在哪里可以找到与 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 提供程序添加到安全性中。
288223686673以上就是《在哪里可以找到与 Go 中的 EncryptRSAOAEP() 功能等效的 Java 功能?》的详细内容,更多关于的资料请关注golang学习网公众号!
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习