【问题标题】:Using decrypted DES key obtained from RSA decryption使用从 RSA 解密获得的解密 DES 密钥
【发布时间】:2018-10-14 12:32:11
【问题描述】:

我正在研究 java 中的混合加密机制,该机制涉及使用 3DES 加密算法加密消息,然后在发送方使用 RSA 加密机制加密其密钥。一旦传送到接收方,加密的 3DES 密钥使用 RSA 解密机制解密,然后用于解密密文。 一旦我获得解密的 3DES 密钥,它的字符串值是相同的,但 byte [] 不一样,而是返回原始密钥的 2 的补码。

如何让解密后的 3DES 与接收端最初生成的 byte [] 形式的 3DES 相同?

以下是我用于混合加密机制的代码: 包 hybrid_implementation;

import java.security.Key;
import java.security.InvalidKeyException;
import java.security.spec.InvalidKeySpecException;
import java.security.NoSuchAlgorithmException;
import java.util.Scanner;
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.Random;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.BadPaddingException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.IllegalBlockSizeException;

public class Hybrid_Implementation {

//RSA_Encryption Algorithm Required Variables
private static final BigInteger one = new BigInteger("1");
private static final SecureRandom random = new SecureRandom();
private BigInteger privatekey;
private BigInteger publickey;
private BigInteger modulus;

//3DES_Encryption Algorithm Required Variables
private byte[] DES_Key;
private SecretKeyFactory keyfactory;
private DESedeKeySpec spec;
private Key deskey;
private int DES_Key_Length;
private byte[] data;
private Cipher cipher;
private String CipherText;
private byte [] CIPHERText;

Hybrid_Implementation() throws InvalidKeyException, 
NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException
{
    DES_Key_Generator();
    RSA_Key_Generator(999);
}

//3DES Encryption-Decryption Algorithm with 2 differnt keys
private String DES_Encryption(String plaintext) throws InvalidKeyException, 
IllegalBlockSizeException, BadPaddingException
{
    data = plaintext.getBytes();
    cipher.init(Cipher.ENCRYPT_MODE, deskey);
    CIPHERText = cipher.doFinal(data);
    StringBuilder hexCiphertext = new StringBuilder();
    for(int i=0; i<CIPHERText.length; i++)
    {
        int v = CIPHERText[i] & 0xff;
        v+=0x100;
        String temp = Integer.toString(v,16);
        hexCiphertext.append(temp).substring(1);
    }
    return hexCiphertext.toString();
}

private String DES_Decryption(byte [] key, byte [] encrypted_text) throws 
InvalidKeyException, IllegalBlockSizeException, BadPaddingException, 
InvalidKeySpecException
{
   spec = new DESedeKeySpec(key);
   deskey = keyfactory.generateSecret(spec);
    byte[] plaintext = cipher.doFinal(encrypted_text);
    StringBuilder decrypttext= new StringBuilder();
    for (int i = 0; i < plaintext.length; i++)
        decrypttext.append((char) plaintext[i]);
    String decrypted_plaintext = decrypttext.toString();
    return decrypted_plaintext;
}

private void DES_Key_Generator() throws InvalidKeyException, 
NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException
{
    Random rnd = new Random();
    String key = rnd.toString();

    DES_Key = key.getBytes();
    spec = new DESedeKeySpec(DES_Key);
    keyfactory = SecretKeyFactory.getInstance("desede");
    deskey = keyfactory.generateSecret(spec);
    cipher = Cipher.getInstance("desede");
}


//RSA Encryption-Decryption Algorithm
private BigInteger RSA_Encryption(BigInteger des_Key )  //RSA Encryption of 
3DES Key
{
     BigInteger encrypted_DES_Key = des_Key.modPow(publickey, modulus);
     return encrypted_DES_Key;
}

private BigInteger RSA_Decryption(BigInteger encrypted_DES_Key) //RSA 
Decryption of 3DES Key
{
    BigInteger des_Key = encrypted_DES_Key.modPow(privatekey, modulus);
    return des_Key;
}

private void RSA_Key_Generator(int number)     //RSA Public - Private Key 
Generation
{
    BigInteger p = BigInteger.probablePrime(number/2,random);
    BigInteger q = BigInteger.probablePrime(number/2, random);
    BigInteger phi = (p.subtract(one)).multiply(q.subtract(one));

    modulus = p.multiply(q);
    publickey = new BigInteger("65537");
    privatekey = publickey.modInverse(phi);
}

private String encryption(String plaintext) throws InvalidKeyException, 
IllegalBlockSizeException, BadPaddingException
{
    String cipher_text = DES_Encryption(plaintext);
    BigInteger RSA_DESKey = RSA_Encryption(new BigInteger(DES_Key));
    String temp_key = RSA_DESKey.toString();
    DES_Key_Length = temp_key.length();
    CipherText ="";
    CipherText = new 
StringBuilder().append(temp_key).append(cipher_text).toString();
    return CipherText;
}

private String decryption(String encrypted_text) throws InvalidKeyException, 
InvalidKeySpecException, IllegalBlockSizeException, BadPaddingException
{
    StringBuilder encryptedkey = new StringBuilder();
    for(int i = 0 ; i < DES_Key_Length; i++)
        encryptedkey.append (encrypted_text.charAt(i));
    StringBuilder cipheredtext = new StringBuilder();
    for(int j = DES_Key_Length ; j< encrypted_text.length() ; j++)
        cipheredtext.append (encrypted_text.charAt(j));
    BigInteger DES_Encrypted_Key = new BigInteger(encryptedkey.toString());
    BigInteger DES_KEY = RSA_Decryption(DES_Encrypted_Key);
    byte[] decrypt_key = DES_KEY.toByteArray();
    String plaintext = 
DES_Decryption(decrypt_key,cipheredtext.toString().getBytes());
       return plaintext;
}

/**
 *
 * @param args
 * @throws InvalidKeyException
 * @throws IllegalBlockSizeException
 * @throws BadPaddingException
 * @throws java.security.NoSuchAlgorithmException
 * @throws java.security.spec.InvalidKeySpecException
 * @throws javax.crypto.NoSuchPaddingException
 */
public static void main(String[] args) throws InvalidKeyException, 
IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, 
InvalidKeySpecException, NoSuchPaddingException {
    String plaintext;
    Hybrid_Implementation hi = new Hybrid_Implementation ();
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter Text = ");
    plaintext = sc.nextLine();
    String encrypted_text = hi.encryption(plaintext);
    String decrypted_text = hi.decryption(encrypted_text);
    System.out.println("Plain Text Entered = "+plaintext);
    System.out.println("Encrypted Text = "+encrypted_text);
    System.out.println("Decrypted Text = "+decrypted_text);
    }

}

