📜  Java的非对称加密密码学

📅  最后修改于: 2021-04-17 01:49:43             🧑  作者: Mango

密码学是对从未授权实体中保护数据的各种技术的研究。在计算机科学中,我们尝试开发保护敏感数据的策略和实践。大多数密码学都涉及用于保护数据的非常先进的数学功能。为密码学开发的算法的唯一目的是隐藏攻击者或中间人的数据。
在上一篇文章中,我们研究了执行对称加密和解密的不同方法,类和方法。在本文中,我们将了解非对称加密。
非对称加密(也称为私钥/公钥)是两个密钥之间的数学关系,一个用于加密,另一个用于解密。例如,如果有两个密钥“ K1”和“ K2”,则如果密钥“ K1”用于加密,而密钥“ K2”用于解密。如果将“ K1”用于解密,则将“ K2”用于加密。以下是最常用的非对称加密算法:

  • 3DES(三重DES)数据加密标准。
  • RSA算法代表RSA的Rivest,Shamir和Adleman的创建者。
  • 河豚是DES和RSA的替代品。
  • AES(高级加密标准)。
  • IDEA(国际数据加密算法)。

在使用RSA算法实现非对称加密之前,我们将首先了解如何生成密钥对(公共,私有)。为了生成非对称密钥,可以遵循以下步骤:

  • 我们首先需要使用SecureRandom类生成公钥和私钥。 SecureRandom类用于生成随机数。
  • KeyGenerator类将提供getInstance()方法,该方法可用于传递表示密钥生成算法的字符串变量。它返回KeyGenerator对象。我们正在使用RSA算法生成密钥。
  • 使用2048位密钥大小初始化keyGenerator对象,并传递随机数。
  • 现在,将生成密钥,如果我们希望实际看到生成的密钥(它是一个对象),则可以使用DatatypeConverter将其转换为十六进制格式。

下面是上述方法的实现:

Java
// Java program to create a
// asymmetric key
 
package java_cryptography;
import java.security.KeyPair;
import java.security
    .KeyPairGenerator;
import java.security
    .SecureRandom;
import javax.xml.bind
    .DatatypeConverter;
 
// Class to create an asymmetric key
public class Asymmetric {
 
    private static final String RSA
        = "RSA";
 
    // Generating public and private keys
    // using RSA algorithm.
    public static KeyPair generateRSAKkeyPair()
        throws Exception
    {
        SecureRandom secureRandom
            = new SecureRandom();
 
        KeyPairGenerator keyPairGenerator
            = KeyPairGenerator.getInstance(RSA);
 
        keyPairGenerator.initialize(
            2048, secureRandom);
 
        return keyPairGenerator
            .generateKeyPair();
    }
 
    // Driver code
    public static void main(String args[])
        throws Exception
    {
        KeyPair keypair
            = generateRSAKkeyPair();
 
        System.out.println(
            "Public Key is: "
            + DatatypeConverter.printHexBinary(
                  keypair.getPublic().getEncoded()));
 
        System.out.println(
            "Private Key is: "
            + DatatypeConverter.printHexBinary(
                  keypair.getPrivate().getEncoded()));
    }
}


Java
// Java program to perform the
// encryption and decryption
// using asymmetric key
 
package java_cryptography;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.util.Scanner;
 
import javax.crypto.Cipher;
import javax.xml.bind
    .DatatypeConverter;
 
public class Asymmetric {
 
    private static final String RSA
        = "RSA";
    private static Scanner sc;
 
    // Generating public & private keys
    // using RSA algorithm.
    public static KeyPair generateRSAKkeyPair()
        throws Exception
    {
        SecureRandom secureRandom
            = new SecureRandom();
        KeyPairGenerator keyPairGenerator
            = KeyPairGenerator.getInstance(RSA);
 
        keyPairGenerator.initialize(
            2048, secureRandom);
        return keyPairGenerator
            .generateKeyPair();
    }
 
    // Encryption function which converts
    // the plainText into a cipherText
    // using private Key.
    public static byte[] do_RSAEncryption(
        String plainText,
        PrivateKey privateKey)
        throws Exception
    {
        Cipher cipher
            = Cipher.getInstance(RSA);
 
        cipher.init(
            Cipher.ENCRYPT_MODE, privateKey);
 
        return cipher.doFinal(
            plainText.getBytes());
    }
 
