Automate bank transaction(Bank reconciliation) in ERPnext through ICICI Bank API

Hello all,
I am Trying automate bank transaction(Bank reconciliation) in ERPNext through ICICI Bank API ,But getting error “ValueError: RSA key format is not supported”, need help to resolve this.
Thanks in advance

Regards
Neha Bora
Delhi,

Here is my code in Python
# -- coding: utf-8 --
# Copyright © 2020, Frappe Technologies Pvt. Ltd. and contributors
# For license information, please see license.txt

from __future__ import unicode_literals
import frappe
from frappe.model.document import Document
from frappe import _
import json
import requests
import base64
#from Crypto.Cipher import PKCS1_OAEP
#from Crypto.PublicKey import RSA
#from Crypto.Cipher import PKCS1_v1_5 as Cipher_PKCS1_v1_5
from frappe.integrations.utils import (make_get_request, make_post_request, create_request_log,
	create_payment_gateway)

class ICICIBankSettings(Document):
	pass

@frappe.whitelist(allow_guest=True)
def response():
#	frappe.msgprint(str(frappe.db.get_single_value("ICICI Bank Settings" , "server_url")))
#	resp = make_post_request(
#				'https://apigwuat.icicibank.com:8443/api/Corporate/CIB/v1/Transaction/apikey/971d473c1ce148f1a659e57cad9b6473/AGGRID/CUST0381/AGGRNAME/SPRINGHOUSE/CORPID/PRACHICIB1/USERID/USER3/URN/SR188835288/DEBITACC/000451000301/CREDITACC/000405002777/IFSC="ICIC0000011" (OWN%26TPA),"DLXB0000092"(IFS%26RGS),"SBIN0003060"(RTG)/AMOUNT/1',
#				auth=("971d473c1ce148f1a659e57cad9b6473", ''),
#				data=json.dumps(subscription_details),
#				headers={
#					"accept": "*/*",
#					"content-length": "684",
#					"content-type": "text/plain",
#					"apikey": "971d473c1ce148f1a659e57cad9b6473"
#					"content-type": "application/json"
#				}
#			)"""

#	url = 'https://apigwuat.icicibank.com:8443/api/Corporate/CIB/v1/AccountStatement/apikey/971d473c1ce148f1a659e57cad9b6473/AGGRID/CUST0381/AGGRNAME/SPRINGHOUSE/CORPID/CIBNEXT/USERID/CIBTESTING6/URN/SR188835288/ACCOUNTNO/000405001611/FROMDATE/01-01-2016/TODATE/30-12-2016/'
	url = 'https://apigwuat.icicibank.com:8443/api/Corporate/CIB/v1/Registration'
	payload = {
		"CORPID":"PRACHICIB1",
		"USERID":"USER3",
#		"ALIASID":"EXPRESSPAY",
		"URN":"SR188835288",
		"AGGRNAME":"SPRINGHOUSE",
		"AGGRID":"CUST0381"
		}
	headers= {
#		"accept": "*/*",
#		"content-length": "684",
		"content-type": "text/plain",
		"apikey": "971d473c1ce148f1a659e57cad9b6473"
		}
#	import Crypto
#	import random
	import base64
	from erpnext.erpnext_integrations.doctype.icici_bank_settings import PKCS1_OAEP
	from erpnext.erpnext_integrations.doctype.icici_bank_settings import RSA
	from erpnext.erpnext_integrations.doctype.icici_bank_settings import PKCS1_v1_5 as Cipher_PKCS1_v1_5
	from erpnext.erpnext_integrations.doctype.icici_bank_settings import random
#	from frappe.utils.make_random import get_random
#	r = random.randint(1,10)
#	import Crypto
#	from Crypto.Cipher import PKCS1_OAEP
#	from Crypto.PublicKey import RSA
#	from Crypto.Cipher import PKCS1_v1_5 as Cipher_PKCS1_v1_5


# sender side
	message = payload
#	f = open('/home/frappe/frappe-bench/apps/erpnext/erpnext/public/certificatename.pem','rb')
# 	key = RSA.importKey(f.read())
	key = RSA.importKey(open('/home/frappe/frappe-bench/apps/erpnext/erpnext/public/certificatename.pem').read())
#	key = RSA.generate(4096)
#	f =open('/home/frappe/frappe-bench/apps/erpnext/erpnext/public/certificatename.pem',wb)
#	f.write(key.export_key('PEM'))
#	f.close()

#	f.open('/home/frappe/frappe-bench/apps/erpnext/erpnext/public/certificatename.pem',r)
#	key=RSA.import_key(f.read())
	cipher = Cipher_PKCS1_v1_5.new(key)
	cipher_text = cipher.encrypt(message.encode())
	response = requests.request("POST", url, headers=headers, data = payload)
	frappe.msgprint(response)
#	frappe.msgprint(str(response))

getting error “ValueError: RSA key format is not supported”

1 Like