2014-03-29 2 views
0

안녕하세요. 문자열을 암호화하고 해독해야합니다. 이 내가java : Blowfish 암호화 해독 잘못된 채우기 예외

/** Utility method to Encrpyt a plain text string using blowfish algorithm. This method is synchronised between threads. 
    * Base64 encoding is used to encode and decode byte array. 
    * <p>NOTE: use the same key for Encryption and Decryption</p> 
    * 
    * @param plainText Plain text String 
    * @param key  Secret key (If null default will be used) 
    * @return String URL safe encrypted String 
    * @throws Exception 
    */ 

    public synchronized static String blowfishEncryption(String plainText, String key) throws Exception { 
     if(DEBUG) { 
      logger.log(Level.INFO,"blowfishEncryption() method ===== passed normal text: { "+plainText+" passed key: "+key+" }"); 
     } 
     if(key==null) { 
      logger.log(Level.INFO,"passed key is null using default key"); 
      key=BLOWFISH_SECRET; 
     } 
     ByteArrayOutputStream os= new ByteArrayOutputStream(1024); 
     byte[] keyByte = hexToBytes(key); 
     SecretKeySpec skeySpec = new SecretKeySpec(keyByte, "Blowfish"); 
     Cipher ecipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding"); 
     ecipher.init(Cipher.ENCRYPT_MODE, skeySpec); 

     byte[] stringByte=plainText.getBytes("US-ASCII"); 

     byte[] econtent=ecipher.doFinal(stringByte); 

     String out= new String(Base64.encodeBase64(econtent), "US-ASCII"); 

     return out; 
    } 

    /** Utility method for Blowfish Decryption. This method is synchronised between threads. 
    * <p>NOTE: use the same key for Encryption and Decryption</p> 
    * 
    * @param cipherContent  Cipher Text Byte array to be decrypted 
    * @param key    Key used for Decryption. NOTE: use same key for encryption and decryption 
    * @return String   Plain text String 
    * @throws Exception 
    */ 

    public synchronized static String blowfishDecryption(String cipherText, String key) throws Exception { 
     // String ciphertext is base 64 endoded string This method returns plain text string 

     if(DEBUG) { 
      logger.log(Level.INFO,"blowfishEncryption() method ===== passed key: "+key+" }"); 
     } 
     if(key==null) { 
      logger.log(Level.INFO,"passed key is null using default key"); 
      key=BLOWFISH_SECRET; 
     } 

     byte[] myKeyByte = hexToBytes(key); 

     SecretKeySpec skeySpec = new SecretKeySpec(myKeyByte, "Blowfish"); 

     Cipher ecipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding"); 

     ecipher.init(Cipher.DECRYPT_MODE, skeySpec); 

     byte[] cipherContent=cipherText.getBytes("US-ASCII"); 


     byte[] dContent=ecipher.doFinal(cipherContent); 

     String out=new String(Base64.encodeBase64(dContent), "US-ASCII"); 

     return out; 
    } 

을 사용하고있는 방법이다하지만 난 잘못 어떤 나쁜 패딩 예외를 얻고있다. 또한 결과를 String에 넣기를 원합니다. 이 문자열을 내 서버에 전달하면 서버는 여기서 사용한 것과 같은 방법으로 해독합니다.

Blowfish encryption 
Encrypt value=J7mbZ4mal7R9ckRBodqqyti70XD3+Bci 
[java] Blow fish decryption==================== 
[java] Encrypt value=J7mbZ4mal7R9ckRBodqqyti70XD3+Bci 

답변

1

넌 대신 blowfishEncryption

byte[] cipherContent=cipherText.getBytes("US-ASCII"); 

를 사용하는 바이트의 배열로 다시 64 비트로 디코딩 상기 blowfishEncryption 방법으로 암호화 된 데이터를 64 비트로 부호화

와 그 광고를 교체되고
byte[] cipherContent=Base64.decodeBase64(cipherText); 
// fix the method name if needed, as it is not clear what Base64 class you are using 
+0

암호문은 문자열이고 Base64 클래스는 매개 변수로 바이트 배열이 필요합니다. byte [] cipherContent = Base64.decodeBase64 (cipherText.getBytes ("US-ASCII"); – user123

+0

) * base64를 디코드하는 메소드는 String을 받아 들여 바이트 []를 반환해야합니다. –

+0

Base64 클래스는 다음과 같습니다. 이것을 다시 지원하지 않을 것입니다. 아파치 커먼즈 코덱 라이브러리 – user123