2016-04-20 4 views
0

Der vollständige Code meiner Java Verschlüsselung/Entschlüsselung Algorithmus:reagieren-native AES-Verschlüsselung passende Java Decryption Algorithmus

public class AESEncryptUtil { 

    private static AESEncryptUtil instance = new AESEncryptUtil(); 
    private String password = "123456"; 
    private Key key; 
    private Cipher cipher; 

    public AESEncryptUtil(){ 
     try { 
      KeyGenerator kgen = KeyGenerator.getInstance("AES"); 
      kgen.init(128, new SecureRandom(password.getBytes())); 
      SecretKey secretKey = kgen.generateKey(); 
      byte[] enCodeFormat = secretKey.getEncoded(); 
      key = new SecretKeySpec(enCodeFormat, "AES"); 
      cipher = Cipher.getInstance("AES"); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
    public static byte[] encrypt(String content) throws Exception { 
     byte[] byteContent = content.getBytes("utf-8"); 
     instance.cipher.init(Cipher.ENCRYPT_MODE, instance.key); 
     byte[] result = instance.cipher.doFinal(byteContent); 
     return result; 
    } 
    public static byte[] decrypt(byte[] content) throws Exception { 
     instance.cipher.init(Cipher.DECRYPT_MODE, instance.key); 
     byte[] result = instance.cipher.doFinal(content); 
     return result; 
    } 
    public static String parseByte2HexStr(byte buf[]) { 
     StringBuffer sb = new StringBuffer(); 
     for (int i = 0; i < buf.length; i++) { 
      String hex = Integer.toHexString(buf[i] & 0xFF); 
      if (hex.length() == 1) { 
       hex = '0' + hex; 
      } 
      sb.append(hex.toUpperCase()); 
     } 
     return sb.toString(); 
    } 
    public static byte[] parseHexStr2Byte(String hexStr) { 
     if (hexStr.length() < 1) 
      return null; 
     byte[] result = new byte[hexStr.length()/2]; 
     for (int i = 0; i < hexStr.length()/2; i++) { 
      int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16); 
      int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 
        16); 
      result[i] = (byte) (high * 16 + low); 
     } 
     return result; 
    } 
    public static String getNonce() { 
     String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; 
     Random random = new Random(); 
     StringBuffer sb = new StringBuffer(); 
     for (int i = 0; i < 16; i++) { 
      int number = random.nextInt(base.length()); 
      sb.append(base.charAt(number)); 
     } 
     return sb.toString(); 
    } 
    public static void main(String[] args) throws Exception { 
     String content = "test"; 
     System.out.println("content: " + content); 
     byte[] encryptResult = encrypt(content); 
     String encryptResultStr = parseByte2HexStr(encryptResult); 
     System.out.println("encryptResultStr: " + encryptResultStr); 
     byte[] decryptFrom = parseHexStr2Byte(encryptResultStr); 
     byte[] decryptResult = decrypt(decryptFrom); 
     System.out.println("decryptResult: " + new String(decryptResult)); 
    } 
} 

ich viele Male und viele Möglichkeiten versucht haben, den Java-Algorithmus entsprechen, aber das Ergebnis ist immer anders. Welches Modul soll ich verwenden? Kann mir jemand helfen, damit umzugehen? Danke vielmals !

Antwort

2

ich den richtigen Weg gefunden zwei Algorithmus entsprechen:

Java Teil:

public static String encrypt() throws Exception { 
     try { 
      String data = "123456"; 
      String key = "1234567812345678"; 
      String iv = "1234567812345678"; 

      Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding"); 
      int blockSize = cipher.getBlockSize(); 

      byte[] dataBytes = data.getBytes(); 
      int plaintextLength = dataBytes.length; 
      if (plaintextLength % blockSize != 0) { 
       plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize)); 
      } 

      byte[] plaintext = new byte[plaintextLength]; 
      System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length); 

      SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES"); 
      IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes()); 

      cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec); 
      byte[] encrypted = cipher.doFinal(plaintext); 

      return new sun.misc.BASE64Encoder().encode(encrypted); 

     } catch (Exception e) { 
      e.printStackTrace(); 
      return null; 
     } 
    } 

    public static String desEncrypt() throws Exception { 
     String encrypted = encrypt() ; 
     try 
     { 
      String data = encrypted ; 
      String key = "1234567812345678"; 
      String iv = "1234567812345678"; 

      byte[] encrypted1 = new BASE64Decoder().decodeBuffer(data); 

      Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding"); 
      SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES"); 
      IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes()); 

      cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec); 

      byte[] original = cipher.doFinal(encrypted1); 
      String originalString = new String(original); 
      return originalString; 
     } 
     catch (Exception e) { 
      e.printStackTrace(); 
      return null; 
     } 
    } 

Reagieren nativer Teil:

pre-Codierung: npm install crypto-js

import CryptoJS from 'crypto-js' ; 
encryptFun() { 
    var data = "123456"; 
    var key = CryptoJS.enc.Latin1.parse('1234567812345678'); 
    var iv = CryptoJS.enc.Latin1.parse('1234567812345678'); 
    var encrypted = CryptoJS.AES.encrypt(
     data, 
     key, 
     {iv:iv,mode:CryptoJS.mode.CBC,padding:CryptoJS.pad.ZeroPadding 
    }); 
    console.log('encrypted: ' + encrypted) ; 
    var decrypted = CryptoJS.AES.decrypt(encrypted,key,{iv:iv,padding:CryptoJS.pad.ZeroPadding}); 
    console.log('decrypted: '+decrypted.toString(CryptoJS.enc.Utf8)); 
    } 

das Ergebnis:

encrypted: aK7+UX24ttBgfTnAndz9aQ== 
decrypted: 123456 

Ich hoffe, dass mein Code jemand :)

helfen würde