- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 C# 中,我通过执行以下操作来加密文本数据(请注意我正在以 block ( block )的形式加密数据):
public string EncryptData(string publicKey, string data)
{
try
{
var bytesToEncrypt = Encoding.UTF8.GetBytes(data);
int srclen = bytesToEncrypt.Length;
//Prepare encryption engine
var encryptEngine = new Pkcs1Encoding(new RsaEngine());
//Initialize Key
using (var txtreader = new StringReader(publicKey))
{
var keyParameter = (AsymmetricKeyParameter)new PemReader(txtreader).ReadObject();
encryptEngine.Init(true, keyParameter);
}
//Encrypt in loop
byte[] complete = new byte[0];
int src_block_size = encryptEngine.GetInputBlockSize();
for (int idx = 0; idx < srclen; idx += src_block_size)
{
int data_len = srclen - idx;
if (data_len > src_block_size)
{
data_len = src_block_size;
}
var encryptedChunk = encryptEngine.ProcessBlock(bytesToEncrypt, idx, data_len);
complete = CombineByteArrays(complete, encryptedChunk);
}
var finalString = Convert.ToBase64String(complete);
return finalString;
}
catch (InvalidCipherTextException)
{
//catch exception
}
}
现在,我需要为某些 java 人员(我一点也不熟悉 java)提供相同的加密逻辑。现在他们是这样做的:
public static byte[] encrypt(byte[] text, PublicKey key) throws Exception
{
byte[] cipherText = null;
//
// get an RSA cipher object and print the provider
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
// encrypt the plain text using the public key
cipher.init(Cipher.ENCRYPT_MODE, key);
cipherText = cipher.doFinal(text);
return cipherText;
}
因此,有时解密(发生在基于 .Net 的服务器上)会失败并出现错误“输入对于 RSA 密码而言太大”。所以我怀疑这可能是因为加密和解密数据的逻辑不同(加密发生在基于 java 的客户端上,逻辑在上面,解密发生在基于 .Net 的客户端上,逻辑如下):
public string DecryptData(string privateKey, string base64Data)
{
try
{
var bytesToDecrypt = Convert.FromBase64String(base64Data);
AsymmetricCipherKeyPair keyPair;
//var internalEngine = new RsaEngine();
var decryptEngine = new RsaEngine(); //No paddind. We'll hunt the data inside packets
using (var txtreader = new StringReader(privateKey))
{
keyPair = (AsymmetricCipherKeyPair)new PemReader(txtreader).ReadObject();
decryptEngine.Init(false, keyPair.Private);
}
//Loop por todo el bloque y saca data
byte[] complete = new byte[0];
int blockSize = decryptEngine.GetInputBlockSize();
for (int chunkPosition = 0; chunkPosition < bytesToDecrypt.Length; chunkPosition += blockSize)
{
//int chunkSize = Math.Min(blockSize, bytesToDecrypt.Length - ((chunkPosition / blockSize) * blockSize));
int chunkSize = bytesToDecrypt.Length - chunkPosition;
if (chunkSize > blockSize)
{
chunkSize = blockSize;
}
var decryptedChunk = decryptEngine.ProcessBlock(bytesToDecrypt, chunkPosition, chunkSize);
//the actual decrypted data is in the middle, locate it!
int idxFirstZero = -1;
int outlen = decryptedChunk.Length;
int idxNextZero = (int)outlen;
for (int i = 0; i < outlen; i++)
{
if (decryptedChunk[i] == 0)
{
if (idxFirstZero < 0)
{
idxFirstZero = i;
}
else
{
idxNextZero = i;
break;
}
}
}
var totalSizeToCopy = idxNextZero - idxFirstZero - 1;
Array.Resize(ref complete, complete.Length + totalSizeToCopy);
int dstOffset = complete.Length - totalSizeToCopy > 0 ? complete.Length - totalSizeToCopy : 0;
Buffer.BlockCopy(decryptedChunk, idxFirstZero + 1, complete, dstOffset, totalSizeToCopy);
}
var finalString = Encoding.UTF8.GetString(complete).Trim('\0');
return finalString;
}
catch (InvalidCipherTextException)
{
}
}
如您所见,我正在以 block 的形式解密数据。所以我的问题是我们如何在 java 中(使用充气城堡)逐 block 进行加密(就像我在博文的第一个代码片段中在 .Net 中所做的那样)?
最佳答案
这不是一个常见的操作,因为对于大数据(甚至更小的数据)通常使用混合加密,其中使用对称密码的操作模式(例如 CBC)来加密更大的数据对象。
据我所知,没有直接的方法可以从 Cipher
实例请求 RSA/PKCS#1 的最大输入大小。
但是,这不是什么大问题,因为您可以根据 RSA key 大小自行计算。并且此 key 大小与模数的大小(以字节为单位)相同:
(keysize - 1)/Byte.SIZE + 1
或者,如果 keysize 是 8 的倍数(像往常一样),只需 keysize/Byte .SIZE
当然是;要获取 key 大小,只需将您的 key 转换为 RSAPublicKey
,然后调用 getModulus()
,然后调用 bitLength()
结果 BigInteger
。
请注意,发送没有完整性保护(例如签名)的密文被认为不是很安全。 PKCS#1 也可能成为填充 oracle 攻击的受害者,尤其是在传输模式安全性中使用时。 RSA/OAEP 将是更好的选择,用于混合加密(如 RSA/OAEP will store even less )。
关于java - 在 java 中使用 Bouncy CaSTLe 进行 block 式 RSA 加密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43798966/
我目前正在使用 Crypto++ 为数据块生成签名。我希望签名是 20 个字节(SHA 1 Hash),因为我的理解是签名只是一个加密的哈希。但是当检查 maxsignaturelength 和 si
我不是加密专家,所以我对这些事情的了解接近于零。我必须与使用 RSA 加密的系统进行互操作。使用他们的 key 时,我遇到了为相同的输入/ key 获取不同密码的问题。图书馆是https://code
我想利用 postman 来测试需要使用 RSA 加密对输入字段之一进行加密的 REST API。 我看到 postman 通过 require('crypto-js') 提供了功能使用 AES 加密
我正在尝试在我的 (Java) 应用程序中实现一个(简化的)类似 RSA 的验证过程。客户端发送一个请求(数据+私钥签名),服务器要么拒绝他的请求,要么处理它——取决于签名的有效性。 但是我不明白验证
我正在尝试在我的 (Java) 应用程序中实现一个(简化的)类似 RSA 的验证过程。客户端发送一个请求(数据+私钥签名),服务器要么拒绝他的请求,要么处理它——取决于签名的有效性。 但是我不明白验证
下面是我想要做的最小、完整且可验证的示例。 基本上我想实现一个集成一些 CUDA 代码的 OpenSSL RSA 引擎。 CUDA 部分应该执行模幂运算,但在本例中并不重要,因此我只是按顺序使用了 B
是否有任何非常简单的跨平台 C++ 库可以进行不对称加密?不需要高效,只要工作。我想它可能只是 .h 文件中的 3-4 个函数,它们可以执行任意精度的数学运算,仅此而已。 我相信在这里使用 OpenS
我使用以下命令创建了私钥和公钥, openssl genrsa -out privatekey.pem 1024 openssl req -new -x509 -key privatekey.pem
我在 .net 环境(所有版本)中工作并使用 vb.net。我想根据密码生成 RSA 公钥和私钥。 我对 RSA 算法的理解仅限于使用 .net 提供的类,即 System.Security.Cryp
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 7年前关闭。 Improve thi
我有 ssh-keygen 生成的 id_rsa.pub key 。 如何以编程方式将 id_rsa.pub 文件转换为 RSA DER 格式的 key ? 最佳答案 如果使用 ssh-keygen
我在 JWT(JSON Web Token)方案的帮助下实现了一个登录系统。基本上,在用户登录/登录后,服务器对 JWT 进行签名并将其传递给客户端。 然后客户端在每个请求中返回 token ,服务器
我使用的是 WAS 6.1,我的服务器过去可以正常启动,但突然间无法正常工作.. 当我尝试在我的本地 RSA 中启动我的服务器时。我收到以下错误 我已经重新启动了系统,RSA,杀死了所有 Java 进
我正在开发一个支付网关,他们有一个正在运行的 Java 演示,但我想用 php 来实现它。 支付网关使用 3DES 和随机生成的 key 来加密有效负载。该 key 使用支付网关的公钥通过 RSA 进
这是此处未回答问题的副本:Using an RSA Public Key to decrypt a string that was encrypted using RSA Private Key 您可
我一直无法将 RSA 加密字节编码为字符串,然后在另一端(远程主机)将它们恢复为字节,另一端抛出“错误数据”异常。 我试过谷歌,没有运气。 到目前为止我的代码: 客户: array^Test=Enco
我需要一些帮助来解决我的问题。 问题:我想用 Android 平台的公共(public) RSA key 加密一个数字 (A),然后用私钥在 PHP 服务器上解密它。在每个平台上,我都可以加密和解密数
当我尝试将参数传递给函数时出现以下错误。 error[E0243]: wrong number of type arguments: expected 1, found 0 --> src/mai
我尝试将公共(public) RSA key 加载到我的程序中。我在 C 中使用 openssl 库。 key 在 header crypt.h 文件中定义: #define PUBLIC_KEY
Bouncy CaSTLe 加密库中有两种不同的密码可以传递给 PKCS1Encoding:NativeRSAEngine 和 RSAEngine。这两个变体之间有区别吗? 编辑: 正如 Maarte
我是一名优秀的程序员,十分优秀!