android----Java DES密碼編譯演算法工具類

來源:互聯網
上載者:User

標籤:

 1 package entity; 2  3 import javax.crypto.Cipher; 4 import javax.crypto.SecretKey; 5 import javax.crypto.SecretKeyFactory; 6 import javax.crypto.spec.DESKeySpec; 7 import javax.crypto.spec.IvParameterSpec; 8  9 public class Des {10     private byte[] desKey;   11       12     13     //解密資料   14     public static String decrypt(String message,String key) throws Exception {   15             16             byte[] bytesrc =convertHexString(message);      17             Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");       18             DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));      19             SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");      20             SecretKey secretKey = keyFactory.generateSecret(desKeySpec);      21             IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));   22                    23             cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);         24              25             byte[] retByte = cipher.doFinal(bytesrc);        26             return new String(retByte);    27     }   28   29     public static byte[] encrypt(String message, String key)   30             throws Exception {   31         Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");   32   33         DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));   34   35         SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");   36         SecretKey secretKey = keyFactory.generateSecret(desKeySpec);   37         IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));   38         cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);   39   40         return cipher.doFinal(message.getBytes("UTF-8"));   41     }   42        43     public static byte[] convertHexString(String ss)    44     {    45     byte digest[] = new byte[ss.length() / 2];    46     for(int i = 0; i < digest.length; i++)    47     {    48     String byteString = ss.substring(2 * i, 2 * i + 2);    49     int byteValue = Integer.parseInt(byteString, 16);    50     digest[i] = (byte)byteValue;    51     }    52   53     return digest;    54     }    55     56     public static String toHexString(byte b[]) {   57         StringBuffer hexString = new StringBuffer();   58         for (int i = 0; i < b.length; i++) {   59             String plainText = Integer.toHexString(0xff & b[i]);   60             if (plainText.length() < 2)   61                 plainText = "0" + plainText;   62             hexString.append(plainText);   63         }   64            65         return hexString.toString();   66     }   67   68   69     public static void main(String[] args) throws Exception {   70         String key = "12345678";   71         String value="test1234";   72         String jiami=java.net.URLEncoder.encode(value, "utf-8").toLowerCase();   73            74         System.out.println("加密資料:"+jiami);   75         String a=toHexString(encrypt(jiami, key)).toUpperCase();   76         System.out.println("加密後的資料為:"+a);  77         78         79         String b=java.net.URLDecoder.decode(decrypt(a,key), "utf-8") ;   80         System.out.println("解密後的資料:"+b);   81   82     }   83   84        85     86 }

 

android----Java DES密碼編譯演算法工具類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.