thieumao

AESCBCNopading Java

Jun 25th, 2015
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.60 KB | None | 0 0
  1. import javax.crypto.Cipher;
  2. import javax.crypto.spec.IvParameterSpec;
  3. import javax.crypto.spec.SecretKeySpec;
  4. import static tivitop2.Lai.hexArray;
  5.  
  6.  
  7. public class AESCBCNopading {
  8.     public static final String KEY = "d7f8g9hk0dj2s712";
  9.     public static final String IV = "f7h9a5d0gkvi7v6v";
  10.    
  11.     public static void main(String[] args) throws Exception {
  12.         // d74fb44c840ebe083d59908e836a2e5c7d9963ec24c5df8ac3b9713e43e75dc92ebb0fd9e3ad9d9249d8395178debd9853c94c8ac573665925cfbab4c55d7efb7aa68725eea10fb4b85a3cc31e51704c
  13.         // http://hlscache.fptplay.net.vn/livez/vtv1_500.stream/playlist.m3u8
  14.        
  15.         // f37249baa85d917abaf0a3a6552ec1812876f8f97a89a05f26aad35a4970df85
  16.         // http://222.255.27.92/vtv1.m3u8
  17.  
  18.         String dataMaHoa = "http://tivi.crackgame.net/youtubefaker/Hai-co-tien-365.mp4      ";
  19.         System.out.println("dataMaHoa:" + dataMaHoa.length());
  20.         String dataGiaiMa = "7bffbdb21c836b1f1a3d3f697ba37d679647c47b0c6a538ef3a484bc63c68e999747105fb472b88f46557065afe57a4f0ceb11605de772bc462d644bda331430";
  21.                String mao = "7bffbdb21c836b1f1a3d3f697ba37d679647c47bc6a538ef3a484bc63c68e999747105fb472b88f46557065afe57a4fceb11605de772bc462d644bda331430";
  22.         System.out.println("Ma hoa: " + encrypt(dataMaHoa, KEY, IV));
  23.         System.out.println("Giai ma: " + desEncrypt(dataGiaiMa, KEY, IV) + "mao");
  24.         String tinh = "7bffbdb21c836b1f1a3d3f697ba37d679647c47b";
  25.         System.out.println(tinh.length());
  26.     }
  27.    
  28.     public static String encrypt(String data, String key, String iv) throws Exception {
  29.         try {
  30. //            String data = "Test String";
  31. //            String key = "1234567812345678";
  32. //            String iv = "1234567812345678";
  33.             Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
  34.             int blockSize = cipher.getBlockSize();
  35.             byte[] dataBytes = data.getBytes();
  36.             int plaintextLength = dataBytes.length;
  37.             if (plaintextLength % blockSize != 0) {
  38.                 plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
  39.             }
  40.             byte[] plaintext = new byte[plaintextLength];
  41.             System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
  42.              
  43.             SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
  44.             IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());
  45.             cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
  46.             byte[] encrypted = cipher.doFinal(plaintext);
  47.             return bytesToHex(encrypted);
  48.                     //new sun.misc.BASE64Encoder().encode(encrypted);
  49.         } catch (Exception e) {
  50.             e.printStackTrace();
  51.             return null;
  52.         }
  53.     }
  54.     public static String desEncrypt(String data, String key, String iv) throws Exception {
  55.         try
  56.         {
  57. //            String data = "2fbwW9+8vPId2/foafZq6Q==";
  58. //            String key = "1234567812345678";
  59. //            String iv = "1234567812345678";
  60.              
  61.             byte[] encrypted1 = hexToBytes(data);
  62.                     //new sun.misc.BASE64Decoder().decodeBuffer(data);
  63.              
  64.             Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
  65.             SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
  66.             IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());
  67.              
  68.             cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
  69.             byte[] original = cipher.doFinal(encrypted1);
  70.             String originalString = new String(original);
  71.             return originalString;
  72.         }
  73.         catch (Exception e) {
  74.             e.printStackTrace();
  75.             return null;
  76.         }
  77.     }
  78.    
  79.     public static String bytesToHex(byte[] bytes) {
  80.         char[] hexChars = new char[bytes.length * 2];
  81.         for (int j = 0; j < bytes.length; j++) {
  82.             int v = bytes[j] & 0xFF;
  83.             hexChars[j * 2] = hexArray[v >>> 4];
  84.             hexChars[j * 2 + 1] = hexArray[v & 0x0F];
  85.         }
  86.         return new String(hexChars);
  87.     }
  88.    
  89.     private static byte[] hexToBytes(String hex) {
  90.         String HEXINDEX = "0123456789abcdef";
  91.         int l = hex.length() / 2;
  92.         byte data[] = new byte[l];
  93.         int j = 0;
  94.  
  95.         for (int i = 0; i < l; i++) {
  96.             char c = hex.charAt(j++);
  97.             int n, b;
  98.  
  99.             n = HEXINDEX.indexOf(c);
  100.             b = (n & 0xf) << 4;
  101.             c = hex.charAt(j++);
  102.             n = HEXINDEX.indexOf(c);
  103.             b += (n & 0xf);
  104.             data[i] = (byte) b;
  105.         }
  106.  
  107.         return data;
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment