Pogledaj jedan post
Old 26.04.2012., 20:03   #11
Korištenje API-ja za enkripciju, dekripciju nije komplicirano, evo primjer koda u javi koji kriptira AES algoritmom:

Kod:
    public byte[] encrypt(byte[] dataToEncrypt, byte[] key, byte[] initializationVector) throws Exception
    {
    	if (dataToEncrypt == null) return null;
    	
    	byte[] encryptedBytes = null;
    	try
    	{
    	    SecretKey secretKey = new SecretKeySpec(key, _algorithm);
    	    Cipher cipher = Cipher.getInstance(_cipher);	        	        	    
	    cipher.init(Cipher.ENCRYPT_MODE, secretKey, new IvParameterSpec(initializationVector));
	    encryptedBytes = cipher.doFinal(dataToEncrypt);			
    	}
        catch (Exception ex)
        {
            throw ex;
        }	        	            	
        return encryptedBytes;
    }
i .NET-u:
Kod:
        public byte[] Encrypt(byte[] dataToEncrypt, byte[] key, byte[] initializationVector)
        {
            if (dataToEncrypt == null) return null;

            Aes aes = null;

            byte[] encryptedData = null;
            try
            {
                aes = new AesCryptoServiceProvider();
                aes.Mode = CipherMode.CBC;
                aes.Padding = PaddingMode.PKCS7;
                ICryptoTransform encryptor = aes.CreateEncryptor(key, initializationVector);
                MemoryStream memoryStream = null;
                CryptoStream cryptoStream = null;

                memoryStream = new MemoryStream();
                cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write);
                cryptoStream.Write(dataToEncrypt, 0, dataToEncrypt.Length);
                cryptoStream.FlushFinalBlock();
                encryptedData = memoryStream.ToArray();
            }
            catch
            {
                try
                {
                    aes = new AesManaged();
                    aes.Mode = CipherMode.CBC;
                    aes.Padding = PaddingMode.PKCS7;
                    ICryptoTransform encryptor = aes.CreateEncryptor(key, initializationVector);
                    MemoryStream memoryStream = null;
                    CryptoStream cryptoStream = null;

                    memoryStream = new MemoryStream();
                    cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write);
                    cryptoStream.Write(dataToEncrypt, 0, dataToEncrypt.Length);
                    cryptoStream.FlushFinalBlock();
                    encryptedData = memoryStream.ToArray();
                }
                catch (Exception ex)
                {
                    encryptedData = null;
                    throw new Exception(ex.Message);
                }
            }
            return encryptedData;
        }
GemsBond is offline  
Odgovori s citatom