首頁 > 後端開發 > php教程 > DES解密,但是key是36字节的,怎么处理?

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

WBOY
發布: 2016-06-06 20:40:47
原創
1449 人瀏覽過

和java端进行对接的,我用php解密,key是36字节的,所以没有头绪了。我想着是不是有把这36字节转化成16或者32字节的通用方法?先谢谢大家了。

下面是java的代码片段:

<code>public static String DES_KEY="36位的key";
</code>
登入後複製
登入後複製

执行方法:

<code>DesUtil.decrypt(rs.getString(i+1),Global.DES_KEY );
</code>
登入後複製
登入後複製

类:

<code>    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 </code>
登入後複製
登入後複製

回复内容:

和java端进行对接的,我用php解密,key是36字节的,所以没有头绪了。我想着是不是有把这36字节转化成16或者32字节的通用方法?先谢谢大家了。

下面是java的代码片段:

<code>public static String DES_KEY="36位的key";
</code>
登入後複製
登入後複製

执行方法:

<code>DesUtil.decrypt(rs.getString(i+1),Global.DES_KEY );
</code>
登入後複製
登入後複製

类:

<code>    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 </code>
登入後複製
登入後複製

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

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板