gpt4 book ai didi

java - "RSA/ECB/OAEPWITHSHA256ANDMGF1PADDING"和 "RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING"有什么区别

转载 作者:行者123 更新时间:2023-11-29 05:11:45 24 4
gpt4 key购买 nike

据我所知,两者是相同的,但一个在一台 PC 上工作,而相同的代码说:

javax.crypto.NoSuchPaddingException:OAEPWITHSHA-256ANDMGF1PADDING 在另一台机器上无法使用 RSA

当我从名称 (OAEPWITHSHA256ANDMGF1PADDING) 中删除破折号 - 时,它开始在另一台机器上运行,但会导致其他一些行错误填充异常出错。可能是什么原因?

提示的示例代码

我正在使用 jdk1.7.0_71 32bit:

private byte[] decryptSecretKeyData(byte[] encryptedSecretKey, byte[] iv, PrivateKey privateKey) throws Exception 
{
try {

Provider provider= new sun.security.pkcs11.SunPKCS11(keyStoreFile1);
Security.addProvider(provider);

LOG.info("**************Inside decryptSecretKeyData***********************");
Cipher rsaCipher = Cipher.getInstance("RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING", provider);

// decrypting the session key with rsa no padding.
rsaCipher.init(Cipher.DECRYPT_MODE, privateKey);

/* The reason is RSA OAEP SHA256 is not supported in HSM. */
byte[] decKey = rsaCipher.doFinal(encryptedSecretKey);

OAEPEncoding encode = new OAEPEncoding(new RSAEngine(), new SHA256Digest(), iv);
LOG.info("******************RSAPublicKey rsaPublickey = (*****************************");

java.security.interfaces.RSAPublicKey rsaPublickey = (java.security.interfaces.RSAPublicKey) publicKeyFile;
RSAKeyParameters keyParams = new RSAKeyParameters(false, rsaPublickey.getModulus(), EXPONENT);
encode.init(false, keyParams);

LOG.info("******************encode.processBlock(decKey, 0, decKey.length);************************");
byte decryptedSecKey[] = encode.processBlock(decKey, 0, decKey.length);

return decryptedSecKey;
} catch (InvalidCipherTextException e) {
LOG.info("*******************Failed to decrypt AES secret key using RSA :**********************");
throw new Exception("Failed to decrypt AES secret key using RSA :" + e.toString());
}

}

最佳答案

RSA/ECB/OAEPWITHSHA256ANDMGF1PADDINGRSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING 是不同的别名,但两者都指的是相同的算法,所以从这个意义上说没有区别全部。

问题是您正在使用 PKCS#11(加密 token 接口(interface))来加密您的数据。根据 java PKCS#11 reference :

The Sun PKCS#11 provider, in contrast to most other providers, does not implement cryptographic algorithms itself. Instead, it acts as a bridge between the Java JCA and JCE APIs and the native PKCS#11 cryptographic API, translating the calls and conventions between the two. This means that Java applications calling standard JCA and JCE APIs can, without modification, take advantage of algorithms offered by the underlying PKCS#11 implementations, such as, for example,

Cryptographic Smartcards, Hardware cryptographic accelerators, and High performance software implementations. Note that Java SE only facilitates accessing native PKCS#11 implementations, it does not itself include a native PKCS#11 implementation. However, cryptographic devices such as Smartcards and hardware accelerators often come with software that includes a PKCS#11 implementation, which you need to install and configure according to manufacturer's instructions.

总而言之,如果您使用的是 PKCS#11,算法的使用取决于供应商的本地实现(Windows 上的 .dll.so 在 linux 上 ...) 有时在特定的程序连接器上所以:检查您是否在两台 PC 中为您的 PKCS#11 token 使用相同的驱动程序/程序版本和两者都已正确安装,因为其中一个可能存在错误,不允许您正确使用 RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING

希望对您有所帮助,

关于java - "RSA/ECB/OAEPWITHSHA256ANDMGF1PADDING"和 "RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING"有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28190858/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com