为什么80%的码农都做不了架构师?>>>
-module(test).-export([encrypt/0, decrypt/1]).encrypt() ->PemBin &#61; <<"-----BEGIN RSA PRIVATE KEY-----\nMIICXAIBAAKBgQC1pJLB45JEyit2&#43;vOeZDIhnICGuf4h/w4OEwDn8C0BO84nLpoh\n7O8idxizwCkvmojdytgHc&#43;x4XFdVfzT8ZEKupH97yK8GXw0hQbMbRJssaheU9lsb\nAoSUV7V5bjckqfIdn2AE4jvus9nNo9KWgJoVbZYQGefMmBRNLLIvJbMntwIDAQAB\nAoGAXhtvtWhUBbPApRoLUcqTdnqA1ExUcASWoZAS7hevGW6EtiQc0urQUkjl0WsD\na5hixJYtKl9S6mDdFnybVRAUw0m3aDMZYiHZDk/f7YL&#43;Rz7piHcbzB68x8hSIfDc\n5ylEyohLnDOzq0TcGsn6jcFnCqSBe/cTXx6ALapK45e34fECQQDxRqqzwD2nhDhZ\nBDMzQYldiONeaj&#43;sZyOcfRz6KUc3nWyAvWYZGB1STAb7WPv0Loo2TFLELMQsz55l\n5vFW8AeDAkEAwLpJnwvoZ3hhu1K3fZsA6IMeGUnYWL9AaMClLEVLy4o1pI2Bbn/q\nG2UYS&#43;DiIULYOp6d82dqyCGnBbNELn00vQJAAw5ADzK6FSsJg6cX9ug3kyWuq31w\n/P/5ZXv4J/rp2fx2aVL8vgieJnKsC/beigMk4PcqfS2L9n1Spju8hrtXfQJBALV2\nMR9pTNcvh4WiqlW/filuWwageu2ElZaL3HlEfcsVRNC806NBGSHWG/xFr2R11bXL\n5xhoxSfOV76MPaQwC1kCQCtixwm5s7LqYdbCKmrQrPJJzBamE7nNfAPFphG4mR2/\n4K1oO&#43;byCdSzw0KOX9rHqeOcgom&#43;vybRC5NUMxpe1jk&#61;\n-----END RSA PRIVATE KEY-----">>,[DSAEntry] &#61; public_key:pem_decode(PemBin),PrivateKey &#61; public_key:pem_entry_decode(DSAEntry),{&#39;RSAPrivateKey&#39;, _Version, Modulus, _PublicExponent, _PrivateExponent, _Prime1, _Prime2, _Exponent1, _Exponent2, _Coefficient, _OtherPrimeInfos} &#61; PrivateKey,Size &#61; byte_size(binary:encode_unsigned(Modulus)) - 11,Msg &#61; <<"devicefeweger5hy67i89kjuytegrwfvcqe34567j8k9loikudesadfdsfrt564trefdg5trvfdvtgbnyhjunyhbgfvcdolikujhytr675rwfecsdvrtvfbrthnuy5nbtervferwbtynumnybregfvwfdgergrtyh7ujh56tgrvwefecefvgyj6uyrthgtvewcdevwt5j7uyhrtgvf4y6u7hyrtevceeefbtyu5jntdevicefewegt">>,Chunks &#61; split_packet(Size, Msg),EnChunks &#61; lists:map(fun(A) ->public_key:encrypt_private(A, PrivateKey)end, Chunks),list_to_binary(EnChunks).decrypt(Encode) ->PemBin &#61; <<"-----BEGIN PUBLIC KEY-----\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC1pJLB45JEyit2&#43;vOeZDIhnICG\nuf4h/w4OEwDn8C0BO84nLpoh7O8idxizwCkvmojdytgHc&#43;x4XFdVfzT8ZEKupH97\nyK8GXw0hQbMbRJssaheU9lsbAoSUV7V5bjckqfIdn2AE4jvus9nNo9KWgJoVbZYQ\nGefMmBRNLLIvJbMntwIDAQAB\n-----END PUBLIC KEY-----">>,[DSAEntry] &#61; public_key:pem_decode(PemBin),PublicKey &#61; public_key:pem_entry_decode(DSAEntry),{&#39;RSAPublicKey&#39;, Modulus, _PublicExponent} &#61; PublicKey,Size &#61; byte_size(binary:encode_unsigned(Modulus)),Chunks &#61; split_packet(Size, Encode),EnChunks &#61; lists:map(fun(A) ->public_key:decrypt_public(A, PublicKey)end, Chunks),list_to_binary(EnChunks).split_packet(Size, P) when byte_size(P) >&#61; Size->{Chunk, Rest} &#61; split_binary(P, Size),[Chunk|split_packet(Size, Rest)];
split_packet(_Size, <<>>) ->[];
split_packet(_Size, P) ->[P].
在控制台测试
> c(test).
{ok, test}
> EncryptS &#61; test:encrypt().
<<....>>
> test:decrypt(EncryptS).
今天才知道&#xff0c;rsa加密解密是有长度限制的&#xff0c;调试了好长时间&#xff0c;莫名其妙不知道为什么会报错&#xff0c;搜索到其他的语言写的demo才知道