Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ JAVA(AES/CBC/PKCS5PADDING)对应的C/C++;_C++_C_Aes_Crypto++ - Fatal编程技术网

C++ JAVA(AES/CBC/PKCS5PADDING)对应的C/C++;

C++ JAVA(AES/CBC/PKCS5PADDING)对应的C/C++;,c++,c,aes,crypto++,C++,C,Aes,Crypto++,Cipher.getInstance(“AES/CBC/PKCS5PADDING”)i使用Sun JCE提供程序: public static void main(String[] args) throws UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, ShortBufferException, IllegalBlockSizeExcepti

Cipher.getInstance(“AES/CBC/PKCS5PADDING”)i使用Sun JCE提供程序:

public static void main(String[] args) throws UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, ShortBufferException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException
{
    System.out.println("=======================AES/CBC/PKCS5Padding=====================");
    // key
    byte[] key = "0123456789abcdef".getBytes("UTF-8");
    dump("key", key);
    // iv
    byte[] iv = "fedcba9876543210".getBytes("UTF-8");
    dump("iv", iv);

    byte[] indata = "bsmith is a good guy.".getBytes("UTF-8");
    dump("indata", indata);

    AES aes = new AES();
    aes.init(key, iv);
    byte[] outdata = aes.encrypt(indata);
    dump("outdata", outdata);

    byte[] indata1 = aes.decrypt(outdata);
    dump("indata1", indata1);
}

private Cipher enc;
private Cipher dec;
private SecretKeySpec keySpec;
private IvParameterSpec ivSpec;

public AES()
{
}

/**
 * init the AES key.
 * the key must be 128, 192, or 256 bits.
 * @param key the AES key.
 * @param keyoff the AES key offset.
 * @param keylen the AES key length, the key length must be 16 bytes because SunJCE only support 16 bytes key.
 * @param iv the IV for CBC, the length of iv must be 16 bytes.
 * @param ivoff the iv offset.
 */
public void init(byte[] key, int keyoff, int keylen, byte[] iv, int ivoff)
{
    keySpec = new SecretKeySpec(key, keyoff, keylen, "AES");
    ivSpec = new IvParameterSpec(iv, ivoff, 16);
}

/**
 * init the AES key.
 * the key must be 16 bytes, because SunJCE only support 16 bytes key..
 * @param key the AES key.
 * @param iv the iv for CBC, iv must be 16 bytes length.
 */
public void init(byte[] key, byte[] iv)
{
    keySpec = new SecretKeySpec(key, "AES");
    ivSpec = new IvParameterSpec(iv);
}

/**
* get the maximal cipher data length after encrypted.
* @param len the plain data length.
* @return the cipher data length.
*/
public int getCipherLen(int len)
{
    // for PKCS#1 v1.5 padding
    // max padding BLOCK_SIZE=16.
    int pad = len%16;
    if (0 == pad)
    {
        return len + 16;
    }
    return len - pad + 16;
}

/**
 * encrypt the input data to output data.
 * the input data length must be the times of 16 bytes.
 * and the output data length is equals to the input data.
 * @param indata the input data.
 * @param inoff the input data offset.
 * @param inlen the input data length.
 * @param outdata the output data.
 * @param outoff the output data offset.
 */
public void encrypt(byte[] indata, int inoff, int inlen, byte[] outdata, int outoff) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, ShortBufferException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException
{
    initEncryptor();
    enc.doFinal(indata, inoff, inlen, outdata, outoff);
}

/**
 * encrypt the input data to output data.
 * @param indata the input data.
 * @param inoff the input data offset.
 * @param inlen the input data length.
 * @return the output encrypted data.
 */
public byte[] encrypt(byte[] indata, int inoff, int inlen) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, ShortBufferException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException
{
    initEncryptor();
    return enc.doFinal(indata, inoff, inlen);
}

/**
 * encrypt the input data to output data.
 * @param indata the input data.
 * @return the output data.
 * @throws NoSuchAlgorithmException
 * @throws NoSuchPaddingException
 * @throws InvalidKeyException
 * @throws InvalidAlgorithmParameterException
 * @throws IllegalBlockSizeException
 * @throws BadPaddingException
 */