我收到的输出是: enter image description here 而解密后的文本与输入的纯文本不同

【问题讨论】:

  • 你真的是说它是值的 negative,还是说它显示 as 使用 System.out.print(bytearray[i]) 或 @987654324 之类的符号签名@?后者是预期的,因为 Java 中的 byte 类型已签名。然而,Java crypto(以及 I/O)将 byte[] 元素(密钥/IVs/等和数据)视为未签名并正常工作。以十六进制显示(有时输入)加密值通常更清楚。 OTOH 键不应仅限于有效字符,因此将它们视为String 通常会产生错误的结果。
  • 下面是我用于混合加密机制的代码:
  • 您确定要使用自己的 RSA 实现而不使用安全填充吗?您没有消息身份验证,因此您的消息很容易被篡改。考虑使用 Java 的内置 RSA/ECB/PKCS1PaddingRSA/ECB/OAEPWithSHA-1AndMGF1Padding RSA 模式。

标签: java encryption rsa 3des


【解决方案1】:

您的代码存在多个(很多)问题

主要问题(为什么您得到不正确的解密文本)在于密文编码。 您使用 3DES 解密的密文与您从加密中获得的密文不同(您的“十六进制编码”只是有问题)。只需调试您的程序(并在加密之后和解密之前打印值)你应该找到它。我建议使用一些标准的东西,例如工作十六进制或 base64 编码。

其他问题:

  • 您使用的“教科书 RSA”并不真正安全,所以我希望您这样做是为了学习/分配目的,而不是现实生活中的加密应用程序。如前所述,使用 RSA 时应始终使用填充(例如 RSA/ECB/PKCS1Padding 或 OAEP)
  • 生成随机密钥 - 您应该使用 SecureRandom 而不是 Random(这不是真正的随机)或 - 更好 - KeyGenerator
  • DESede 不提供任何 IV 使用 ECB 模式,这有其弱点。所以使用对称加密你应该提供随机IV并明确指定加密模式(例如DESede/CBC/PKCS5Padding
  • 对于doFinal() 之后的每个操作,您应该使用具有初始化解密模式的新 Cipher 实例

我有一个blog about encryption 有几个例子,你可以从中获得灵感。

【讨论】:

  • Nit: .getInstance("DESede") 不指定 mode 默认为 ECB。使用IvParameterSpec 指定"DESede/CBC/PKCS5Padding" 而不是initing 隐式使用内部生成的随机IV,然后您必须将其提取并传递给解密;这比为两个方向显式生成和设置它有点笨拙,但确实有效。
猜你喜欢
  • 1970-01-01
  • 2020-02-17
  • 1970-01-01
  • 2012-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-03
  • 2018-09-10
相关资源
最近更新 更多