Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

Calcul de la clé RIB d'un compte bancaire

5 réponses
Avatar
fraginfo
Bonjour auriez vous un code java sous la main pour calculer la cl=E9 RIB
d un compte bancaire.

J'essaie de le faire, mais en vain :(

Merci de votre aide

5 réponses

Avatar
Ph. B.
a demandé:
Bonjour auriez vous un code java sous la main pour calculer la clé RIB
d un compte bancaire.

J'essaie de le faire, mais en vain :(

Merci de votre aide


Bonjour,

Voici une methode Delphi [ désolé ;-) ] qui permet de vérifier la
validité d'un RIB/RIP.

Rem.:
Ord renvoie la valeur ASCII d'un caractère (Ex.: Ord('A') => 65)
StrToIntDef renvoie la valeur entière d'un nombre transmis comme chaine
de caractère avec une valeur par défaut en cas d'erreur de conversion
(Ex.: StrToIntDef('1234', -1) => 1234)
(Ex.: StrToIntDef('12AB', -1) => -1)

Pour obtenir la clé RIB:

n4 := 97 - ((62 * n1 + 34 * n2 + 3 * n3) mod 97);

et adapter le reste... ;-)


function RIBPValide(NoCpte: string): Boolean;
var
n1, n2, n3, n4, i: Integer;
begin
Result := False;
// Longueur correcte ?
if Length(NoCpte) <> 23 then
Exit;

for i := 11 to 21 do
begin
// On transforme les lettres en chiffres
if NoCpte[i] in ['A'..'I'] then
NoCpte[i] := Char(Ord(NoCpte[i]) - 16)
else if NoCpte[i] in ['J'..'R'] then
NoCpte[i] := Char(Ord(NoCpte[i]) - 25)
else if NoCpte[i] in ['S'..'Z'] then
NoCpte[i] := Char(Ord(NoCpte[i]) - 33)
end;

// Découper la chaîne en 3 parties
n1 := StrToIntDef(Copy(NoCpte, 1, 7), -1);
n2 := StrToIntDef(Copy(NoCpte, 8, 7), -1);
n3 := StrToIntDef(Copy(NoCpte, 15, 7), -1);
n4 := StrToIntDef(Copy(NoCpte, 22, 2), -1);

if (n1 < 0) or (n2 < 0) or (n3 < 0) or (n4 < 0) then
Exit;

// Vérifier le n° sur la base d'un modulo 97
Result := ((62 * n1 + 34 * n2 + 3 * n3 + n4) mod 97 = 0);
end;

--
Philippe.

Avatar
TestMan
Bonjour,

Essayes :

private static final String SOURCES =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
private static final String RESULTS =
"123456789123456789234567890123456789";

public static final boolean isRIBValide(String compte){
assert compte!=null;
if(compte.length()!= 23) return false;
final StringBuilder buffer = new StringBuilder(compte);
for(int i;i<20;i++){
buffer.setCharAt(i,RESULTS.charAt( SOURCES.indexOf(
buffer.charAt(i))));
}
int n1 = Integer.parseInt(buffer.substring(0,7));
int n2 = Integer.parseInt(buffer.substring(7,14));
int n3 = Integer.parseInt(buffer.substring(14,21));
int n4 = Integer.parseInt(buffer.substring(21));
return (62 * n1 + 34 * n2 + 3 * n3 + n4) % 97 == 0;
}


A toi de tester s'il est bon ;-)
J'ai testé sur mon cas perso, ça marche, par contre si tu passes avec
que des zéros, ça marche aussi (!!).

A+

TM

Bonjour auriez vous un code java sous la main pour calculer la clé RIB
d un compte bancaire.

J'essaie de le faire, mais en vain :(

Merci de votre aide



Avatar
Ph. B.
TestMan wrote:
Bonjour,

Essayes :

private static final String SOURCES =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
private static final String RESULTS =
"123456789123456789234567890123456789";

public static final boolean isRIBValide(String compte){
assert compte!=null;
if(compte.length()!= 23) return false;
final StringBuilder buffer = new StringBuilder(compte);
for(int i;i<20;i++){
buffer.setCharAt(i,RESULTS.charAt( SOURCES.indexOf(
buffer.charAt(i))));
}
int n1 = Integer.parseInt(buffer.substring(0,7));
int n2 = Integer.parseInt(buffer.substring(7,14));
int n3 = Integer.parseInt(buffer.substring(14,21));
int n4 = Integer.parseInt(buffer.substring(21));
return (62 * n1 + 34 * n2 + 3 * n3 + n4) % 97 == 0;
}


A toi de tester s'il est bon ;-)
J'ai testé sur mon cas perso, ça marche, par contre si tu passes avec
que des zéros, ça marche aussi (!!).

A+

TM



Comme il s'agit d'obtenir la clé RIB: ;-)