public byte[] encrypt(byte[] indata) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException
{
    initEncryptor();
    return enc.doFinal(indata);
}

/**
* the maximal plain data length after decrypted.
* @param len the cipher data length that will be decrypted.
* @return the maximal plain data length.
*/
public int getPlainLen(int len)
{
    // for PKCS#1 v1.5 padding
    // len always be times of BLOCK_SIZE=16.
    return len;
}

/**
 * decrypt the input data to output data.
 * @param indata the input data.
 * @param inoff the input data offset.
 * @param inlen the input data length.
 * @param outdata the output data.
 * @param outoff the output data offset.
 */
public void decrypt(byte[] indata, int inoff, int inlen, byte[] outdata, int outoff) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, ShortBufferException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException
{
    initDecryptor();
    dec.doFinal(indata, inoff, inlen, outdata, outoff);
}

/**
 * decrypt the input data to output data.
 * @param indata the input data.
 * @param inoff the input data offset.
 * @param inlen the input data length.
 * @return the output decrypted data.
 */
public byte[] decrypt(byte[] indata, int inoff, int inlen) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, ShortBufferException, InvalidAlgorithmParameterException
{
    initDecryptor();
    return dec.doFinal(indata, inoff, inlen);
}

/**
 * decrypt the input data to output data.
 * @param indata the input cipher data.
 * @return the output plain data.
 * @throws NoSuchAlgorithmException
 * @throws NoSuchPaddingException
 * @throws InvalidKeyException
 * @throws InvalidAlgorithmParameterException
 * @throws IllegalBlockSizeException
 * @throws BadPaddingException
 */
public byte[] decrypt(byte[] indata) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException
{
    initDecryptor();
    return dec.doFinal(indata);
}

private void initEncryptor() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException
{
    if (null == enc)
    {
        enc = Cipher.getInstance("AES/CBC/PKCS5Padding");
        enc.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
    }
}

private void initDecryptor() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException
{
    if (null == dec)
    {
        dec = Cipher.getInstance("AES/CBC/PKCS5Padding");
        dec.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
    }
}
}

对应于什么是C/C++?我发现crypto++恰到好处,其他的都没有,但是里面的算法太多了,我只需要有AES。那太复杂了,我不会,我是一个新手,你熟悉这个算法吗,我想把它移植到Android上,打包成这样。

首先,PKCS#5填充更好地被称为PKCS#7填充。PKCS#5填充实际上用于8字节块密码,PKCS#7填充用于16字节块密码。否则,它们是相同的

如果加密库提供AES和CBC,则它是正确的。这或多或少是给定的,因为CBC是更常见的模式之一。正确的填充模式的可用性可能有所不同,尽管大多数C++库都支持PKCSα5填充,因为它是事实上的标准。
由于有(NIST)测试向量可用,而且字节顺序已知,任何支持算法、模式和填充模式组合的库都应该是可互操作的。

请注意包装器库,如果它们不添加任何标准功能之外的内容,则弊大于利。与低级语言相比,Java需要更少的类型转换或资源管理,因此通常您不需要这么多。我用它来加密大数据,它太耗时,NDK速度明显更快。是的,NDK可以明显更快。例如,了解缓冲区处理并使用
enc.update()
调用而不是仅使用
enc.doFinal()
也会产生很大的不同。这就是我所说的包装libs的危险,您刚刚从视图中隐藏了那些
update()
方法。但是我看到它们大多数都缺少向量ivi如果它们包含CBC,那么必须有一种方法来设置IV,大多数情况下它是在更新之前的初始化阶段设置的(中间加密)或者加密的最后一步。@mryys您找到了同样的东西吗?我也在寻找same@MaartenBodewes我也在寻找同样的。在我的例子中,我有使用
Cipher.getInstance(“AES/CBC/PKCS5Padding”,“SunJCE”)用java加密的文本现在我想用c/c++解密该文本。你能告诉我任何库或其他方式吗?OpenSSL、Crypto++和Botan都应该能够实现CBC模式。