簡體   English   中英

如何使用AES算法進行加密

[英]How to do encryption using AES algorithm

我給了一個.key(abc.key)文件。 使用上述.key文件,可以用Java完成加密和解密。 在這里,我需要使用C#.net實現相同的加密和解密功能。

請給我有關如何破解的想法。 我是這些加密技術的新手。

他們在Java中設置的一些參數如下。 希望它不會給什么主意

    String keyFile="abc.key";
    String keyAlgorithm= "AES";
    String cipherTransformation="AES/CBC/PKCS5Padding";
    String needtoEncStr = "Password1";

任何幫助將不勝感激。

更新 :

@daveBM:我使用了bouncycastle,但是得到了不同的結果(與Java輸出不同)。 下面是我的完整代碼

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Generators;
using Org.BouncyCastle.Crypto.Modes;
using Org.BouncyCastle.Crypto.Paddings;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.Utilities.Encoders;

public class BCEngine
{
    private readonly Encoding _encoding;
    private readonly IBlockCipher _blockCipher;
    private PaddedBufferedBlockCipher _cipher;
    private IBlockCipherPadding _padding;

    public BCEngine(IBlockCipher blockCipher, Encoding encoding)
    {
        _blockCipher = blockCipher;
        _encoding = encoding;
    }

    public void SetPadding(IBlockCipherPadding padding)
    {
        if (padding != null)
            _padding = padding;
    }

    public string Encrypt(string plain, string key)
    {
        byte[] result = BouncyCastleCrypto(true, _encoding.GetBytes(plain), key);
        return Convert.ToBase64String(result);
    }

    public string Decrypt(string cipher, string key)
    {
        byte[] result = BouncyCastleCrypto(false, Convert.FromBase64String(cipher), key);
        return _encoding.GetString(result);
    }

    /// <summary>
    ///
    /// </summary>
    /// <param name="forEncrypt"></param>
    /// <param name="input"></param>
    /// <param name="key"></param>
    /// <returns></returns>
    /// <exception cref="CryptoException"></exception>
    private byte[] BouncyCastleCrypto(bool forEncrypt, byte[] input, string key)
    {
        try
        {
            _cipher = _padding == null ? new PaddedBufferedBlockCipher(_blockCipher) : new PaddedBufferedBlockCipher(_blockCipher, _padding);
            byte[] keyByte = _encoding.GetBytes(key);
            _cipher.Init(forEncrypt, new KeyParameter(keyByte));
            return _cipher.DoFinal(input);
        }
        catch (Org.BouncyCastle.Crypto.CryptoException ex)
        {
            throw new CryptoException(ex.Message);
        }
    }
}



static void Main(string[] args)
        {
            Encoding _encoding;
            IBlockCipherPadding _padding;
            string key = "abc.key";

            Stream inStr = null;
            inStr = File.OpenRead(key);

            Stream stream = inStr;

            byte[] bytes = new byte[stream.Length];

            stream.Position = 0;

            stream.Read(bytes, 0, (int)stream.Length);

            string data1 = Encoding.UTF8.GetString(bytes); // this is your string.


            _encoding = Encoding.ASCII;
            Pkcs7Padding pkcs = new Pkcs7Padding();
            _padding = pkcs;  

            BCEngine bcEngine = new BCEngine(new AesEngine(), _encoding);
            bcEngine.SetPadding(_padding);
            string data=bcEngine.Encrypt("AbcDefg12$", data1);

        }

當我的預期輸出是“ 3df36eb77ccfc05e264a6212c2db5380”時,我得到此輸出“ S2jjvVJVKfGodPfMuI4v + g ==“ ......請讓我知道出了什么問題。

您可以使用某人在其他相關問題中發布的這段代碼。

在C#中加密和解密字符串

您需要做的是使用Encrypt(string plainText,string passPhrase)方法 ,其中plainText是您想要加密的任何內容,而passPhrase是您的.key文件的內容。

順便說一句,僅提及您擁有一個.key文件就足夠了。 我們不需要知道內容:) ..

希望能有所幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM