我编写了这个简单的Java程序,它加密一个字符串并输出iv、salt、派生密钥和密码文本的十六进制值。

public class tmp{
    static Cipher encryptionCipher;
    static String RANDOM_ALGORITHM = "SHA1PRNG";
    static String PBE_ALGORITHM = "PBEWithSHA256And256BitAES-CBC-BC";
    static String CIPHER_ALGORITHM = "AES/CBC/PKCS7Padding";
    static String SECRET_KEY_ALGORITHM = "AES";
    static int PBE_ITERATION_COUNT = 2048;
    static String PROVIDER = "BC";

    public static byte[] generateIv() {
        try{
        SecureRandom random;
        random = SecureRandom.getInstance(RANDOM_ALGORITHM);
        byte[] iv = new byte[16];
        random.nextBytes(iv);
        return iv;
        } catch(Exception e){
        return null;            // Always must return something
        }
    }

    public static byte[] generateSalt() {
         try {SecureRandom random;
         random = SecureRandom.getInstance(RANDOM_ALGORITHM);
         byte[] salt = new byte[32];
         random.nextBytes(salt);
         return salt;
         } catch(Exception e){
        return null;            // Always must return something
    }
     }

     public static SecretKey getSecretKey(String password, byte[] salt){
         try {
            PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray(), salt, PBE_ITERATION_COUNT, 256);
            SecretKeyFactory factory = SecretKeyFactory.getInstance(PBE_ALGORITHM, PROVIDER);
            SecretKey tmp = factory.generateSecret(pbeKeySpec);
            return new SecretKeySpec(tmp.getEncoded(), SECRET_KEY_ALGORITHM);
        } catch(Exception e){
        System.out.println(e);            // Always must return something
        return null;
        }
     }

     public static String encrypt(String plaintext, Key key, byte[] iv) {
        try {
            AlgorithmParameterSpec ivParamSpec = new IvParameterSpec(iv);
            encryptionCipher = Cipher.getInstance(CIPHER_ALGORITHM, PROVIDER);
            encryptionCipher.init(Cipher.ENCRYPT_MODE, key, ivParamSpec);
            byte[] ciphertext = encryptionCipher.doFinal(plaintext.getBytes("UTF-8"));
            String cipherHexString = DatatypeConverter.printHexBinary(ciphertext);
            return cipherHexString;
        }
        catch (Exception e) {
             System.out.println(e);
            return null;
        }

    }

     public static void main (String[] Args){
         SecretKey key;
         //sha512(ciao)
         String encami = "This is a test pharse. Thanks!!";
         String password = "a0c299b71a9e59d5ebb07917e70601a3570aa103e99a7bb65a58e780ec9077b1902d1dedb31b1457beda595fe4d71d779b6ca9cad476266cc07590e31d84b206";
         byte[] iv = new byte[16];
         byte[] salt = new byte[32];
         iv = generateIv();
         salt = generateSalt();
         String ll = DatatypeConverter.printHexBinary(iv);
         String lp = DatatypeConverter.printHexBinary(salt);
         System.out.println(ll);
         System.out.println(lp);
         key = getSecretKey(password, salt);
         byte tt[] = new byte[32];
         tt = key.getEncoded();
         String lo = DatatypeConverter.printHexBinary(tt);
         System.out.println(lo);
         String outenc = encrypt(encami, key, iv);
         System.out.println(outenc);
     }
}

在下面的C程序中,iv和salt用上述Java程序给出的值初始化因为文本的长度是32字节,所以不需要填充。
#include <stdio.h>
#include <gcrypt.h>
#include <stdlib.h>
#include <string.h>

int
main (void)
{
    int i;
    char *encami = "This is a test pharse. Thanks!!";
    char *pwd = "a0c299b71a9e59d5ebb07917e70601a3570aa103e99a7bb65a58e780ec9077b1902d1dedb31b1457beda595fe4d71d779b6ca9cad476266cc07590e31d84b206";
    unsigned char iv[] = {};
    unsigned char salt[] = {};
    int algo = gcry_cipher_map_name("aes256");
    unsigned char *devkey = NULL;
    unsigned char *enc_buf = NULL;

    enc_buf = gcry_malloc(32);
    devkey = gcry_malloc_secure (32);

    gcry_cipher_hd_t hd;
    gcry_cipher_open(&hd, algo, GCRY_CIPHER_MODE_CBC, 0);
    gcry_kdf_derive (pwd, strlen(pwd)+1, GCRY_KDF_PBKDF2, GCRY_MD_SHA256, salt, 32, 2048, 32, devkey);
    for (i=0; i<32; i++)
        printf ("%02x", devkey[i]);
    printf("\n");
    gcry_cipher_setkey(hd, devkey, 32);
    gcry_cipher_setiv(hd, iv, 16);

    gcry_cipher_encrypt(hd, enc_buf, strlen(encami)+1, encami, strlen(encami)+1);

    for (i=0; i<32; i++)
        printf("%02x", enc_buf[i]);
    printf("\n");
    gcry_cipher_close(hd);
    gcry_free(enc_buf);
    gcry_free (devkey);
    return 0;
}

我的问题是这两个程序的派生密钥不一样为什么?
bouncy castle派生函数的工作方式是否与gcry-kdf-derive不同?
谢谢!

最佳答案

我现在研究了BC提供者中的pbewithsha256和256位CBC-BC算法,发现它与GCRY-KDF-PBKDF2不兼容gcrypt算法是PKCS#52.0方案2,而BC算法实际上是在实现PKCS#12。
实际上,到目前为止,我还没有在提供程序中找到与gcrypt匹配的命名算法,但是我可以直接使用BC API获得与gcrypt匹配的结果,如下所示。
弹跳城堡:

    byte[] salt = new byte[8];
    Arrays.fill(salt, (byte)1);
    PBEParametersGenerator pGen = new PKCS5S2ParametersGenerator(new SHA256Digest());
    pGen.init(Strings.toByteArray("password"), salt, 2048);
    KeyParameter key = (KeyParameter)pGen.generateDerivedParameters(256);
    System.out.println(Hex.toHexString(key.getKey()));

gcrypt公司:
unsigned char salt[8];
memset(salt, 1, 8);

unsigned char key[32];
gcry_kdf_derive("password", 8, GCRY_KDF_PBKDF2, GCRY_MD_SHA256, salt, 8, 2048, 32, key);

for (int i = 0; i < 32; ++i)
    printf("%02x", key[i]);
printf("\n");

两者都输出:
4182537a153b1f0da1ccb57971787a42537e38dbf2b4aa3692baebb106fc02e8

07-24 14:48