def validate_rsa_key(key, is_secret=True):
 try:
  key = RSA.importKey(key)
 except ValueError:
  raise forms.ValidationError('Invalid RSA key. Please ensure that your key is in PEM (base64) format.')
 except Exception as e:
  raise forms.ValidationError('Invalid key detected: {}'.format(e))
 if (is_secret and (not key.has_private())):
  raise forms.ValidationError('This looks like a public key. Please provide your private RSA key.')
 elif ((not is_secret) and key.has_private()):
  raise forms.ValidationError('This looks like a private key. Please provide your public RSA key.')
 try:
  PKCS1_OAEP.new(key)
 except:
  raise forms.ValidationError('Error validating RSA key. Please ensure that your key supports PKCS#1 OAEP.')

def validate_rsa_key ( key , is_secret = True ) : try : key = RSA . importKey ( key ) except ValueError : raise forms . ValidationError ( 'Invalid RSA key. Please ensure that your key is in PEM (base64) format.' ) except Exception as e : raise forms . ValidationError ( 'Invalid key detected: {}' . format ( e ) ) if ( is_secret and ( not key . has_private ( ) ) ) : raise forms . ValidationError ( 'This looks like a public key. Please provide your private RSA key.' ) elif ( ( not is_secret ) and key . has_private ( ) ) : raise forms . ValidationError ( 'This looks like a private key. Please provide your public RSA key.' ) try : PKCS1_OAEP . new ( key ) except : raise forms . ValidationError ( 'Error validating RSA key. Please ensure that your key supports PKCS#1 OAEP.' )
validate the format and type of an rsa key .