    // Decryption function which converts
    // the ciphertext back to the
    // orginal plaintext.
    public static String do_RSADecryption(
        byte[] cipherText,
        PublicKey publicKey)
        throws Exception
    {
        Cipher cipher
            = Cipher.getInstance(RSA);
 
        cipher.init(Cipher.DECRYPT_MODE,
                    publicKey);
        byte[] result
            = cipher.doFinal(cipherText);
 
        return new String(result);
    }
 
    // Driver code
    public static void main(String args[])
        throws Exception
    {
        KeyPair keypair
            = generateRSAKkeyPair();
 
        String plainText = "This is the PlainText "
                           + "I want to Encrypt using RSA.";
 
        byte[] cipherText
            = do_RSAEncryption(
                plainText,
                keypair.getPrivate());
 
        System.out.println(
            "The Public Key is: "
            + DatatypeConverter.printHexBinary(
                  keypair.getPublic().getEncoded()));
 
        System.out.println(
            "The Private Key is: "
            + DatatypeConverter.printHexBinary(
                  keypair.getPrivate().getEncoded()));
 
        System.out.print("The Encrypted Text is: ");
 
        System.out.println(
            DatatypeConverter.printHexBinary(
                cipherText));
 
        String decryptedText
            = do_RSADecryption(
                cipherText,
                keypair.getPublic());
 
        System.out.println(
            "The decrypted text is: "
            + decryptedText);
    }
}


输出:

使用非对称密钥进行加密和解密:在以上步骤中,我们为加密和解密创建了公共和私有密钥。现在,让我们使用RSA算法实现非对称加密。为了实现加密和解密,可以遵循以下步骤。

  • 密码类用于两种不同的加密和解密模式。由于非对称加密使用不同的密钥,因此我们使用私钥进行加密,并使用公钥进行解密。
  • 在密码上调用doFinal()方法,该密码以单部分操作对数据进行加密/解密,或者完成多部分操作并返回字节数组。
  • 最后,我们使用ENCRYPT_MODE加密后得到了密文。

下面是上述方法的实现:

Java

// Java program to perform the
// encryption and decryption
// using asymmetric key
 
package java_cryptography;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.util.Scanner;
 
import javax.crypto.Cipher;
import javax.xml.bind
    .DatatypeConverter;
 
public class Asymmetric {
 
    private static final String RSA
        = "RSA";
    private static Scanner sc;
 
    // Generating public & private keys
    // using RSA algorithm.
    public static KeyPair generateRSAKkeyPair()
        throws Exception
    {
        SecureRandom secureRandom
            = new SecureRandom();
        KeyPairGenerator keyPairGenerator
            = KeyPairGenerator.getInstance(RSA);
 
        keyPairGenerator.initialize(
            2048, secureRandom);
        return keyPairGenerator
            .generateKeyPair();
    }
 
    // Encryption function which converts
    // the plainText into a cipherText
    // using private Key.
    public static byte[] do_RSAEncryption(
        String plainText,
        PrivateKey privateKey)
        throws Exception
    {
        Cipher cipher
            = Cipher.getInstance(RSA);
 
        cipher.init(
            Cipher.ENCRYPT_MODE, privateKey);
 
        return cipher.doFinal(
            plainText.getBytes());
    }
 
    // Decryption function which converts
    // the ciphertext back to the
    // orginal plaintext.
    public static String do_RSADecryption(
        byte[] cipherText,
        PublicKey publicKey)
        throws Exception
    {
        Cipher cipher
            = Cipher.getInstance(RSA);
 
        cipher.init(Cipher.DECRYPT_MODE,
                    publicKey);
        byte[] result
            = cipher.doFinal(cipherText);
 
        return new String(result);
    }
 
    // Driver code
    public static void main(String args[])
        throws Exception
    {
        KeyPair keypair
            = generateRSAKkeyPair();
 
        String plainText = "This is the PlainText "
                           + "I want to Encrypt using RSA.";
 
        byte[] cipherText
            = do_RSAEncryption(
                plainText,
                keypair.getPrivate());
 
        System.out.println(
            "The Public Key is: "
            + DatatypeConverter.printHexBinary(
                  keypair.getPublic().getEncoded()));
 
        System.out.println(
            "The Private Key is: "
            + DatatypeConverter.printHexBinary(
                  keypair.getPrivate().getEncoded()));
 
        System.out.print("The Encrypted Text is: ");
 
        System.out.println(
            DatatypeConverter.printHexBinary(
                cipherText));
 
        String decryptedText
            = do_RSADecryption(
                cipherText,
                keypair.getPublic());
 
        System.out.println(
            "The decrypted text is: "
            + decryptedText);
    }
}

输出: