Block Cipher _ Counter (CTR) Mode _ principle and Java implementation

Source: Internet
Author: User
Tags decrypt

First, the principle:

CTR Mode is a stream cipher that generates a key stream by encrypting successive accumulated counters, and in CTR mode, each packet corresponds to a successive cumulative counter, and the key stream is generated by encrypting the counter. The final cipher packet is obtained by XOR the bit sequence encrypted by the counter and the plaintext packet.

Second, schematic diagram:

Three, the advantages of CRT mode:

1, the hardware efficiency is high, compared with three kinds of link mode, CTR can encrypt and decrypt in parallel.

2, the software efficiency is high, can make full use of its parallel characteristics for parallel computing

3, because the encryption and decryption process is not Reimingwen and ciphertext, so can be pre-processing to improve efficiency

4, can randomly access a clear text or ciphertext group for partial encryption and decryption

Iv. realization of:

The previous use of DES is the cipher class that uses Java. And then found that Ctr mode does not write, there is no person on the internet to paste code. On the StackOverflow, it is found that the implementation of the CRT is required to use the IV (initial value) to ensure that the initial value of each encryption counter is different, if you can guarantee that each encryption key is different or do not use IV, so it becomes a secret, security is high, But in practice is not practical. But the CRT principle does not need IV.

Encrypt function:

     Public byte[] Descrypto (byte[] datasource, String password) {//plaintext and encryption keys        Try{deskeyspec Deskey=NewDeskeyspec (Password.getbytes ()); //Create a key factory and use it to convert the password (user encryption key) to the 16 keys in DES16 wheel encryptionSecretkeyfactory keyfactory = secretkeyfactory.getinstance ("DES"); Secretkey SecureKey=Keyfactory.generatesecret (Deskey); //The cipher object actually completes the cryptographic operationCipher Cipher = cipher.getinstance ("des/ctr/pkcs5padding"); //Initialize the Cipher object with a keyCipher.init (Cipher.encrypt_mode, SecureKey,NewIvparameterspec (Deskey.getkey ())); //now, get the data and encrypt//formally perform cryptographic operations            returncipher.dofinal (DataSource); }Catch(Throwable e) {e.printstacktrace (); }          return NULL; }

Decrypt function:

 Public byte[] Decrypt (byte[] src, String password)throwsException {//ciphertext and decryption key//Create a Deskeyspec objectDeskeyspec Deskey =NewDeskeyspec (Password.getbytes ()); //Create a key factorySecretkeyfactory keyfactory = secretkeyfactory.getinstance ("DES"); //convert Deskeyspec objects to Secretkey objectsSecretkey SecureKey =Keyfactory.generatesecret (Deskey); //The cipher object actually completes the decryption operationCipher Cipher = cipher.getinstance ("des/ctr/pkcs5padding"); //Initialize the Cipher object with a keyCipher.init (Cipher.decrypt_mode, SecureKey,NewIvparameterspec (Password.getbytes ())); //actually start the decryption operation        returncipher.dofinal (SRC); }

Block Cipher _ Counter (CTR) Mode _ principle and Java implementation

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.