aes

Java Cipher - AES Padding Problem

非 Y 不嫁゛ 提交于 2019-12-06 02:49:05
问题 I am using a AES cipher with a 16 byte block size. If I try and encrypt a 16 byte string I have no problems, but any other length not a multiple of 16 is throwing an exception. I know with 3-DES you can specify a padding type as part of the algorithm and it's handled with no extra work (e.g. DES/CBC/PKCS5Padding), but is there a way to specify this with AES? Or do I need to pad the pytes manually to a multiple of 16, and then strip them when I decrypt? Here is an abbreviated code sample.

Why is this string not equal when it is decrypted a second time with AesCryptoServiceProvider?

怎甘沉沦 提交于 2019-12-06 02:31:30
I'm having trouble with encryption and decryption of text in C# (VS2012, .NET 4.5). Specifically, When I encrypt and subsequently decrypt a string, the output is not the same as the input. However, bizarrely, if I copy the encrypted output and hardcode it as a string literal, decryption works. The following code sample illustrates the problem. What am I doing wrong? var key = new Rfc2898DeriveBytes("test password", Encoding.Unicode.GetBytes("test salt")); var provider = new AesCryptoServiceProvider { Padding = PaddingMode.PKCS7, KeySize = 256 }; var keyBytes = key.GetBytes(provider.KeySize >>

How do I encrypt in Python and decrypt in Java?

岁酱吖の 提交于 2019-12-06 01:08:47
问题 I'm trying to encrypt some data in a Python program and save it out, then decrypt that data in a Java program. In Python, I'm encrypting it like this: from Crypto.Cipher import AES KEY = '12345678901234567890123456789012' def encrypt(data): cipher = AES.new(KEY, AES.MODE_CFB) return cipher.encrypt(data) And in Java, I'm decrypting it like this: import java.security.*; import javax.crypto.*; import javax.crypto.spec.SecretKeySpec; public class Encryption { private static byte[] KEY = { '1', '2

AES/CBC/PKCS5Padding implementation in Ruby (for rails)

余生颓废 提交于 2019-12-05 21:52:44
I need to decrypt text encrypted using AES/CBC/PKCS5Padding scheme. The encrypted text I got was generated using some Java software. All values below are changed by me to something fictional. What I get is a Key aHjgYFutF672eGIUGGVlgSETyM9VJj0K (256-bit = 32-chars * 8-bit) and IV: rxYoks3c8hRRsL2P (16-bit) and (I supposed) Base64 encoded encrypted result ETlAHS5ZcshKxQUaHVB8== What I need is to decrypt in Ruby this ETlAHS5ZcshKxQUaHVB8== to get in the and a simple string, like 'blablablabla' I tried to decrypt what I got using both Ruby and just common linux console openssl command. NOTE: Key

How to correctly and consistely get bytes from a string for AES encryption?

怎甘沉沦 提交于 2019-12-05 21:27:00
问题 I am currently working on AES implementation in C#. The encryption method has two parameters: a string and a password. I am taking the supplied string and converting it to an array of bytes, so I can use it later for writing data to a stream with BinaryWriter . The problem is that when I use Convert.FromBase64String(string) I get FormatException: Invalid length. and when I use Encoding.UTF8.GetBytes(string) my decryption method throws and invalid PKCS7.Padding exception. I have been trying to

implementing bouncy castle aes 256

帅比萌擦擦* 提交于 2019-12-05 20:56:30
I am working on a chat application. The main feature is to send messages in encrypted form and when they reach their destination they can be decrypted. The problem I am having is that the messages are not getting decrypted at their destination but however they reach their destination in encrypted form. How the code works: Client A sends message "Hello" to client B... When Client A clicks on button "Send message" I save that text in a String and then that String is passed along with key and iv to the method Encrypt like this... en=enc.encrypt(msg.getBytes(), key.getBytes(), iv.getBytes()); I

PHP implementing Ciphertext Stealing (CTS) with CBC

核能气质少年 提交于 2019-12-05 19:55:40
I have been trying to implement Ciphertext Stealing(CTS) in PHP for CBC. Referring below two links How can I encrypt/decrypt data using AES CBC+CTS (ciphertext stealing) mode in PHP? and http://en.wikipedia.org/wiki/Ciphertext_stealing I am confused and stuck on the last and simplest step of XOR. I know this is silly but having tried all the combinations, i don't know what am i missing. Code follows. // 1. Decrypt the second to last ciphertext block, using zeros as IV. $second_to_last_cipher_block = substr($cipher_text, strlen($cipher_text) - 32, 16); $second_to_last_plain = @mcrypt_decrypt

AESManaged Encryption/Decryption - Padding is invalid and cannot be removed

谁说胖子不能爱 提交于 2019-12-05 19:48:45
I am coding an encryption/decryption aes utility with specific requirements: -AES/CBC/PKCS7 -256-Bit Key provided as base64 string -IV provided as base64 string So I am trying to encryp/decrypt this string "1234567890123456" using the same key and IV. Encryption runs fine but when trying to decrypt the encrypted string I get the "Padding is invalid and cannot be removed" exception. What am I missing? //This is the calling test method public void Test_AESEncryption_Decrypt() { try { var encoding = Encoding.ASCII; var key = encoding.GetString(Convert.FromBase64String(

How to export AES key derived using CryptoAPI

戏子无情 提交于 2019-12-05 19:38:28
I want to use the Windows CryptoAPI functions for AES encryption. As you know, the API has a lot of functions to create, hash and change the entered key; it derives the key and you get a handle to it. My problem is that I want to know what the derived key is? #include <Windows.h> #include <stdio.h> int main() { HCRYPTPROV hProv = 0; HCRYPTKEY hKey = 0; HCRYPTHASH hHash = 0; DWORD dwCount = 5; BYTE rgData[512] = {0x01, 0x02, 0x03, 0x04, 0x05}; LPWSTR wszPassword = L"pass"; DWORD cbPassword = (wcslen(wszPassword)+1)*sizeof(WCHAR); if(!CryptAcquireContext( &hProv, NULL, MS_ENH_RSA_AES_PROV, PROV

Perl & Ruby exchange AES encrypted information

こ雲淡風輕ζ 提交于 2019-12-05 19:07:28
What is the equivalent to Crypt::CBC in Perl for Ruby? Note: This problem similar to PHP/Perl at stackoverflow:655691 . Perl Version use Crypt::CBC; use MIME::Base64::Perl; my $cipher = Crypt::CBC->new( -key => "95A8EE8E89979B9EFDCBC6EB9797528D", -keysize => 32, -cipher => "Crypt::OpenSSL::AES" ); $encypted = $cipher->encrypt("ABCDEFGH12345678"); $base64 = encode_base64($encypted); print("$base64"); # output -> U2FsdGVkX18m1jVqRTxANhcEj6aADeOn+2cccDft2eYAMfOkYCvAAkTIOv01VHc/ $de_base64 = decode_base64($base64); $decrypted = $cipher->decrypt($de_base64); $c = $cipher->finish; print("$decrypted