2013-12-09 1 views
3

이 암호화 클래스는 J2ME 응용 프로그램에서 사용하고 있습니다. 내 J2ME 응용 프로그램은 모든 Nokia 장치에서 정상적으로 작동합니다. 응용 프로그램이 중국 MIw200 전화에서 작동하지 않습니다. 아마도이 암호는 해당 전화에서 지원되지 않을 수 있습니까? 다른 솔루션이나 다른 방법으로 암호화하고 해독 할 수 있습니까?j2me 중국 전화에서 AESEncoder 클래스가 지원되지 않습니다.

도와주세요. 많은 감사드립니다.

내 코드는 다음과 같습니다 :

import java.security.NoSuchAlgorithmException; 
import javax.crypto.Cipher; 
import javax.crypto.NoSuchPaddingException; 
import javax.crypto.spec.SecretKeySpec; 


public class AESEncoder { 

    private SecretKeySpec keyspec; 
    private Cipher cipher; 
    private String secretkey; 

    public AESEncoder(String secretkey) { 
     this.secretkey = secretkey; 
     keyspec = new SecretKeySpec(secretkey.getBytes(), 0, 16, "AES"); 

//  keyspec=new SecretKeySpec(key, offset, len, secretkey); 

     try { 
      cipher = Cipher.getInstance("AES/ECB/NoPadding"); 
     } catch (NoSuchAlgorithmException e) { 
      e.printStackTrace(); 
     } catch (NoSuchPaddingException e) { 
      e.printStackTrace(); 
     } 
    } 
    public byte[] encrypt(String text) throws Exception { 
     if (text == null || text.length() == 0) { 
      throw new Exception("Empty string"); 
     } 

     int encrypted = 0; 

     byte[] bytenc = null;//new byte[32]; 
     byte[] input = null; 

     try { 
      cipher.init(Cipher.ENCRYPT_MODE, keyspec); 
//   byte empty[]=padString(text).getBytes(); 
//   encrypted = cipher.doFinal(padString(text).getBytes()); 
//   encrypted=cipher.doFinal(padString(text).getBytes(), 0, 0, padString(text).getBytes(), 0); 

      input = padString(text).getBytes(); 
      bytenc = new byte[input.length]; 
      encrypted = cipher.doFinal(input, 0, input.length, bytenc, 0); 

      String str = new String(bytenc, 0, encrypted); 
//   encrypted=cipher.update(padString(text).getBytes(), 0, 0, 0, 0); 
//   System.out.println("Encrypted is:>>" + str); 
//   bytenc=hexToBytes(String.valueOf(encrypted)); 
     } catch (Exception e) { 
      throw new Exception("[encrypt] " + e.getMessage()); 
     } 
     return bytenc; 
    } 


    public String encrypt_hsm(String text) throws Exception { 
     if (text == null || text.length() == 0) { 
      throw new Exception("Empty string"); 
     } 
     String base64=null; 
     int encrypted = 0; 

     byte[] bytenc = null;//new byte[32]; 
     byte[] input = null; 

     try { 
      cipher.init(Cipher.ENCRYPT_MODE, keyspec); 
//   byte empty[]=padString(text).getBytes(); 
//   encrypted = cipher.doFinal(padString(text).getBytes()); 
//   encrypted=cipher.doFinal(padString(text).getBytes(), 0, 0, padString(text).getBytes(), 0); 

      input = padString(text).getBytes(); 
      bytenc = new byte[input.length]; 
      encrypted = cipher.doFinal(input, 0, input.length, bytenc, 0); 

      String str = new String(bytenc, 0, encrypted); 

      base64 = Base64.encode(bytenc); 

//   encrypted=cipher.update(padString(text).getBytes(), 0, 0, 0, 0); 
//   System.out.println("Encrypted is:>>" + str); 
//   bytenc=hexToBytes(String.valueOf(encrypted)); 


     } catch (Exception e) { 
      throw new Exception("[encrypt] " + e.getMessage()); 
     } 
     return base64; 
    } 

