假设我们有一张密码为3451的LISA信用卡,它的密码可以像这样被编码:

    这样密码就可以写在一张纸上,即使这张纸落在他人手上,密码也是安全的。

    我们很容易的就可以构造一个用来执行加密的函数encode(Pin,Password)

    1. encode(Pin, Password) ->
    2. Code = {nil,nil,nil,nil,nil,nil,nil,nil,nil,
    3. nil,nil,nil,nil,nil,nil,nil,nil,nil,
    4. nil,nil,nil,nil,nil,nil,nil,nil},
    5. encode(Pin, Password, Code).
    6.  
    7. encode([], _, Code) ->
    8. Code;
    9. encode(Pin, [], Code) ->
    10. io:format("Out of Letters~n",[]);
    11.  
    12. encode([H|T], [Letter|T1], Code) ->
    13. Arg = index(Letter) + 1,
    14. case element(Arg, Code) of
    15. nil ->
    16. encode(T, T1, setelement(Arg, Code, index(H)));
    17. encode([H|T], T1, Code)
    18. end.
    19.  
    20. index(X) when X >= $0, X =< $9 ->
    21. X - $0;
    22.  
    23. index(X) when X >= $A, X =< $Z ->
    24. X - $A.

    我们看一下以下的例子:

    1. print_code([], Seed) ->
    2. Seed;
    3.  
    4. print_code([nil|T], Seed) ->
    5. NewSeed = ran(Seed),
    6. Digit = NewSeed rem 10,
    7. io:format("~w ",[Digit]),
    8. print_code(T, NewSeed);
    9. print_code([H|T],Seed) ->
    10. io:format("~w ",[H]),
    11. print_code(T, Seed).
    12.  
    13. ran(Seed) ->
    14. (125 * Seed + 1) rem 4096.

    然后我们需要一些小函数将所有东西连接在一起:

    最后我们可以运行这个程序了:

    1. 1> pin:test().
    2. a b c d e f g h i j k l m n o p q r s t u v w x y z
    3. 1 0 5 3 4 3 2 7 2 5 4 1 9 4 9 6 3 4 1 4 1 2 7 8 5 0 lisa
    4. 9 0 3 1 2 5 8 3 6 7 0 4 5 2 3 4 7 6 9 4 9 2 7 4 9 2 carwash
    5. 7 2 2 4 3 1 2 1 8 3 0 1 5 4 1 0 5 6 5 4 3 0 3 8 5 8 bigbank
    6. 1 0 6 7 5 7 6 9 4 5 4 8 3 2 1 0 7 6 1 4 9 6 5 8 3 4 doorcode1
    7. 1 4 3 8 8 3 2 5 6 1 4 2 7 2 9 4 5 2 3 6 9 4 3 2 5 8 doorcode2
    8. 7 4 7 4 2 5 6 5 8 5 8 8 9 4 7 6 5 0 1 2 9 0 9 6 3 8 cashcard
    9. 7 4 7 4 2 7 8 7 4 3 8 8 9 6 3 8 5 2 1 4 1 2 1 4 3 4 chequecard
    10. true