Chapter 9 symmetric encryption algorithms-IDEA and encryption algorithms-idea

Source: Internet
Author: User

Chapter 9 symmetric encryption algorithms-IDEA and encryption algorithms-idea

Note: In this section, refer to Chapter 2nd "primary encryption algorithms-symmetric encryption algorithms" from the art of Java encryption and decryption (version 7th"

9.1 IDEA

Features:

  • Replace DES before AES
  • Extremely high security
  • Commonly Used in email encryption algorithms

9.2. Implementation Method

  • Bouncy Castle (BC, working mode: Only ECB, key length: 128 bits)

9.2.1. BC-based IDEA algorithm

1 package com. util. idea; 2 3 import java. io. unsupportedEncodingException; 4 import java. security. invalidAlgorithmParameterException; 5 import java. security. invalidKeyException; 6 import java. security. key; 7 import java. security. noSuchAlgorithmException; 8 import java. security. noSuchProviderException; 9 import java. security. security; 10 import java. security. spec. invalidKeySpecException; 11 12 import Javax. crypto. badPaddingException; 13 import javax. crypto. cipher; 14 import javax. crypto. illegalBlockSizeException; 15 import javax. crypto. keyGenerator; 16 import javax. crypto. noSuchPaddingException; 17 import javax. crypto. secretKey; 18 import javax. crypto. spec. secretKeySpec; 19 20 import org. apache. commons. codec. binary. base64; 21 import org. bouncycastle. jce. provider. bouncyCastleProvider; 22 23 /* * 24 * BC-based IDEA algorithm, working mode only ECB 25 */26 public class IDEABC {27 private static final String ENCODING = "UTF-8 "; 28 private static final String KEY_ALGORITHM = "IDEA"; // key generation algorithm 29 private static final String CIPHER_ALGORITHM = "IDEA/ECB/PKCS5Padding"; // encryption/decryption algorithm format: algorithm/working mode/filling mode 30/** 31 * generate key 32 */33 public static byte [] getKey () throws NoSuchAlgorithmException {34 Security. addProvider (new BouncyCast LeProvider (); // used in BC, except 35 KeyGenerator keyGenerator = KeyGenerator for JDK. getInstance (KEY_ALGORITHM); 36 keyGenerator. init (128); // initialization key length, 128 37 SecretKey key = keyGenerator. generateKey (); // generate the key 38 return key. getEncoded (); 39} 40 41/** 42 * restore Key: convert a binary byte array to a Java object 43 */44 public static Key toKey (byte [] keyByte) {45 return new SecretKeySpec (keyByte, KEY_ALGORITHM); 46} 47 48/** 49 * IDEA encryption 50 * @ p Aram data with encrypted data 51 * @ param keyByte key 52 */53 public static byte [] encrypt (String data, byte [] keyByte) throws NoSuchAlgorithmException, 54 NoSuchProviderException, 55 NoSuchPaddingException, 56 InvalidKeyException, 57 IllegalBlockSizeException, 58 BadPaddingException, 59 UnsupportedEncodingException {60 Key key = toKey (keyByte); // restore Key 61 Cipher cipher = Cipher. getInstance (CIPHER_ALGORITHM, "BC" ); // Uses 62 cipher in BC. init (Cipher. ENCRYPT_MODE, key); // sets the encryption mode and initializes key 63 return cipher. doFinal (data. getBytes (ENCODING); 64} 65 66/** 67 * IDEA encryption, convert to a hexadecimal String or Base64 encoded String 68 */69 public static String encryptIDEAHex (String data, byte [] keyByte) throws NoSuchAlgorithmException, 70 NoSuchProviderException, 71 NoSuchPaddingException, 72 InvalidKeyException, 73 IllegalBlockSizeException, 74 BadPaddingExc Eption, 75 UnsupportedEncodingException {76 byte [] encodedByte = encrypt (data, keyByte); 77 // return new String (Hex. encode (encodedByte); // use BC 78 // return new String (org. apache. commons. codec. binary. hex. encodeHexString (encodedByte); // use CC 79 return Base64.encodeBase64String (encodedByte ); // use Base64 encoding of CC 80} 81 82/** 83 * IDEA to decrypt 84 * @ param data the data to be decrypted is a byte array 85 * @ param keyByte key 86 */87 public Static byte [] decrypt (byte [] data, byte [] keyByte) throws NoSuchAlgorithmException, 88 NoSuchProviderException, 89 NoSuchPaddingException, 90 InvalidKeyException, 91 IllegalBlockSizeException, 92 BadPaddingException {93 Key key = toKey (keyByte); // restore the Key 94 Cipher cipher = Cipher. getInstance (CIPHER_ALGORITHM, "BC"); // 95 cipher for BC. init (Cipher. DECRYPT_MODE, key); 96 return cipher. doFinal (data); 97} 98 99/** 100 * IDEA decryption 101 * @ param data the data to be decrypted is String 102 * @ param keyByte key 103 */104 public static byte [] decrypt (String data, byte [] keyByte) throws failed, 105 NoSuchProviderException, 106 NoSuchPaddingException, 107 InvalidKeyException, 108 IllegalBlockSizeException, 109 BadPaddingException {110 Key key = toKey (keyByte ); // restore the key 111 Cipher cipher = Cipher. getInstance (CIPHER_ALGORITHM, "BC"); // uses 112 cipher in BC. init (Cipher. DECRYPT_MODE, key); 113 return cipher. doFinal (Base64.decodeBase64 (data); // note that data cannot be directly used. the getByte () method is converted to a byte array. Otherwise, an exception 114} 115 116/** 117 * test 118 */119 public static void main (String [] args) throws NoSuchAlgorithmException will be thrown, 120 InvalidKeyException, 121 InvalidKeySpecException, 122 NoSuchPaddingException, 123 IllegalBlockSizeException, 124 BadPaddingException, 125 UnsupportedEncodingException, 126 NoSuchProviderException, 127 InvalidAlgorithmParameterException {128 String data = "It is my dream to find a good girl to be a wife! "; 129/************** test encrypt (), decrypt () * *************/130 System. out. println ("original -->" + data); 131 byte [] keyByte = IDEABC. getKey (); 132 System. out. println ("key -->" + Base64.encodeBase64String (keyByte); // Save the binary key with base64 encryption, this is also the method used in actual use 133 byte [] encodedByte = IDEABC. encrypt (data, keyByte); 134 System. out. println ("encrypted -->" + encodedByte); 135 byte [] encodedByte2 = IDEABC. encrypt (data, keyByte); 136 System. out. println ("encrypted -->" + encodedByte2); 137 byte [] decodedByte = IDEABC. decrypt (encodedByte, keyByte); 138 System. out. println ("decrypted -->" + decodedByte); 139 for (int I = 0; I <encodedByte. length; I ++) {140 System. out. println (encodedByte [I] = encodedByte2 [I]); 141} 142/************* test encryptIDEAHex (), decrypt () * *************/143 System. out. println ("original -->" + data); 144 byte [] keyByte3 = IDEABC. getKey (); 145 System. out. println ("key -->" + Base64.encodeBase64String (keyByte3); // you can use base64 to encrypt and save the binary key. This is also the actual method used. 146 String encodedStr = IDEABC. encryptIDEAHex (data, keyByte3); 147 System. out. println ("encrypted -->" + encodedStr); 148 String encodedByte4 = IDEABC. encryptIDEAHex (data, keyByte3); 149 System. out. println ("encrypted -->" + encodedByte4); 150 byte [] decodedByte3 = IDEABC. decrypt (Base64.decodeBase64 (encodedStr), keyByte3); 151 System. out. println ("decrypt Byte [] -->" + decodedByte3); 152 byte [] decodedByte4 = IDEABC. decrypt (encodedStr, keyByte3); 153 System. out. println ("decrypted String -->" + decodedByte4); 154} 155}View Code

Note:

  • This is basically the same as the AES algorithm code based on BC.

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.