Des\3des\base64 in Java and C + +

Source: Internet
Author: User
Tags decrypt

First, let's look at the code in Java that decrypts the string:

Secret key

Private String key = "123456789012345678901234";

Decryption process, the first decryption with Base64, and then 3DES for the second decryption, to get the plaintext

public string Decryptthreedesecb (String src) throws Exception {
Desedekeyspec DKs = new Desedekeyspec (key.getbytes (CharSet));
Secretkeyfactory keyfactory = secretkeyfactory.getinstance ("Desede");
Secretkey SecureKey = Keyfactory.generatesecret (DKS);
Cipher Cipher = cipher.getinstance ("Desede");
Cipher.init (Cipher.decrypt_mode, SecureKey);
byte[] Retbyte = cipher.dofinal (Base64.decodebase64 (Src.getbytes (CharSet)));
return new String (Retbyte,charset);
}

Encryption process, first with 3DES encryption, and then use BASE64 for the second encryption output, to get ciphertext
Public String decryptthreedesecbbyte (byte[] encodebyte) throws Exception {
Desedekeyspec DKs = new Desedekeyspec (key.getbytes (CharSet));
Secretkeyfactory keyfactory = secretkeyfactory.getinstance ("Desede");
Secretkey SecureKey = Keyfactory.generatesecret (DKS);
Cipher Cipher = cipher.getinstance ("Desede");
Cipher.init (Cipher.decrypt_mode, SecureKey);
byte[] Retbyte = cipher.dofinal (Base64.decodebase64 (Src.getbytes (CharSet)));
return new String (Retbyte,charset);
}

It is easy to implement this functionality by introducing libraries in Java, and in C + + there is really no standard library to call, so it is still necessary to understand des-related knowledge before implementing the code.

Let's take a look at the difference between simple des/3des/aes:

Des

If mode is encrypted, the data is encrypted with key, and the password form (64 bits) is generated as the output result of DES.

If mode is decrypted, use key to decrypt the data in the form of the password, and revert to the plaintext (64-bit) as the output result of DES.

At both ends of the communication network, both parties agree to the key, in the source point of communication with key to the core data des encryption, and then in the form of a password in the public communications network (such as telephone network) to the end of the communication network, the data arrives at the destination, with the same key to decrypt the password data, It reproduces the core data in the form of plaintext. This ensures the security and reliability of core data (such as pins, MACS, etc.) that are transmitted in the public communication network.

By periodically switching to a new key at the source and destination of the communication network, it is possible to further improve the confidentiality of the data, which is now a popular practice in the financial Transactions network.

3DES

3DES is a mode of the DES encryption algorithm that uses 3 64-bit keys to encrypt data three times . Data Encryption Standard (DES) is a long-standing encryption standard in the United States that uses symmetric key cryptography.

3DES (i.e. Triple DES) is the des -to- AES Transition Encryption Algorithm (1999, NIST designates 3-des as the encryption standard for transitions ), which is a more secure variant of DES. It uses DES as the basic module to design a packet encryption algorithm by combining grouping method.

Set the Ek () and DK () on behalf of the DES algorithm encryption and decryption process, K for the DES algorithm used by the key, p for the plaintext, C for the secret table, so,

The 3DES encryption process is: C=ek3 (Dk2 (EK1 (P)))

The 3DES decryption process is: P=dk1 ((EK2 (DK3 (C)))

K1, K2, K3 determine the security of the algorithm, if the three keys are different , essentially equivalent to a 168-bit key encryption. Over the years, it has been relatively safe against brute force attacks. If the data is less secure,K1 can be equal to K3. In this case, the valid length of the key is 112 bits .

Aes

AES (Advanced encryption): High Encryption Standard, the next generation of encryption algorithm standard, fast, high security level.

Encrypted with AES in October 2000, NIST (National Institute of Standards and Technology) announced a new key encryption standard chosen from 15 candidate algorithms. Rijndael is selected as the future AES. Rijndael was created in the second half of 1999 by researchers Joan Daemen and Vincent Rijmen. AES is increasingly becoming the real standard for encrypting various forms of electronic data.

The new Advanced Encryption Standard (AES) specification was developed by the American Institute of Standards and Technology (NIST) on May 26, 2002.

The AES algorithm is based on permutation and permutation operations. permutations are rearranging the data, replacing one unit of data with another.

AES uses several different methods to perform permutation and permutation operations. AES is an iterative , symmetric key grouping of passwords that can be used with the192 and 256-bit keys , and is grouped with 128 bits (16 bytes) Encrypt and decrypt data.

Unlike public key encryption, which uses a key pair, the symmetric key password encrypts and decrypts the data using the same key. The number of bits of encrypted data returned by a block cipher is the same as the input data. Iterative encryption uses a looping structure that repeats the substitution and substitution of input data in the loop.

Having learned the relevant knowledge, we then write the corresponding C + + code. I have here a well-written and Java implementation of the same C + + code, for your reference. The address is:

http://download.csdn.net/detail/liulong371212/9391330

Des\3des\base64 in Java and C + +

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.