Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Calcul de la clé RIB d'un compte bancaire

318 views
Skip to first unread message

frag...@free.fr

unread,
May 15, 2006, 11:47:54 AM5/15/06
to
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

Ph. B.

unread,
May 15, 2006, 6:11:16 PM5/15/06
to
frag...@free.fr a demandé:

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.

TestMan

unread,
May 16, 2006, 9:31:01 AM5/16/06
to
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=10;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

frag...@free.fr a écrit :

Ph. B.

unread,
May 16, 2006, 1:56:20 PM5/16/06
to
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=10;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.

drjarode

unread,
Dec 26, 2009, 8:13:53 PM12/26/09
to
fraginfo a �crit le 15/05/2006 � 17h47 :
> 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
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==13){formulaire.Guichet.focus()}'><br>

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

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

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

</html>

Fos Pat

unread,
Jan 29, 2010, 1:02:15 PM1/29/10
to
drjarode wrote:
> fraginfo a �crit le 15/05/2006 � 17h47 :

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

http://commons.apache.org/validator/api-1.3.1/org/apache/commons/validator/CreditCardValidator.html

> voici le code, enregistre le avec extension htm

C'est du javascript...


riromain

unread,
Jun 19, 2014, 10:32:45 AM6/19/14
to
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 :-)
0 new messages