    public byte[] decrypt(String code) throws Exception { 
     if (code == null || code.length() == 0) { 
      throw new Exception("Empty string"); 
     } 
     int decrypted = 0; 

     byte[] bytedec = null; 
     byte[] input = null; 

     try { 
      cipher.init(Cipher.DECRYPT_MODE, keyspec); 

//   input=hexToBytes(code); 
      input = Base64ToBytes(code); 
      bytedec = new byte[input.length]; 
      decrypted = cipher.doFinal(input, 0, input.length, bytedec, 0); 

      String str = new String(bytedec, 0, decrypted); 
//   System.out.println("Decrypted is:>>" + str); 

     } catch (Exception e) { 
      throw new Exception("[decrypt] " + e.getMessage()); 
     } 
     return bytedec; 
    } 

    public static String bytesToHex(byte[] bsData) { 
     int nDataLen = bsData.length; 
     String sHex = ""; 
     for (int nIter = 0; nIter < nDataLen; nIter++) { 
      int nValue = (bsData[nIter] + 256) % 256; 
      int nIndex1 = nValue >> 4; 
      sHex += Integer.toHexString(nIndex1); 
      int nIndex2 = nValue & 0x0f; 
      sHex += Integer.toHexString(nIndex2); 
     } 
     return sHex; 
    } 

    public static byte[] hexToBytes(String str) { 
     if (str == null) { 
      return null; 
     } else if (str.length() < 2) { 
      return null; 
     } else { 
      int len = str.length()/2; 
      byte[] buffer = new byte[len]; 
      for (int i = 0; i < len; i++) { 
       buffer[i] = (byte) Integer.parseInt(str.substring(i * 2, i * 2 + 2), 16); 
      } 
      return buffer; 
     } 
    } 

    private static String padString(String source) { 
     char paddingChar = ' '; 
     int size = 32; 
     int x = source.length() % size; 
     int padLength = size - x; 

     for (int i = 0; i < padLength; i++) { 
      source += paddingChar; 
     } 

//  System.out.println("====>Pad String:" + source); 
     return source; 
    } 

    public void startApp() { 
    } 

    public void pauseApp() { 
    } 

    public void destroyApp(boolean unconditional) { 
    } 

    private byte[] Base64ToBytes(String code) { 
     code = code.replace('-', '+'); 
     code = code.replace('_', '/'); 
     code = code.replace(',', '='); 
     System.out.println("Final Base 64:"+code); 

     byte[] aesString = Base64.decode(code); 
//  System.out.println("Base64 after decoding:"+new String(aesString)); 
     return aesString; 
    } 
} 
+0

오류에 대해 더 자세히 설명해주십시오. 우리가 공유 할 수있는 스택 추적이 있습니까? –

+0

@Duncan 실제로 J2ME App은 모든 장치에서 완벽하게 작동합니다. 하지만 미시건 차이나 폰에서는 NT가 있었고 MI 폰에서 지원되지 않는 AES 암호화 클래스에 문제가 있음을 알게되었습니다. 그래서 나는 AES 암호화를위한 다른 솔루션을 원한다. –

+2

좋아요,하지만 당신에게 묻고 있어요 * 중국 전화 *에 어떤 오류가 있습니까? "작동하지 않는다"고 말하면 문제를 해결하기에 충분한 정보를 얻을 수 없습니다. –

답변

0

스택 추적을 게시하시기 바랍니다. 그렇지 않으면 우리 모두가 추측하고 있습니다.

AES는 required cipher이며 언젠가 v4와 v7 사이입니다. 시간을 들여 MIw200에서이 암호를 사용할 수 없는지 확인하십시오.

Someone getting "AES not available"은 Mac에서 몇 가지 제안 사항이 있습니다.

AES를 강제 실행하는 대신 시스템을 3DES 또는 DES로 폴백 할 수있게하십시오. Here's an example that tests encryption with each algorithm.

BouncyCastle API를 사용해보세요. Here is a how-to guide.