Posts

Showing posts with the label Encryption

An Example Of Encrypting An Xml File In Java Using Bouncy Castle

Answer : What type of encryption do you want to perform? Password-based (PBE), symmetric, asymmetric? Its all in how you configure the Cipher. You shouldn't have to use any BouncyCastle specific APIs, just the algorithms it provides. Here is an example that uses the BouncyCastle PBE cipher to encrypt a String: import java.security.SecureRandom; import java.security.Security; import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.PBEKeySpec; import org.bouncycastle.jce.provider.BouncyCastleProvider; public class PBE { private static final String salt = "A long, but constant phrase that will be used each time as the salt."; private static final int iterations = 2000; private static final int keyLength = 256; private static final SecureRandom random = new SecureRandom(); public static void main(String [] args) throws Exception { ...

AES Encryption Error: The Input Data Is Not A Complete Block?

Answer : StreamWriter writes UTF8 text characters to a stream. You're writing plaintext.ToString() as text for the ciphertext. This returns "System.Byte[]" , which does not translate into 16 bytes of UTF8. I believe the problem to be padding mode. Unless your text to be encrypted is for sure divisible by BlockSize (in bits, or BlockSize / 8 in bytes), you should specify a PaddingMode other than None. see the post here for example code I changed the function to this: public static byte[] Encrypt(byte[] plaintext, byte[] key) { using (var aes = Aes.Create()) { aes.BlockSize = 128; aes.Mode = CipherMode.ECB; aes.Padding = PaddingMode.None; var encryptor = aes.CreateEncryptor(key, new byte[16]); using(var target = new MemoryStream()) using (var cs = new CryptoStream(target, encryptor, CryptoStreamMode.Write)) { cs.Write(plaintext, 0, plaintext.Length); return target.ToArray...