Bouncycastle JCE Practice (3)

xiaoxiao2021-03-17  194

Generation of the key

Symmetric key generation

Symmetry encryption adopts symmetric password coding technology, it is characterized by file encryption and decryption using the same key, that is, the encryption key can also be used as a decryption key. This method is called a symmetrical encryption algorithm in cryptography, and the symmetric encryption algorithm is simple and fast, the key is shorter, and the difficulty is difficult. In addition to the data encryption standard (DES), another symmetric key encryption system system is an international data encryption algorithm (IDEA), it is better than DES's encryption, and it is not so high for computer functionality. The IDEA Encryption Standard is used by the PGP (PRETTY Good Privacy) system.

// First of all, IMPORT JAVAX.CRYPTO. *;

SecretKey Key = NULL;

???? Try

{

// Specify an algorithm, here is DES; if you want to use the Blowfish algorithm, use GetInstance ("BLOWFISH")

// Bouncycastle basically supports all general standard algorithms

KeyGenerator keygen = keygenerator.getInstance ("des");

/ / Specify the length of the key, the higher the length, the greater the encryption strength

Keygen.init (56);

// Generate a key

Key = keygen.generateKey ();

// Conformarize the output file, the directory here is dynamic, constructed according to user names

???? ObjectOutputStream KeyFile = New ObjectOutputStream (New FileoutputStream

???????????? ("C: // security file //" misclass.username "/-symmetric // symmetry key //yhb.des"));

???? kyfile.writeObject (key);

???? keyfile.close ();

????}

???? catch (NosuchalgorithMexception E5)

{

// generateKey () Excellence

???? system.out.print ("No Such algorithm");

???? system.exit (0);

????}

???? carat (IOException E4)

???? {

???? system.out.print ("Error Whenrate The des Key);

???? system.exit (0);

}

Asymmetric key generation

In 1976, American scholars DIME and HENMAN proposed a new key exchange protocol to resolve information public delivery and key management issues, allowing information to exchange information in unsafe media, safely reaching a consistent key, This is the "public key system". This method is also called "asymmetric encryption algorithm" relative to the "symmetric encryption algorithm".

Different from the symmetric encryption algorithm, the asymmetric encryption algorithm requires two keys: public key (

Publickey) and private key (private key). The public key is paired with the private key. If the data is encrypted with the public key, only the corresponding private key can be decrypted; if the data is encrypted with the private key, then only the corresponding public key can only be used Decrypt. This algorithm is called a non-symmetrical encryption algorithm because two different keys are used.

// Key pair

??? keypair keys = NULL;

??? Try

{

// Specify algorithm

Keypairgenerator kpg = keypairgenerator.getInstance ("RSA");

// Specify length

??? kpg.initialize (1024); keys = kpg.genkeypair ();

// public key

BYTE [] key1 = keys.getpublic (). getENCODED ();

// private key

??? byte [] key2 = keys.getPrivate (). getENCODED ();

?

??? // Construct a public key file and write to the public key

FileOutputStream Keyfile1 = New FileOutputStream

?????? ("C: // security file //" misclass.username "/-asymmetric // my private key //yhb.public");

???? keyfile1.write (key1);

???? keyfile1.close ();

??? // Construct a private key file and write private key

??? keyfile1 = new fileoutputstream

???? ("C: // security file //" misclass.username "/-asymmetric // my private key //yhb.private");

??? kyfile1.write (key2);

??? kyfile1.close ();

???}

??? catch (NosuchalgorithMexception E8)

{

// Algorithm is abnormal

???? system.out.print ("No Such algorithm");

???? system.exit (0);

????}

???? catch (IOException E9)

???? {

???? system.out.print ("Error Whenrate The RSA Key);

???? system.exit (0);

????}

?

Author's name Hongsoft, Research Area: 1) Research Based on Workflow 2) Java-based information security technology. Welcome to discuss all aspects of Java related issues

HONGBOSoftware@163.com

转载请注明原文地址:https://www.9cbs.com/read-129622.html

New Post(0)