Skip to content

Instantly share code, notes, and snippets.

@HarshSonawane
Created January 12, 2023 12:57
Show Gist options
  • Save HarshSonawane/20e3cb8291f15b5d8b7b5fa3f47ebe20 to your computer and use it in GitHub Desktop.
Save HarshSonawane/20e3cb8291f15b5d8b7b5fa3f47ebe20 to your computer and use it in GitHub Desktop.
Working AES Encryption and Decryption in RUBY
data = {
"A99RequestData": {
"RequestId": "PB4343265424",
"source": "PolicyBazaar",
"policyNumber": "",
"GetRecordType": "IND",
"InputIdType": "C",
"InputIdNo": "DCVPM4582S",
"DateOfBirth": "28-03-1992",
"MobileNumber": "",
"Pincode": "421202",
"BirthYear": "1992",
"Tags": "",
"ApplicationRefNumber": "",
"FirstName": "Mohit",
"MiddleName": "",
"LastName": "kumar",
"Gender": "M",
"ResultLimit": "Latest",
"photo": "",
"AdditionalAction": ""
}
}
# encryption
cipher = OpenSSL::Cipher::AES.new(256, :GCM)
cipher.encrypt
cipher.key = VENDOR_API["sbi_general"]["ckyc"]["key"]
cipher.iv = VENDOR_API["sbi_general"]["ckyc"]["iv"]
cipher.auth_data = 'auth_data'
result = cipher.update(data.to_json) + cipher.final
enc = Base64.encode64(result).gsub("\n", "")
## descryption
base_data = Base64.decode64(enc)
cipher = OpenSSL::Cipher::AES.new(256, :GCM)
cipher.decrypt
cipher.padding = 0
cipher.key = VENDOR_API["sbi_general"]["ckyc"]["key"]
cipher.iv = VENDOR_API["sbi_general"]["ckyc"]["iv"]
cipher.auth_data = 'auth_data'
result = cipher.update(base_data)
parsed_data = result.encode("ASCII", "UTF-8", invalid: :replace, undef: :replace, replace: "")
parsed_json = JSON(parsed_data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment