2016-09-01 3 views
-1

デコードしようとするとエラーが発生します。 これはデコーダである:javax.crypto.BadPaddingException:最終ブロックが正しく埋め込まれていないと仮定DES

public class Encrypter { 

    Cipher ecipher; 
    Cipher dcipher; 

    SecretKeySpec key = new SecretKeySpec("missyou1".getBytes(), "DES"); 

    public DesEncrypter() throws Exception { 
     ecipher = Cipher.getInstance("DES"); 
     dcipher = Cipher.getInstance("DES"); 
     ecipher.init(Cipher.ENCRYPT_MODE, key); 
     dcipher.init(Cipher.DECRYPT_MODE, key); 
    } 

    public String encrypt(String str) throws Exception { 
     byte[] utf8 = str.getBytes("UTF8"); 
     byte[] enc = ecipher.doFinal(utf8); 
     return new sun.misc.BASE64Encoder().encode(enc); 
    } 

    public String decrypt(String str) throws Exception { 
     byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str); 

     byte[] asd = new byte[(dec.length/8+1)*8]; 
     for(int i = 0; i < dec.length; i++){ 
      asd[i] = dec[i]; 
     } 
     byte[] utf8 = dcipher.doFinal(asd); 

     return new String(utf8, "UTF8"); 
    } 
} 

これは私がエラーを取得するコードです:行endpoint.append(encrypter.decrypt(s));

private static void appenda(Encrypter encrypt) { 
     if (Files.exists(Paths.get(filePath), new LinkOption[] { LinkOption.NOFOLLOW_LINKS })) { 
      Stream<String> stringStream = null; 
      try { 
       stringStream = Files.lines(Paths.get(filePath), StandardCharsets.UTF_8); 
      } catch (IOException e) { 
       LOGGER.error("Failed ", e); 
      } 

      if (stringStream.toString() != null) { 
       stringStream.forEach(s -> { 
        try { 
         System.out.println(encrypt.decrypt(s)); 
         endpoint.delete(0, endpoint.length()); 
         endpoint.append(encrypt.decrypt(s)); 
        } catch (Exception e) { 
         LOGGER.error("Failed to decrypt", e); 
        } 
       }); 
      } 
     } 
    } 

任意のアイデアなぜですか?

+0

DESは新しい作業では使用しないでください。DESは安全ではなく、AESを超越しています。 – zaph

答えて

0

データが間違ったサイズの配列に不必要にコピーされているため、機能しません。

アレイのコピーを削除し、バイトを直接復号化するだけです。

public String decrypt(String str) throws Exception { 
    byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str); 
    byte[] utf8 = dcipher.doFinal(dec); 
    return new String(utf8, "UTF8"); 
} 
関連する問題