int n4 = 97 - ((62 * n1 + 34 * n2 + 3 * n3) % 97);
return n4;

--
Philippe.

Avatar
drjarode
fraginfo a écrit le 15/05/2006 à 17h47 :
Bonjour auriez vous un code java sous la main pour calculer la cl RIB
d un compte bancaire.

J'essaie de le faire, mais en vain :(

Merci de votre aide


voici le code, enregistre le avec extension htm

<!-- saved from url=(0022)http://internet.e-mail -->
</head><body onload="formulaire.Banque.focus()">
<script language="JavaScript">

function getKey(banque, guichet, compte)
{
if (5 != banque.length || 5 != guichet.length || 11 != compte.length)
return;
function replaceAlpha(alpha)
{
return '12345678912345678923456789'.charAt(alpha.charCodeAt(0) - 65);
}
compte= parseInt(compte.toUpperCase().replace(/[A-Z]/g, replaceAlpha), 10);
return 97 - (((parseInt(banque, 10)% 97 * 100000 + parseFloat(guichet)) % 97 * 100000000000 + compte) % 97) * 100 % 97;
}






function verif() {
valtotal = "OK";

document.formulaire.rib.value=getKey( document.formulaire.Banque.value, document.formulaire.Guichet.value, document.formulaire.Compte.value);
; }

function RsetVL(){formulaire.reset();formulaire.Banque.focus();cxv=0}

</script>
</head>
<body>
<b><font color="#008000">Touche</font></b> <b><font color="#FF0000">Echap</font></b> =
<font color="#0000FF">efface le formulaire</font><br>
<b><font color="#008000">Touche</font></b> <b><font color="#FF0000">Entrée</font></b> =
<font color="#0000FF">passe au champ suivant</font><br>
<font color="#008000"><b>NB </b></font>: vous pouvez taper les chiffres a la suite,<br>
le curseeur passera au champ suivant automatiquement<br>
<form name="formulaire">

Banque : <input type="text" name="Banque" size="5" onkeypress='
if (formulaire.Banque.value.length==5) {cxv=window.event.keyCode;window.event.keyCode="";formulaire.Guichet.focus();}
if (window.event.keyCode=){formulaire.Guichet.focus()}'><br>

Guichet : <input type="text" name="Guichet" size="5" onfocus='window.event.keyCode;formulaire.Guichet.value=String.fromCharCode(cxv)' onkeypress='
if (window.event.keyCode='){RsetVL()}
if (formulaire.Guichet.value.length==5) {cxv=window.event.keyCode;window.event.keyCode="";formulaire.Compte.focus();}
if (window.event.keyCode=){formulaire.Compte.focus()}'><br>

Compte : <input type="text" name="Compte" size="12" title="Enter = Calcule" onfocus='window.event.keyCode;formulaire.Compte.value=String.fromCharCode(cxv)' onkeypress='
if (formulaire.Compte.value.length=) {window.event.keyCode="";verif();}
if (window.event.keyCode='){RsetVL()}
if (window.event.keyCode=){verif()}'><br>

RIB : <input type="text" name="rib" size="2"><br>
<input type="button" value="Calcule" name="CalVL" OnClick="verif();">
</form>

</html>
Avatar
riromain
Le mardi 16 Mai 2006 à 19:56 par Ph. B. :
TestMan wrote:
Bonjour,

Essayes :

private static final String SOURCES =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
private static final String RESULTS =
"123456789123456789234567890123456789";

public static final boolean isRIBValide(String compte){
assert compte!=null;
if(compte.length()!= 23) return false;
final StringBuilder buffer = new StringBuilder(compte);
for(int i;i<20;i++){
buffer.setCharAt(i,RESULTS.charAt( SOURCES.indexOf(
buffer.charAt(i))));
}
int n1 = Integer.parseInt(buffer.substring(0,7));
int n2 = Integer.parseInt(buffer.substring(7,14));
int n3 = Integer.parseInt(buffer.substring(14,21));
int n4 = Integer.parseInt(buffer.substring(21));
return (62 * n1 + 34 * n2 + 3 * n3 + n4) % 97 == 0;
}


A toi de tester s'il est bon ;-)
J'ai testé sur mon cas perso, ça marche, par contre si tu passes
avec
que des zéros, ça marche aussi (!!).

A+

TM




Comme il s'agit d'obtenir la clé RIB: ;-)

int n4 = 97 - ((62 * n1 + 34 * n2 + 3 * n3) % 97);
return n4;

--
Philippe.


Gros déterrage mais ayant eu besoin de ce calcul de clé ça servira peut-être à d'autre avec le passage imminent en SEPA:

Ce code ne fonctionne pas si le dernier caractère du compte est alphabétique. C'est rare mais parfaitement autorisé pour un numéro de compte français donc faite attention si vous utilisé ce code...

Sinon la clé calculé est correcte :-)