require 'openssl' require 'base64' require 'nokogiri' require 'securerandom' xml = File.read('spec/fixtures/encryptedResponse.xml') private_key = OpenSSL::PKey::RSA.new File.read('spec/fixtures/keysncerts/userkey.pem'), 'hello' cv = Base64.decode64 "YYq0hkSXofEEiiZ2LdCx8M/yvR0P+G4U510Bqkg4E3YoApqPJinqmOlNU7x/MChZp23zWHJqS4fH0VtK5ZT8gToEwbKQ/DsjYFQXVCRHxjrRp0Mfrvj89bTpivA3TPdVXhS80MFqtd7NfKEQSI4roJY9JgDvn45j77oXurr6h1c=" cv2 = Base64.decode64 "TQzLeeCNG8y8+C1XpC7ZNtvrCyIOl7e4b1zOUwARlbmiIC1YIUHl38uhAkw0B7+BNZ9inafH2RyWO8MH5XSs4O7cyLkNJXETmc1CFozTa2J4DWLqDOByyT0LqlGir80PjfCeOnSFsLR9SCnvJpcpuEseRHfXnotnMpP1fQFiSheKGEb/oXn9feVxqlvBOOK0VHxFqis0Cb4KYsQWu0DiujBZQMj+tKNnnodb6jNwKcR4JDNda2JwWxXs0hViumO/2AC+YhAoiVBwPH2rXBv+yXkQcI0y8u+B9fYSoZ2pNXk=" key = private_key.private_decrypt(cv) cipher = OpenSSL::Cipher::Cipher.new('des-ede3-cbc') cipher.decrypt cipher.padding = 0 cipher.key = key cipher.iv = cv2[0..8] out = cipher.update(cv2[8..-1]) out << cipher.final puts out.inspect out = out[0...-out.bytes.to_a.last] cipher.encrypt cipher.padding = 0 cipher.key = key cipher.iv = cv2[0..8] padding_length = (out.length % 8) padding = SecureRandom.random_bytes(padding_length -1) + padding_length.chr puts padding.inspect out = cipher.update(out + padding) out << cipher.final cv2 = out key = private_key.private_decrypt(cv) cipher = OpenSSL::Cipher::Cipher.new('des-ede3-cbc') cipher.decrypt cipher.padding = 0 cipher.key = key cipher.iv = cv2[0..8] out = cipher.update(cv2[8..-1]) out << cipher.final puts out.inspect out = out[0...-out.bytes.to_a.last] puts out.inspect #doc = Nokogiri::XML::Document.parse(xml) # #wrapped_key = Base64.decode64('BxnBnHLtE1IjI9bTGDVs9EyJspS46SJrX4r3yYRdBJYawEp5AHWx6SgVB1TuU466ORIPX0HqdUvy8efOP/7gHhFlm/noKc+qToD1YSvGQ27kJdZbBJE+kcnDZpgqPHCR+fr3Nv/WfN+eZRYMlky1D8JaWFd1P6Eqi8xqY1BirHQ=') # #private_key = OpenSSL::PKey::RSA.new File.read('spec/fixtures/post-office.pem') #key = private_key.private_decrypt(wrapped_key, OpenSSL::PKey::RSA::PKCS1_OAEP_PADDING) # #bytes = Base64.decode64(c2).unpack('C*') #puts key.inspect # #iv = bytes.pack('c16') #others = bytes.pack('c16X16c*') # #cipher = OpenSSL::Cipher.new('aes-128-cbc') #cipher.decrypt #cipher.iv = Base64.decode64(c2)[0..15] #cipher.key = key # #out = cipher.update(others) # #The encrypted string's length might not be a multiple of the block #length of aes-128-cbc (16), so add in another block and then trim #off the padding. More info about padding is available at #http://www.w3.org/TR/2002/REC-xmlenc-core-20021210/Overview.html in #Section 5.2 #puts out << cipher.update("\x00" * 16) #puts out[0...(-out.bytes.to_a.last)] #