当前位置: 首页 > 面试题库 >

在Java中生成和使用两个密钥进行加密和解密

高迪
2023-03-14
问题内容

我正在使用Java应用程序,该应用程序要求我使用从不同字符串生成的两个密钥进行加密和解密。一个字符串来自用户,另一个是主密钥。我上网查看了有关它的一些参考资料。我真的很想知道如何实现此目标。我将展示我现在拥有的。

因此,从代码中可以看到,我使用了其他stackoverflow帖子中的一些代码,并对其进行了一些修改。我只是不知道如何从2个字符串生成2个密钥,以及从哪里可以获取用于解密的SecretKey
desKey。

代码:

public class Encryption {

public void doStuff() {

    String plaintext = "abc";

    SecretKey k1 = generateDESkey();
    SecretKey k2 = generateDESkey();


    String firstEncryption = desEncryption(plaintext, k1);
    String decryption = desDecryption(firstEncryption, k2);
    String secondEncryption = desEncryption(decryption, k1);

    System.out.println(firstEncryption);
    System.out.println(decryption);
    System.out.println(secondEncryption);
}

public static SecretKey generateDESkey() {
    KeyGenerator keyGen = null;
    try {
        keyGen = KeyGenerator.getInstance("DESede");
    } catch (NoSuchAlgorithmException ex) {
        Logger.getLogger(Test.class
                .getName()).log(Level.SEVERE, null, ex);
    }
    try {
        assert keyGen != null;
        keyGen.init(112); // key length 56
        return keyGen.generateKey();
    } catch (NullPointerException ex){
        return null;
    }
}


public static String desEncryption(String strToEncrypt, SecretKey desKey) {
    try {
        Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, desKey);
        return Base64.encode(cipher.doFinal(strToEncrypt.getBytes()));
    } catch (NoSuchAlgorithmException | NoSuchPaddingException |
            IllegalBlockSizeException | BadPaddingException |
            InvalidKeyException ex) {
        Logger.getLogger(Test.class
                .getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}


public static String desDecryption(String strToDecrypt, SecretKey desKey) {
    try {
        Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, desKey);
        return new String(cipher.doFinal(Base64.decode(strToDecrypt)));

    } catch (NoSuchAlgorithmException |  BadPaddingException | IllegalBlockSizeException
            | InvalidKeyException | NoSuchPaddingException ex) {
        Logger.getLogger(Test.class
                .getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}

}

如果有任何困惑或怀疑。请让我知道。


问题答案:

在更改代码之前先对其进行回答。

您正在尝试仅使用两个键而不是三个键来进行DESede。

这通常可能会奏效,但并非如您所写。问题是填充。在第二步中,您尝试使用不同于加密密钥的其他密钥解密密文,因此解密将失败超过256次中的255次,因为填充将是错误的(还因为您在其中使用Base64编码)没有必要)。

如果您确实想这样做,则必须解密而无需填充和Base64编码。好消息是,未编码的密文已经是块大小的倍数,因此不会阻止您使用"DES/ECB/NoPadding"

public static void main(String[] args) {
    // First I would like to create keys by giving Strings
    SecretKey k1 = generateDESkey();
    SecretKey k2 = generateDESkey();

    // encryption
    byte[] firstEncryption = desEncryption("plaintext".getBytes("UTF-8"), k1, false);
    byte[] decryption = desDecryption(firstEncryption, k2, true);
    byte[] secondEncryption = desEncryption(decryption, k1, true);

    // decryption
    byte[] firstDecryption = desDecryption(secondEncryption, k1, true);
    byte[] encryption = desEncryption(firstDecryption, k2, true);
    byte[] secondDecryption = desDecryption(encryption, k1, false);

    System.out.println(new String(secondDecryption)); // plaintext
}

public static byte[] desEncryption(byte[] strToEncrypt, SecretKey desKey, boolean noPadding) {
    try {
        Cipher cipher = Cipher.getInstance(noPadding ? "DES/ECB/NoPadding" : "DES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, desKey);
        return cipher.doFinal(strToEncrypt);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}

public static byte[] desDecryption(byte[] strToDecrypt, SecretKey desKey, boolean noPadding) {
    try {
        Cipher cipher = Cipher.getInstance(noPadding ? "DES/ECB/NoPadding" : "DES/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, desKey);
        return cipher.doFinal(strToDecrypt);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}

当以这种方式构造通用密钥时,这实际上是具有两个密钥的DESede的等效实现:

SecretKey k1 = generateDESkey();
SecretKey k2 = generateDESkey();

byte[] edeKeyBytes = new byte[24];
System.arraycopy(k1.getEncoded(), 0, edeKeyBytes, 0, 8);
System.arraycopy(k2.getEncoded(), 0, edeKeyBytes, 8, 8);
System.arraycopy(k1.getEncoded(), 0, edeKeyBytes, 16, 8);

edeKey = new SecretKeySpec(edeKeyBytes, "DESede");

Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, edeKey);

System.out.println(Base64.encode(cipher.doFinal("plaintext".getBytes("UTF-8"))));

DESede使用三个密钥,我们将其称为k1,k2和k3。所有这些都串联到一个字节数组中。在您的情况下,第二次使用k1代替k3。



 类似资料:
  • 我正在开发一个Java应用程序,它要求我使用从不同字符串生成的两个密钥进行加密和解密。一个字符串来自用户,另一个是主密钥。我在网上查了一下,找到了一些关于它的参考资料。我真的很想得到一些帮助,了解如何实现这一点。我将展示我现在所拥有的。 正如您从代码中看到的,我使用了其他stackoverflow帖子中的一些代码,并对其进行了一些修改。我只是不知道如何从2个字符串生成2个密钥,以及从哪里可以获得用

  • 这是可能的还是加密必须共享和使用相同的密钥? 主要目的就是这样。 我将有两个客户端可以发送和接收加密数据到彼此。

  • 使用Python-GnuPG我想 null 不幸的是,加密返回错误: 但它仍然生成加密ASCII铠甲消息,如果解密结果为对象,则bool值为属性,并包含以下属性: 不确定错误发生的确切位置以及如何处理

  • 问题内容: 这是我正在做的事情,可能看起来有些笨拙,但是可以帮助您解决该问题。我得到一个。阅读几乎所有相关主题,但找不到合适的解决方案。我是加密解密程序设计的新手,需要在我的Java应用程序之一中实现它。 谢谢..这就是代码的样子.... 问题答案: 在这里,您需要了解的是密文可能包含不可打印的字符。因此,当您使用readLine()时,它可能不会为您提供文件中的所有字节。 同样,它并没有给您您认

  • 我正在尝试将我的应用程序从128位AES密钥升级为256位AES。然而,当我将第54行从128更改为256时,我会得到以下密钥大小错误。 java.security.无效密钥异常: 非法的密钥大小 我已正确安装了JCE文件,我的应用程序生成较长的密钥这一事实证明了这一点。 我在其他文章中看到过“AES / CBC / PKCS7Padding”加密方法,但这只会让我遇到这个例外:

  • 问题内容: 我想生成rsa密钥对(公共和私有),然后将它们用于AES加密和解密。例如,用于加密的公共密钥和用于解密的私有密钥。我为此编写了一个简单的代码,但是问题是当我运行时这段代码我得到这个错误: 我该如何解决这个问题?我的加密代码如下: 问题答案: 如评论中所建议,我搜索了“混合密码术”。这个例子解决了我的问题。