DES decryption, but the key is 36 bytes. How can this problem be solved?

Source: Internet
Author: User
Tags log log
I used php to decrypt the connection with the java client. The key is 36 bytes, so I have no clue. I think there is a general method to convert these 36 bytes into 16 or 32 bytes? Thank you. The following is a java code snippet: {code ...} execution method: {code ...} class: {code... I used php to decrypt the connection with the java client. The key is 36 bytes, so I have no clue. I think there is a general method to convert these 36 bytes into 16 or 32 bytes? Thank you.

The following is a java code snippet:

Public static String DES_KEY = "36-bit key ";

Execution method:

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

Class:

    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();         }}

Reply content:

I used php to decrypt the connection with the java client. The key is 36 bytes, so I have no clue. I think there is a general method to convert these 36 bytes into 16 or 32 bytes? Thank you.

The following is a java code snippet:

Public static String DES_KEY = "36-bit key ";

Execution method:

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

Class:

    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();         }}

Use redundant characters to complete up to 48 bytes.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.