encryption - RSA encrypt and decrypt between java and go -
in java encrypt string rsa : "hello,i plaintext string!@sina.com" get:
kkkhf5qsxx8adadk66aoysmv8loi4vwuanal+7kv6va/5zr7pswgrs5bzbk4vmyk9fa5clqolr2n b6oupnwpgq3af7pn/f45+pdtkrsblx8+q/mw7toyr525e7nvepdblm2wlqz4gh5qmqzei3me3zc3 030jrg0geg13n/1ezmo=
but tried many methods , couldn't decrypt in go. what's problem?
any appreciated, thanks.
here code:
java:
public static void main(string[] args) throws exception { string pubkey_from_go="migfma0gcsqgsib3dqebaquaa4gnadcbiqkbgqdzsfv1qscqydy4vy+p4e3catmv"+ "ppxqcrvrf1cb4drkv0hau24y7m5qytt52kr539rdbkkdlam6s20lwy7+5c0dgacd"+ "wywd/7pecelyeipzjl07vro7ate8bfjya+wltgk9+xnuihiumukulw4kdx21+1nl"+ "auej6pew+dakmjwf6qidaqab"; string plaintext = "hello,i plaintext string!@sina.com"; string encryptstring=encbygopubkey(pubkey_from_go,plaintext); } public static string encbygopubkey(string pubkey_from_go,string plaintext) throws exception { cipher cipher = cipher.getinstance("rsa");//cipher.getinstance("rsa/ecb/pkcs1padding"); byte[] plaintextbytes = plaintext.getbytes(); publickey pubkey_go=getpublickey(pubkey_from_go); cipher.init(cipher.encrypt_mode, pubkey_go); byte[] enbytes = cipher.dofinal(plaintextbytes); string encryptstring = (new base64encoder()).encode(enbytes); return encryptstring; } public static publickey getpublickey(string key) throws exception { byte[] keybytes; keybytes = (new base64decoder()).decodebuffer(key); x509encodedkeyspec keyspec = new x509encodedkeyspec(keybytes); keyfactory keyfactory = keyfactory.getinstance("rsa"); publickey publickey = keyfactory.generatepublic(keyspec); return publickey; }
go:
import ( "crypto/rand" "crypto/rsa" // "crypto/sha1" "crypto/x509" "encoding/base64" "encoding/pem" "errors" // "flag" "fmt" "github.com/astaxie/beego" ) var publickey = []byte(` -----begin public key----- migfma0gcsqgsib3dqebaquaa4gnadcbiqkbgqdzsfv1qscqydy4vy+p4e3catmv ppxqcrvrf1cb4drkv0hau24y7m5qytt52kr539rdbkkdlam6s20lwy7+5c0dgacd wywd/7pecelyeipzjl07vro7ate8bfjya+wltgk9+xnuihiumukulw4kdx21+1nl auej6pew+dakmjwf6qidaqab -----end public key----- `) var privatekey = []byte(` -----begin rsa private key----- miicxqibaakbgqdzsfv1qscqydy4vy+p4e3catmvppxqcrvrf1cb4drkv0hau24y 7m5qytt52kr539rdbkkdlam6s20lwy7+5c0dgacdwywd/7pecelyeipzjl07vro7 ate8bfjya+wltgk9+xnuihiumukulw4kdx21+1nlauej6pew+dakmjwf6qidaqab aogbajlnxentqj6ofcl9fmr2jlmjjtmrtqt9inqee7m3m7blhec+mcjohmnvbjam zpthdordxiz6ocuof6z2+dl35lntgfh5j7s34up2bwzf1iyyqfyscnexgnhkt1g1 xkqthmtc2gwwtheg+s6ciiyw2igrrp2rke81vyhexprexf0hakea9izb0miysmcb /jemljb0lb3y/b8xjgjqffbqt7bmwbvjvzwzvpnmnxi9swgdgupxscuairoxjz40 irz2c9eouwjbaopjpvv8sgw4vaseoqljvsq/c/pifx6rvzndglc8brg7sgtppjhg 4g+m3mvgpcx1a/eu1mb+fhij2laz/ptty6scqgaw9nwiwu3drivgcsmm0myh/3x9 dacwlsjoctiodq1fq9rrede5qfpjnajdjfsijntx1f+l3yceebxtw0ynz2mcqbi8 9kp274is5fkwkufnknukuk4wkouexeo+lpr+vihs7k6wq8ngdd4/mujojbr5mkrw dpwqa3n5tmndqvgv8gmcqqcakgjgwygvo3/milffimbp+m7/y3vcptarldxryqwo aqjxwc71zgbfdityvdgjm1mtqc8xqek1fxn1vfpy2c6o -----end rsa private key----- ` func rsaencrypt(origdata []byte, pubkey []byte) ([]byte, error) { block, _ := pem.decode(pubkey) if block == nil { return nil, errors.new("public key error") } pubinterface, err := x509.parsepkixpublickey(block.bytes) if err != nil { return nil, err } pub := pubinterface.(*rsa.publickey) return rsa.encryptpkcs1v15(rand.reader, pub, origdata) } func rsadecrypt(ciphertext []byte, privkey []byte) ([]byte, error) { block, _ := pem.decode(privkey) if block == nil { return nil, errors.new("private key error!") } priv, err := x509.parsepkcs1privatekey(block.bytes) if err != nil { return nil, err } return rsa.decryptpkcs1v15(rand.reader, priv, ciphertext) } var data64 string = `kkkhf5qsxx8adadk66aoysmv8loi4vwuanal+7kv6va/5zr7pswgrs5bzbk4vmyk9fa5clqolr2nb6oupnwpgq3af7pn/f45+pdtkrsblx8+q/mw7toyr525e7nvepdblm2wlqz4gh5qmqzei3me3zc3030jrg0geg13n/1ezmo=` func main() { data, _ := base64.stdencoding.decodestring(data64) origdata, _ := rsadecrypt(data, privatekey) fmt.println("rsa-------" + string(origdata)) }
try use padding in java encrypt. know nothing "go" in java encrypting message no padding.
cipher cipher = cipher.getinstance("rsa");
in code snippet change "rsa" "rsa/ecb/pkcs1padding" since in "go" see using function called "decryptpkcs1v15", pkcs1 v1.5 standard means pkcs#1 padding used. hope helps you.
Comments
Post a Comment