2016-10-13 190 views
0

所以我有我的C#服務器程序和一個工作的C#客戶端,現在我想用於Linux和Mac客戶端的Java。 因此,當應用程序啓動時,它將向服務器請求當前的加密/解密密鑰,該密鑰將用於通信加密。 但因爲我很新到Java,我真的不知道如何得到它的工作,代碼是非常不同的,我爲我的Java客戶端下面的代碼:C#服務器與java/c#客戶端,aes256加密和解密

public class CryptoClass 
{ 
    static String IV = "AAAAAAAAAAAAAAAA"; 

    public static byte[] encrypt(String plainText, String encryptionKey) throws Exception { 
    Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding", "SunJCE"); 
    SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES"); 
    cipher.init(Cipher.ENCRYPT_MODE, key,new IvParameterSpec(IV.getBytes("UTF-8"))); 
    return cipher.doFinal(plainText.getBytes("UTF-8")); 
    } 

    public static String decrypt(byte[] cipherText, String encryptionKey) throws Exception{ 
    Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding", "SunJCE"); 
    SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES"); 
    cipher.init(Cipher.DECRYPT_MODE, key,new IvParameterSpec(IV.getBytes("UTF-8"))); 
    return new String(cipher.doFinal(cipherText),"UTF-8"); 
    } 
} 

,這是代碼對於C#客戶端

public byte[] AES_Decrypt(byte[] bytesToBeDecrypted, byte[] passwordBytes) 
{ 
    byte[] decryptedBytes = null; 
    byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; 
    using (MemoryStream ms = new MemoryStream()) 
    { 
     using (RijndaelManaged AES = new RijndaelManaged()) 
     { 
      AES.KeySize = 256; 
      AES.BlockSize = 128; 
      var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000); 
      AES.Key = key.GetBytes(AES.KeySize/8); 
      AES.IV = key.GetBytes(AES.BlockSize/8); 
      AES.Mode = CipherMode.CFB; 
      AES.Padding = PaddingMode.PKCS7; 
      using (var cs = new CryptoStream(ms, AES.CreateDecryptor(), CryptoStreamMode.Write)) 
      { 
       cs.Write(bytesToBeDecrypted, 0, bytesToBeDecrypted.Length); 
       cs.Close(); 
      } 
      decryptedBytes = ms.ToArray(); 
     } 
    } 
    return decryptedBytes; 
} 
public byte[] AES_Encrypt(byte[] bytesToBeEncrypted, byte[] passwordBytes) 
{ 
    byte[] encryptedBytes = null; 
    byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; 
    using (MemoryStream ms = new MemoryStream()) 
    { 
     using (RijndaelManaged AES = new RijndaelManaged()) 
     { 
      AES.KeySize = 256; 
      AES.BlockSize = 128; 
      var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000); 
      AES.Key = key.GetBytes(AES.KeySize/8); 
      AES.IV = key.GetBytes(AES.BlockSize/8); 
      AES.Mode = CipherMode.CFB; 
      AES.Padding = PaddingMode.PKCS7; 
      using (var cs = new CryptoStream(ms, AES.CreateEncryptor(), CryptoStreamMode.Write)) 
      { 
       cs.Write(bytesToBeEncrypted, 0, bytesToBeEncrypted.Length); 
       cs.Close(); 
      } 
      encryptedBytes = ms.ToArray(); 
     } 
    } 
    return encryptedBytes; 
} 

在此先感謝

回答

0

我固定它,我改變了我的Java代碼如下:

