2016-08-19 1 views
0

언어 간의 차이를 일으키는 원인을 파악할 수 없습니다. 자바에서 나는이 :Java에서 Node.js로 Blowfish ECB를 사용하여 암호 해독 코드를 변환 할 수 없습니다.

byte[] buf = Base64.getDecoder().decode("AutMdzthDvPlE+UnhcHa2h4UZGPdme7t"); 
System.out.println(buf.length); 
String key = "" + 2270457870L; 
byte[] keyBytes = key.getBytes("UTF8"); 
System.out.println(keyBytes.length); 

Cipher cipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding"); 
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(keyBytes, "Blowfish")); 

byte[] newBytes = cipher.doFinal(buf); 
System.out.println(newBytes.length); 
System.out.println(Arrays.toString(newBytes)); 

나는이 점을 설정 노드에서 (http://ideone.com/0dXuJL에서 실행 가능한 온라인) 다음

: (https://tonicdev.com/paulbgd/57b66c8ea0630d1400081ad0에서 실행 가능한 온라인)

const buf = Buffer.from("AutMdzthDvPlE+UnhcHa2h4UZGPdme7t"); 
console.log(buf.length); 
const keyBytes = Buffer.from('2270457870', 'utf8'); 
console.log(keyBytes.length); 
const decipher = require('crypto').createDecipher('bf-ecb', keyBytes); 

const buffers = []; 
buffers.push(decipher.update(buf)); 
buffers.push(decipher.final()); 

const newBytes = Buffer.concat(buffers); 
console.log(newBytes.length); 
console.log(newBytes); 

를 출력한다 오류 : error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt

답변

2

여기에 몇 가지 문제가 있습니다 :

  1. buf에 대한 문자열 인코딩이 없습니다. 기본적으로 utf8이 사용되지만 문자열은 실제로 base64로 인코딩됩니다. 이 대신에 사용을 해결하기 위해 :

    const buf = Buffer.from("AutMdzthDvPlE+UnhcHa2h4UZGPdme7t", "base64"); 
    
  2. createDecipher()에 전달 된 두 번째 인수는 암호, 하지입니다. 차이점은 createDecipher()은 (MD5로) 암호 인수를 해시하여 키를 생성한다는 것입니다. 당신은 이미 열쇠를 가지고 있기 때문에 당신이 원하는 것은 createDecipheriv()이고, 그것은 열쇠와 IV를 기대합니다. ECB 모드는 IV를 사용하지 않으므로 IV 인수는 길이가 0 인 버퍼 일 수 있습니다. 그래서 그 대신 이것을 사용 : 자바에서 바이트의 출력과 일치 할 경우

    const decipher = require('crypto').createDecipheriv('bf-ecb', keyBytes, Buffer.alloc(0)); 
    

마지막으로,이 같은 뭔가 console.log(newBytes)을 대체 할 수

for (var i = 0; i < newBytes.length; ++i) 
    process.stdout.write(newBytes.readInt8(i) + ' '); 
console.log(); 
+0

은 64 기수 난 그냥 실수 그것을 복사 할 때 만들었지 만 createDecipher가 암호를 기다리고 있다는 것을 몰랐습니다! 엄청 고마워. – PaulBGD