key是36字节的DES解密怎么处理? -问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文
阿里云
为了无法计算的价值
打开APP
阿里云APP内打开

key是36字节的DES解密怎么处理?

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

展开
收起
蛮大人123 2016-02-29 10:47:09 2169 0
1 条回答
写回答
取消 提交回答
  • 蛮大人123
    我说我不帅他们就打我,还说我虚伪

    用冗余字符补全到48字节。

    2019-07-17 18:50:08
    赞同 展开评论 打赏
问答分类:
相关产品:
问答排行榜
最热
最新
相关电子书
更多
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载
冬季实战营第三期:MySQL数据库进阶实战
立即下载