DES解密,但是key是36位元組的,怎麼處理?

來源:互聯網
上載者:User
和java端進行對接的,我用php解密,key是36位元組的,所以沒有頭緒了。我想著是不是有把這36位元組轉化成16或者32位元組的通用方法?先謝謝大家了。

下面是java的程式碼片段:

public static String DES_KEY="36位的key";

執行方法:

DesUtil.decrypt(rs.getString(i+1),Global.DES_KEY );

類:

    public class DesUtil {             private final static String DES = "DES";             private final static String PADDING="DES/ECB/PKCS5Padding";             private final static Log log= LogFactory.getLog(DesUtil.class);             public static byte[] encrypt(byte[] src, byte[] key)throws Exception {                     SecureRandom sr = new SecureRandom();                    DESKeySpec dks = new DESKeySpec(key);                     SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);                     SecretKey securekey = keyFactory.generateSecret(dks);                     Cipher cipher = Cipher.getInstance(PADDING);                     cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);                     return cipher.doFinal(src);                  }                public static byte[] decrypt(byte[] src, byte[] key)throws Exception {                     SecureRandom sr = new SecureRandom();                     DESKeySpec dks = new DESKeySpec(key);                    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);                     SecretKey securekey = keyFactory.generateSecret(dks);                     Cipher cipher = Cipher.getInstance(PADDING);                     cipher.init(Cipher.DECRYPT_MODE, securekey, sr);                   return cipher.doFinal(src);                  }              public final static String decrypt(String data,String key){                  try {                           return new String(decrypt(Base64.decode(data.getBytes()),                      key.getBytes()));                 }catch(Exception e) {                            log.error("Exception -->DesUtil");                       e.printStackTrace();                 }                 return null;               }               public final static String decrypt(String data,String key, String charset){                  try {                           return new String(decrypt(Base64.decode(data.getBytes()),                      key.getBytes()), charset);                 }catch(Exception e) {                          log.error("Exception -->DesUtil ");                          e.printStackTrace();                 }                 return null;               }               public final static String encrypt(String code,String key){                 try {                   return Base64.encodeBytes(encrypt(code.getBytes("utf-8"), key.getBytes()));                 }catch(Exception e) {                          log.error("Exception -->DesUtil");                          e.printStackTrace();                 }                 return null;               }/*    public static void main(String[] args) {         /**         * Convert byte[] string         *          * @param src         * @return         */         public static String bytesTo16HexString(byte[] src) {                   StringBuilder stringBuilder = new StringBuilder("");                   if (src == null || src.length <= 0) {                            return "";                   }                   for (int i = 0; i < src.length; i++) {                            int v = src[i] & 0xFF;                            String hv = Integer.toHexString(v);                            if (hv.length() < 2) {                                     stringBuilder.append(0);                            }                            stringBuilder.append(hv);                   }                   return stringBuilder.toString();         }}

回複內容:

和java端進行對接的,我用php解密,key是36位元組的,所以沒有頭緒了。我想著是不是有把這36位元組轉化成16或者32位元組的通用方法?先謝謝大家了。

下面是java的程式碼片段:

public static String DES_KEY="36位的key";

執行方法:

DesUtil.decrypt(rs.getString(i+1),Global.DES_KEY );

類:

    public class DesUtil {             private final static String DES = "DES";             private final static String PADDING="DES/ECB/PKCS5Padding";             private final static Log log= LogFactory.getLog(DesUtil.class);             public static byte[] encrypt(byte[] src, byte[] key)throws Exception {                     SecureRandom sr = new SecureRandom();                    DESKeySpec dks = new DESKeySpec(key);                     SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);                     SecretKey securekey = keyFactory.generateSecret(dks);                     Cipher cipher = Cipher.getInstance(PADDING);                     cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);                     return cipher.doFinal(src);                  }                public static byte[] decrypt(byte[] src, byte[] key)throws Exception {                     SecureRandom sr = new SecureRandom();                     DESKeySpec dks = new DESKeySpec(key);                    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);                     SecretKey securekey = keyFactory.generateSecret(dks);                     Cipher cipher = Cipher.getInstance(PADDING);                     cipher.init(Cipher.DECRYPT_MODE, securekey, sr);                   return cipher.doFinal(src);                  }              public final static String decrypt(String data,String key){                  try {                           return new String(decrypt(Base64.decode(data.getBytes()),                      key.getBytes()));                 }catch(Exception e) {                            log.error("Exception -->DesUtil");                       e.printStackTrace();                 }                 return null;               }               public final static String decrypt(String data,String key, String charset){                  try {                           return new String(decrypt(Base64.decode(data.getBytes()),                      key.getBytes()), charset);                 }catch(Exception e) {                          log.error("Exception -->DesUtil ");                          e.printStackTrace();                 }                 return null;               }               public final static String encrypt(String code,String key){                 try {                   return Base64.encodeBytes(encrypt(code.getBytes("utf-8"), key.getBytes()));                 }catch(Exception e) {                          log.error("Exception -->DesUtil");                          e.printStackTrace();                 }                 return null;               }/*    public static void main(String[] args) {         /**         * Convert byte[] string         *          * @param src         * @return         */         public static String bytesTo16HexString(byte[] src) {                   StringBuilder stringBuilder = new StringBuilder("");                   if (src == null || src.length <= 0) {                            return "";                   }                   for (int i = 0; i < src.length; i++) {                            int v = src[i] & 0xFF;                            String hv = Integer.toHexString(v);                            if (hv.length() < 2) {                                     stringBuilder.append(0);                            }                            stringBuilder.append(hv);                   }                   return stringBuilder.toString();         }}

用冗餘字元補全到48位元組。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.