![]() |
I {adore} you the {maximum} {hideous} side
|
In various systems, we will inevitably use the encryption. Encrypted into the ciphertext can be restored and not restored the two ciphertext, the ciphertext can be used to restore more common,air force one 25th, more and more that DES algorithm, and can not be restored is the MD5 of the more. This article provides two methods to achieve the corresponding java. Some function reference networks
1package com.resoft.util; 2 3import java.io.UnsupportedEncodingException; 4import java.security.InvalidKeyException; 5import java.security.MessageDigest; 6import java.security.NoSuchAlgorithmException; 7import java.security.SecureRandom; 8import java.security.spec.InvalidKeySpecException; 9 10import javax.crypto.BadPaddingException; 11import javax.crypto.Cipher; 12import javax.crypto. IllegalBlockSizeException; 13import javax.crypto.NoSuchPaddingException; 14import javax.crypto.SecretKey; 15import javax.crypto.SecretKeyFactory; 16import javax.crypto.spec.DESKeySpec; 17 18import sun.misc.BASE64Encoder; 19 20 / ** *//** 21 * security encryption type 22 * 23 * @ date 2007-10-31 24 * @ author yayagepei 25 * 26 * / 27public class SecurityEncode { 28 29 / ** *//** 30 * MD5 encrypt a string 31 * 32 * @ param str 33 ; * To encrypt a string 34 * @ return encrypted string 35 * @ throws NoSuchAlgorithmException 36 * @ throws UnsupportedEncodingException 37 * @ author yayagepei 38 * @ date 2007-10-31 39 * @ comment on the Value reflected in two aspects: its current value,cheap air force ones, its future value 40 * / 41 public static String encoderByMd5 (String str) 42 ; throws NoSuchAlgorithmException, UnsupportedEncodingException { 43 / / determine the method 44 ; MessageDigest md5 = MessageDigest.getInstance (\/ / encrypted string 47 String newstr = base64en.encode (md5.digest (str.getBytes (\br> 50 51 / ** *//** 52 * DES encryption and decryption 53 * 54 * @ param plainText 55 * to deal with the byte [] 56 * @ param key 57 * ; key 58 * @ param mode 59 * mode 60 * @ return 61 * @ throws InvalidKeyException 62 ; * @ throws InvalidKeySpecException 63 * @ throws NoSuchAlgorithmException 64 * @ throws NoSuchPaddingException 65 * @ throws BadPaddingException 66 * @ throws IllegalBlockSizeException 67 * @ throws UnsupportedEncodingException 68 * @ author yayagepei 69 * @ date 2008-10-8 70 * / 71 private static byte [] coderByDES (byte [] plainText, String key, int mode) 72 throws InvalidKeyException, InvalidKeySpecException,nike air force one, 73 ; NoSuchAlgorithmException, NoSuchPaddingException, 74 BadPaddingException, IllegalBlockSizeException, 75 UnsupportedEncodingException { 76 SecureRandom sr = new SecureRandom (); 77 byte [] resultKey = makeKey (key); 78 DESKeySpec desSpec = new DESKeySpec (resultKey); 79 SecretKey secretKey = SecretKeyFactory.getInstance (\;. generateSecret (desSpec); 81 Cipher cipher = Cipher.getInstance (\83 return cipher.doFinal (plainText); 84} 85 86 ; / ** *//** 87 * production of 8-bit key 88 * 89 * @ param key 90 * string 91 ; * @ return 92 * @ throws UnsupportedEncodingException 93 * @ author yayagepei 94 * @ date 2008-10-8 95 * / 96 private static byte [] makeKey (String key) 97 throws UnsupportedEncodingException { 98 byte [] keyByte = new byte [8]; 99 byte [] keyResult = key.getBytes (\for (int i = 0; i <keyResult.length & & i <keyByte.length; i + +) { 101 keyByte = keyResult; 102} 103 ; return keyByte; 104} 105 106 / ** *//** 107 * DES encryption 108 * 109 * @ param plainText 110 * expressly 111 * @ param key 112 * Key 113 * @ return 114 * @ author yayagepei 115 * @ date 2008-10-8 116 * / 117 public static String encoderByDES (String plainText, String key) { 118 try { 119 byte [] result = coderByDES (plainText.getBytes (\Cipher.ENCRYPT_MODE); 121 return byteArr2HexStr (result); 122} catch (Exception ex) { 123 ; ex.printStackTrace (); 124 return \;} 126} 127 128 / ** * / / * * 129 * DES decryption 130 * 131 * @ param secretText 132 ; * ciphertext 133 * @ param key 134 * Key 135 * @ return 136 * @ author yayagepei 137 * @ date 2008-10-8 138 * / 139 ; public static String decoderByDES (String secretText, String key) { 140 try { 141 ; byte [] result = coderByDES (hexStr2ByteArr (secretText), key, 142 Cipher.DECRYPT_MODE); 143 return new String (result,air force one, \; ex.printStackTrace (); 146 return \; / ** *//** 151 * the byte array is converted to that string of 16 hexadecimal values, such as: byte [] {8,air force one shoes,18 } into: 0813, and the public static byte [] 152 * hexStr2ByteArr (String strIn) mutually reversible conversion process 153 * 154 * @ param arrB 155 * needs to be converted The byte array 156 * @ return the converted string 157 * / 158 private static String byteArr2HexStr (byte [] arrB) { 159 int iLen = arrB.length; 160 / / Each byte can be expressed with two characters, so the length of the string is twice the length of the array 161 StringBuffer sb = new StringBuffer ( iLen * 2); 162 for (int i = 0; i <iLen; i + +) { 163 int intTmp = arrB; 164 / / the negative into a positive 165 while (intTmp <0) { 166 intTmp = intTmp + 256; 167 ;} 168 / / less than the number required in front of 0F fill 0 169 if (intTmp <16) { 170 ; sb.append (\} 172 sb.append (Integer.toString (intTmp, 16)); 173} 174 return sb.toString (); 175 ;} 176 177 / ** *//** 178 * to that string of 16 hexadecimal values converted to byte array,air force ones, and the public static String byteArr2HexStr (byte [] arrB) 179 * each other reversible conversion process 180 * 181 ; * @ param strIn 182 * to be converted 183 * @ return string the converted byte array 184 * @ throws NumberFormatException 186 * / 187 private static byte [] hexStr2ByteArr (String strIn) 188 throws NumberFormatException { 189 byte [] arrB = strIn.getBytes (); 190 int iLen = arrB.length; 191 / / two characters represent one byte, so the length of the string byte array length is divided by 2 192 byte [] arrOut = new byte [iLen / 2]; 193 for ( int i = 0; i <iLen; i = i + 2) { 194 String strTmp = new String ( arrB, i, 2); 195 arrOut = (byte) Integer.parseInt (strTmp, 16); 196} 197 return arrOut; 198 ;} 199} |
All times are GMT. The time now is 01:09 AM. |
Powered by vBulletin Version 3.6.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Free Advertising Forums | Free Advertising Message Boards | Post Free Ads Forum