标签:int sys a算法 解密 技术 cipher alt VID 生成密钥
软件设计上机实验IDEA算法:
1 import javax.crypto.Cipher; 2 import javax.crypto.KeyGenerator; 3 import javax.crypto.SecretKey; 4 import org.apache.commons.codec.binary.Base64; 5 import org.bouncycastle.jce.provider.BouncyCastleProvider; 6 7 8 9 import javax.crypto.Cipher; 10 import javax.crypto.KeyGenerator; 11 import javax.crypto.SecretKey; 12 import javax.crypto.spec.SecretKeySpec; 13 import java.security.Key; 14 import java.security.Security; 15 16 17 public class IDEA implements algorithm{ 18 public void speak(String value) { 19 String data = "20173599 周博"; 20 data=value; 21 String key = getKey(); 22 System.out.println("要加密的原文:"+data); 23 System.out.println("密钥:" + key); 24 String data_en = ideaEncrypt(data, key); 25 System.out.println("密文:"+data_en); 26 String data_de = ideaDecrypt(data_en, key); 27 System.out.println("原文:"+data_de); 28 } 29 30 public static final String KEY_ALGORITHM="IDEA"; 31 32 public static final String CIPHER_ALGORITHM="IDEA/ECB/ISO10126Padding"; 33 public static byte[] initkey() throws Exception{ 34 //加入bouncyCastle支持 35 Security.addProvider(new BouncyCastleProvider()); 36 37 //实例化密钥生成器 38 KeyGenerator kg=KeyGenerator.getInstance(KEY_ALGORITHM); 39 //初始化密钥生成器,IDEA要求密钥长度为128位 40 kg.init(128); 41 //生成密钥 42 SecretKey secretKey=kg.generateKey(); 43 //获取二进制密钥编码形式 44 return secretKey.getEncoded(); 45 } 46 /** 47 * 转换密钥 48 * @param key 二进制密钥 49 * @return Key 密钥 50 * */ 51 private static Key toKey(byte[] key) throws Exception{ 52 //实例化DES密钥 53 //生成密钥 54 SecretKey secretKey=new SecretKeySpec(key,KEY_ALGORITHM); 55 return secretKey; 56 } 57 58 /** 59 * 加密数据 60 * @param data 待加密数据 61 * @param key 密钥 62 * @return byte[] 加密后的数据 63 * */ 64 private static byte[] encrypt(byte[] data,byte[] key) throws Exception{ 65 //加入bouncyCastle支持 66 Security.addProvider(new BouncyCastleProvider()); 67 //还原密钥 68 Key k=toKey(key); 69 //实例化 70 Cipher cipher=Cipher.getInstance(CIPHER_ALGORITHM); 71 //初始化,设置为加密模式 72 cipher.init(Cipher.ENCRYPT_MODE, k); 73 //执行操作 74 return cipher.doFinal(data); 75 } 76 /** 77 * 解密数据 78 * @param data 待解密数据 79 * @param key 密钥 80 * @return byte[] 解密后的数据 81 * */ 82 private static byte[] decrypt(byte[] data,byte[] key) throws Exception{ 83 //加入bouncyCastle支持 84 Security.addProvider(new BouncyCastleProvider()); 85 //还原密钥 86 Key k =toKey(key); 87 Cipher cipher=Cipher.getInstance(CIPHER_ALGORITHM); 88 //初始化,设置为解密模式 89 cipher.init(Cipher.DECRYPT_MODE, k); 90 //执行操作 91 return cipher.doFinal(data); 92 } 93 public static String getKey(){ 94 String result = null; 95 try { 96 result = Base64.encodeBase64String(initkey()); 97 } catch (Exception e) { 98 e.printStackTrace(); 99 } 100 return result; 101 } 102 public static String ideaEncrypt(String data, String key) { 103 String result = null; 104 try { 105 byte[] data_en = encrypt(data.getBytes(), Base64.decodeBase64(key)); 106 result = Base64.encodeBase64String(data_en); 107 } catch (Exception e) { 108 e.printStackTrace(); 109 } 110 return result; 111 } 112 113 public static String ideaDecrypt(String data, String key) { 114 String result = null; 115 try { 116 byte[] data_de =decrypt(Base64.decodeBase64(data), Base64.decodeBase64(key));; 117 result = new String(data_de); 118 } catch (Exception e) { 119 e.printStackTrace(); 120 } 121 return result; 122 } 123 124 125 126 127 public static void main(String[] args) { 128 // TODO Auto-generated method stub 129 System.out.println("IDEA加密算法"); 130 IDEA idea=new IDEA(); 131 try 132 { 133 idea.speak("20173599 周博"); 134 } 135 catch(Exception e) 136 { 137 System.out.println(e.getMessage()); 138 } 139 } 140 141 }
jar包下载地址:
https://pan.baidu.com/s/16YvZIXsGBRtFjQqrTAGvdQ
提取密码:ynnz
IDEA加密算法(含所需jar包(commons-codec-1.11.jar ,bcprov-jdk15on-160.jar))
标签:int sys a算法 解密 技术 cipher alt VID 生成密钥
原文地址:https://www.cnblogs.com/smartisn/p/11600823.html