public class CryptoClass { 
private static final String AES_CBC_PKCS5PADDING = "AES/CBC/PKCS5PADDING"; 
private static final int KEY_SIZE = 256; 
public String DecryptText(String input) throws NoSuchAlgorithmException 
    { 
     input = input.replace("\0", ""); 
     byte[] bytesToBeDecrypted = input.getBytes(); 
     byte[] passwordBytes = Config.ServerKey.getBytes(); 
     MessageDigest md = MessageDigest.getInstance("SHA-256"); 
     passwordBytes = md.digest(passwordBytes); 
     byte[] bytesDecrypted = null; 
    try { 
     bytesDecrypted = AES_Decrypt(bytesToBeDecrypted, passwordBytes); 
    } catch (IllegalBlockSizeException ex) { 
     Logger.getLogger(CryptoClass.class.getName()).log(Level.SEVERE, null, ex); 
    } catch (BadPaddingException ex) { 
     Logger.getLogger(CryptoClass.class.getName()).log(Level.SEVERE, null, ex); 
    } catch (InvalidKeyException ex) { 
     Logger.getLogger(CryptoClass.class.getName()).log(Level.SEVERE, null, ex); 
    } catch (InvalidAlgorithmParameterException ex) { 
     Logger.getLogger(CryptoClass.class.getName()).log(Level.SEVERE, null, ex); 
    } catch (NoSuchPaddingException ex) { 
     Logger.getLogger(CryptoClass.class.getName()).log(Level.SEVERE, null, ex); 
    } 
     return new String(bytesDecrypted); 
    } 
    public String EncryptText(String input) throws NoSuchAlgorithmException 
    { 
     byte[] bytesToBeEncrypted = input.getBytes(); 
     byte[] passwordBytes = Config.ServerKey.getBytes(); 
     MessageDigest md = MessageDigest.getInstance("SHA-256"); 
     passwordBytes = md.digest(passwordBytes); 
     byte[] bytesEncrypted = null; 
    try { 
     bytesEncrypted = AES_Encrypt(bytesToBeEncrypted, passwordBytes); 
    } catch (NoSuchPaddingException ex) { 
     Logger.getLogger(CryptoClass.class.getName()).log(Level.SEVERE, null, ex); 
    } catch (InvalidAlgorithmParameterException ex) { 
     Logger.getLogger(CryptoClass.class.getName()).log(Level.SEVERE, null, ex); 
    } catch (InvalidKeyException ex) { 
     Logger.getLogger(CryptoClass.class.getName()).log(Level.SEVERE, null, ex); 
    } catch (IllegalBlockSizeException ex) { 
     Logger.getLogger(CryptoClass.class.getName()).log(Level.SEVERE, null, ex); 
    } catch (BadPaddingException ex) { 
     Logger.getLogger(CryptoClass.class.getName()).log(Level.SEVERE, null, ex); 
    } 
     return Base64.getEncoder().encodeToString(bytesEncrypted); 
    } 
    public byte[] AES_Decrypt(byte[] bytesToBeDecrypted, byte[] passwordBytes) throws IllegalBlockSizeException, BadPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchPaddingException 
    { 
     byte[] decryptedBytes = null; 
     byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; 
     final Cipher cipher = Cipher.getInstance(AES_CBC_PKCS5PADDING); 
     final byte[] keyData = Arrays.copyOf(passwordBytes, KEY_SIZE 
       /Byte.SIZE); 
     final byte[] ivBytes = Arrays.copyOf(keyData, cipher.getBlockSize()); 
     cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(keyData, "AES"), 
       new IvParameterSpec(ivBytes)); 
     decryptedBytes = cipher.doFinal(bytesToBeDecrypted); 
     return decryptedBytes; 
    } 
    public byte[] AES_Encrypt(byte[] bytesToBeEncrypted, byte[] passwordBytes) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException 
    { 
     byte[] encryptedBytes = null; 
     byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; 
     final Cipher cipher = Cipher.getInstance(AES_CBC_PKCS5PADDING); 
     final byte[] keyData = Arrays.copyOf(passwordBytes, KEY_SIZE 
       /Byte.SIZE); 
     final byte[] ivBytes = Arrays.copyOf(keyData, cipher.getBlockSize()); 
     cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(keyData, "AES"), 
       new IvParameterSpec(ivBytes)); 
     encryptedBytes = cipher.doFinal(bytesToBeEncrypted); 
     return encryptedBytes; 
    } 
相關問題