Java 密码中带有AES和CBC的ECIES++;
Java 密码中带有AES和CBC的ECIES++;,java,ios,bouncycastle,crypto++,ecies,Java,Ios,Bouncycastle,Crypto++,Ecies,我需要在Crypto++中实现与BouncyCastle的“ECIESwithAES CBC/NONE/PKCS7Padding”等效的功能 主要原因是我需要在iOS上加密数据,并在后端使用Java中的BouncyCastle对其进行解密,我们希望使用这些特定的算法/配置。 我对C++没有任何经验,但这里是我在密码+++:中所得到的。 // loaded private key const unsigned char* privateKey; size_t keyLength; AutoS
我需要在Crypto++中实现与BouncyCastle的“ECIESwithAES CBC/NONE/PKCS7Padding”等效的功能
主要原因是我需要在iOS上加密数据,并在后端使用Java中的BouncyCastle对其进行解密,我们希望使用这些特定的算法/配置。 我对C++没有任何经验,但这里是我在密码+++:中所得到的。
// loaded private key
const unsigned char* privateKey;
size_t keyLength;
AutoSeededRandomPool prng;
ECIES_BC<ECP>::Decryptor decryptor;
decryptor.AccessPrivateKey().Load(StringStore(privateKey, keyLength).Ref());
ECIES_BC<ECP>::Encryptor encryptor(decryptor);
std::string plain("a"); // the message
std::string cipher;
SecByteBlock key(AES::DEFAULT_KEYLENGTH);
prng.GenerateBlock( key, key.size() );
byte iv[ AES::BLOCKSIZE ];
prng.GenerateBlock( iv, sizeof(iv) );
CBC_Mode< AES >::Encryption e;
e.SetKeyWithIV( key, key.size(), iv );
StringSource ss1( plain, true,
new StreamTransformationFilter( e,
new StringSink( cipher ), StreamTransformationFilter::PKCS_PADDING
) // StreamTransformationFilter
); // StringSource
std::string cryptogram;
StringSource ss2 (cipher, true,
new PK_EncryptorFilter(prng, encryptor, new StringSink(cryptogram) ) );
// ... decrypt cryptogram in bouncy castle
目前,当我从Crypto++获取输出并尝试在BouncyCastle中对其进行解密时,它会抛出一个异常:
javax.crypto.BadPaddingException: pad block corrupted
at org.bouncycastle.jcajce.provider.asymmetric.ec.IESCipher.engineDoFinal(Unknown Source)
at javax.crypto.Cipher.doFinal(Cipher.java:2087)
...
我不确定这是否真的是一个填充问题,还是我做了一些完全错误的事情
如有任何建议和帮助,我们将不胜感激。谢谢大家!
PS:我已经应用了ECIES中提到的bouncy castle补丁,根据使用的标准,ECIES有几种不同的加密方法。目前,Crypto++只实现了P1363 XOR方法(以下来自)。这可能解释了大部分例外情况 为了解决问题,我相信你有三个选择。首先,您可以使用XOR方法,因为Bouncy Castle和Crypto++都有它 第二,你可以使用。Botan和Crypto++都试图与Bouncy Castle保持一致,以促进互操作,但Botan还有一些加密方法 第三,Crypto++需要添加另一种与Bouncy Castle兼容的加密方法。我想它应该被称为
DL\u EncryptionAlgorithm\u AES\u CBC
。我不确定在AES-CBC/None/PKCS7Padding中,指的是什么
Crypto++很乐意添加DL\u EncryptionAlgorithm\u AES\u CBC
。另外,我需要一个有Java/BC经验的同事。如果你有兴趣,请联系我一个卸载者,gmail帐户
关于“Botan和Crypto++试图与Bouncy Castle结盟以促进互操作”。。。对于用户来说,事情是一团糟。Martínez、Encinas和Ávila注意到:
。。。不可能实现与兼容的软件版本
所有这些标准,包括具体操作和
允许的函数和算法列表
我可以指出无数互操作问题的例子,从您的问题到比特币和Zcash标准化协议的问题,因为有太多不兼容的选择。它一直在继续
这些评论来自以下网站,并可在以下网站获得:
/\类DL_EncryptionAlgorithm_Xor
//! \基于P1363的XOR加密方法简介
//! \用于MAC计算的tparam MAC MessageAuthenticationCode派生类
//! \t显示DHAES模式的RAM DHAES_模式标志
//! \t RAM LABEL_八位字节标志,指示标签为八位字节计数
//! \详细信息DL_EncryptionAlgorithm_Xor基于早期P1363草案,其本身似乎基于
//! 早期Certicom SEC-1草案(或早期SEC-1草案基于P1363草案)。Crypto++4.2在其集成应用程序中使用了它
//! 具有NOCOFCORMATION乘法的加密方案,DHAES_MODE=false,LABEL_OCTETS=true。
//! \详细信息如果您需要此方法以实现Crypto++4.2兼容性,请将ECIES模板类与
//! NOCOFCORMATION,DHAES_MODE=false,LABEL_OCTETS=true。
//! \详细信息如果您需要此方法以实现Bouncy Castle 1.54和Botan 1.11的兼容性,请使用ECIES模板类
//! NOCOFCORMATION,DHAES_MODE=true,LABEL_OCTETS=false。
//! \详细信息Bouncy Castle 1.54和Botan 1.11兼容性是默认模板参数。
//! \自从Crypto++4.0以来
模板
类DL_EncryptionAlgorithm_Xor:公共DL_SymmetricEncryptionAlgorithm
{
公众:
bool ParameterSupported(const char*name)const{return strcmp(name,name::EncodingParameters())==0;}
大小\u t GetSymmetricketyleLength(大小\u t PlaintTextLength)常量
{返回明文长度+静态_转换(MAC::DIGESTSIZE);}
大小\u t GetSymmetricipherTextLength(大小\u t plaintextLength)常量
{返回明文长度+静态_转换(MAC::DIGESTSIZE);}
大小\u t GetMaxSymmetricLaintExtLength(大小\u t CipherExtLength)常量
{return SaturatingSubtract(ciphertextLength,static_cast(MAC::DIGESTSIZE));}
void SymmetricEncrypt(RandomNumberGenerator&rng、常量字节*密钥、常量字节*明文、大小\u t明文长度、字节*密文、常量名称值对和参数)常量
{
CRYPTOPP_未使用(rng);
常量字节*cipherKey=NULL,*macKey=NULL;
if(DHAES_模式)
{
麦基=钥匙;
cipherKey=key+MAC::默认密钥长度;
}
其他的
{
密码密钥=密钥;
macKey=键+明文长度;
}
ConstByteArrayParameter编码参数;
GetValue(名称::EncodingParameters(),EncodingParameters);
if(plaintextLength)//覆盖率查找
xorbuf(密文、明文、密文密钥、明文长度);
麦基(macKey),;
mac.Update(密文,明文长度);
Update(encodingParameters.begin(),encodingParameters.size());
if(DHAES_模式)
{
字节L[8];
PutWord(false,大端序,L,(LABEL_OCTETS?word64(encodingParameters.size()):8*word64(encodingParameters.size());
mac.Update(L,8);
}
mac.Final(密文+明文长度);
}
DecodingResult SymmetricDecrypt(常量字节*密钥、常量字节*密文、大小\u t密文长度、字节*明文、常量名称值对和参数)常量
{
大小\u t plaintextLength=GetMaxSymmetricLaintTextLength(ciphertextLength);
常量字节*cipherKey,*macKey;
if(DHAES_模式)
{
麦基=钥匙;
cipherKey=key+MAC::默认密钥长度;
}
其他的
{
密码密钥=密钥;
macKey=键+明文长度;
}
ConstByteArrayParameter编码参数
javax.crypto.BadPaddingException: pad block corrupted
at org.bouncycastle.jcajce.provider.asymmetric.ec.IESCipher.engineDoFinal(Unknown Source)
at javax.crypto.Cipher.doFinal(Cipher.java:2087)
...
//! \class DL_EncryptionAlgorithm_Xor
//! \brief P1363 based XOR Encryption Method
//! \tparam MAC MessageAuthenticationCode derived class used for MAC computation
//! \tparam DHAES_MODE flag indicating DHAES mode
//! \tparam LABEL_OCTETS flag indicating the label is octet count
//! \details DL_EncryptionAlgorithm_Xor is based on an early P1363 draft, which itself appears to be based on an
//! early Certicom SEC-1 draft (or an early SEC-1 draft was based on a P1363 draft). Crypto++ 4.2 used it in its Integrated
//! Ecryption Schemes with <tt>NoCofactorMultiplication</tt>, <tt>DHAES_MODE=false</tt> and <tt>LABEL_OCTETS=true</tt>.
//! \details If you need this method for Crypto++ 4.2 compatibility, then use the ECIES template class with
//! <tt>NoCofactorMultiplication</tt>, <tt>DHAES_MODE=false</tt> and <tt>LABEL_OCTETS=true</tt>.
//! \details If you need this method for Bouncy Castle 1.54 and Botan 1.11 compatibility, then use the ECIES template class with
//! <tt>NoCofactorMultiplication</tt>, <tt>DHAES_MODE=ture</tt> and <tt>LABEL_OCTETS=false</tt>.
//! \details Bouncy Castle 1.54 and Botan 1.11 compatibility are the default template parameters.
//! \since Crypto++ 4.0
template <class MAC, bool DHAES_MODE, bool LABEL_OCTETS=false>
class DL_EncryptionAlgorithm_Xor : public DL_SymmetricEncryptionAlgorithm
{
public:
bool ParameterSupported(const char *name) const {return strcmp(name, Name::EncodingParameters()) == 0;}
size_t GetSymmetricKeyLength(size_t plaintextLength) const
{return plaintextLength + static_cast<size_t>(MAC::DIGESTSIZE);}
size_t GetSymmetricCiphertextLength(size_t plaintextLength) const
{return plaintextLength + static_cast<size_t>(MAC::DIGESTSIZE);}
size_t GetMaxSymmetricPlaintextLength(size_t ciphertextLength) const
{return SaturatingSubtract(ciphertextLength, static_cast<size_t>(MAC::DIGESTSIZE));}
void SymmetricEncrypt(RandomNumberGenerator &rng, const byte *key, const byte *plaintext, size_t plaintextLength, byte *ciphertext, const NameValuePairs ¶meters) const
{
CRYPTOPP_UNUSED(rng);
const byte *cipherKey = NULL, *macKey = NULL;
if (DHAES_MODE)
{
macKey = key;
cipherKey = key + MAC::DEFAULT_KEYLENGTH;
}
else
{
cipherKey = key;
macKey = key + plaintextLength;
}
ConstByteArrayParameter encodingParameters;
parameters.GetValue(Name::EncodingParameters(), encodingParameters);
if (plaintextLength) // Coverity finding
xorbuf(ciphertext, plaintext, cipherKey, plaintextLength);
MAC mac(macKey);
mac.Update(ciphertext, plaintextLength);
mac.Update(encodingParameters.begin(), encodingParameters.size());
if (DHAES_MODE)
{
byte L[8];
PutWord(false, BIG_ENDIAN_ORDER, L, (LABEL_OCTETS ? word64(encodingParameters.size()) : 8 * word64(encodingParameters.size())));
mac.Update(L, 8);
}
mac.Final(ciphertext + plaintextLength);
}
DecodingResult SymmetricDecrypt(const byte *key, const byte *ciphertext, size_t ciphertextLength, byte *plaintext, const NameValuePairs ¶meters) const
{
size_t plaintextLength = GetMaxSymmetricPlaintextLength(ciphertextLength);
const byte *cipherKey, *macKey;
if (DHAES_MODE)
{
macKey = key;
cipherKey = key + MAC::DEFAULT_KEYLENGTH;
}
else
{
cipherKey = key;
macKey = key + plaintextLength;
}
ConstByteArrayParameter encodingParameters;
parameters.GetValue(Name::EncodingParameters(), encodingParameters);
MAC mac(macKey);
mac.Update(ciphertext, plaintextLength);
mac.Update(encodingParameters.begin(), encodingParameters.size());
if (DHAES_MODE)
{
byte L[8];
PutWord(false, BIG_ENDIAN_ORDER, L, (LABEL_OCTETS ? word64(encodingParameters.size()) : 8 * word64(encodingParameters.size())));
mac.Update(L, 8);
}
if (!mac.Verify(ciphertext + plaintextLength))
return DecodingResult();
if (plaintextLength) // Coverity finding
xorbuf(plaintext, ciphertext, cipherKey, plaintextLength);
return DecodingResult(plaintextLength);
}
};