Skip to content

Instantly share code, notes, and snippets.

@aliforever
Last active January 25, 2021 11:34
Show Gist options
  • Save aliforever/da8cf3c3469f6b15bcfe4d04f3a67ad5 to your computer and use it in GitHub Desktop.
Save aliforever/da8cf3c3469f6b15bcfe4d04f3a67ad5 to your computer and use it in GitHub Desktop.
// Node JS
var crypto = require('crypto');
// decrypt
var privKeyPem = `LS0tLS1CRUdJTiBSU0EgUFJJVkFURSBLRVktLS0tLQpNSUlFb3dJQkFBS0NBUUVBdXpQWGU1ZSs1Mkx6a0VRWnlyTnBiNDZuV0VTMElEZzMrWndVUGxoUlkzZVVmS0NFCjhBV1FiaWNaTC9qR2h3V3NOaGRodUhTTW42ZHhtZWNobXQxSGlVQitrMTkyWnpxdC92VnZyTzBWTDhkM0d6aHMKeElNS29PMVpyNGJ4Q0pFdm4wcFQ5ZUh6OE5ab1FPdWN4UU9OSThFM0dzMkpxRER2eEhNSUFFTERyd29DaS94egppRGRhZTRNakdUa0YxS1hzMndsVUsrNjk2Q1hOc1hscWhkb2N6Tm5qUlh2L3FRbC93ZU1kYWIxa2xhK3pXa1c4CmE3dU1TQlJRcllpUE5Ya2lKMCtDZXQrL0g5QzVkb1N2bjcrU2xJbXNpT3VnWGlGaUQzNlM0SEFkSTIvVXg5dXEKcTM5UnQzZ0hTTTZaYlFoclZHTnROSnVaaE4rVGdzSmV0WHlrRXdJREFRQUJBb0lCQUNtMzR1U1RLNk1MSlRaWQpSdHNxWW12UEFiSWxGWE04dEJTM1dMZVFIUXNxb2Zlb0doZ1hWVm1HOHhFQ3d0TmlTejkrdEhOVDNlYlNmUG1NClRhdmlka2U3RExveEF3VDhTYnhRM0FOUWpuRW9aWDIyZEY3U3lUS1YwbS9RaGJTZ0RsblVTYU81S0JEYTV6N1kKZ3daUXhVcGJpN2s0WnQ0SmtkVll3VWExbWtCT2U2dHl3Z3EvUDl1ZWNYTDlCUkxPcTVUdE9Kdlh1cnY0ckUrRAo0TU12L3czOFphREZnd3VBYUIwQTNBbjIzR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G1ac0Q3cXNDN1VrNWMzb0Z2L3l0d1NwbVBSVUR6Clk2Nklvd0tCZ0Z0Qm5MRHBPT0NHWHVsNUYxbDNLSzBDQTV3aldGd2dGMTNwL1o0cHpTbXFkMXkzSzRlamhpREkKQnhBR1dzQTB5cnBDSTZZZndzS0lYY3JYbDcveENyR0d0MkdneXdZb0I0TkUrS2JtcEpGTS9FK1FCOXZsNGNjWgpQeUZqazNGaGI4V1FYQTF6MCt1TXVkWGdQSmpOWDUrV2tjNytNeEd0K042OXZtY3h5dHZsCi0tLS0tRU5EIFJTQSBQUklWQVRFIEtFWS0tLS0tCg==
`
var secretData = Buffer.from(`chO6rEYhdLhjwIMCet+8Xdk4YG8myLkkhBeq2VGRaFqnWsSx3j0thrQkZ9fRA2hqjSKpfXAXrkue51YKuqcywiwYNNcNKF7nb//wjOKJliK9LFe3kQkE6VBEIe6ji7P44i9894UoeNqFdfDTEbaHCbjEJbc4VIr2imF3jUu6OetmHfQsfbztJsvOM+GFufQQWBujFEz5jFQYzLu+Eh3DsXP9ACR3K+on2+eEqDvhldKBcHmjS1Z51v/QxQynTNqqkjN9bHWWegiWSi/sQR1+ApYqYaojAvQL/Trw1PMXju1X/cbjo9onZRuFS4kJ+oosFTCURMjOtKBQ+nOa95O1gw==`, "base64");
origData = crypto.privateDecrypt({
key: Buffer.from(privKeyPem, 'base64').toString(),
padding: crypto.constants.RSA_PKCS1_PADDING,
}, secretData);
console.log(origData.toString("base64"));
return
// encrypt
var pubKeyPem = `LS0tLS1CRUdJTiBQVUJMSUMgS0VZLS0tLS0KTUlJQklqQU5CZ2txaGtpRzl3MEJBUUVGQUFPQ0FROEFNSUlCQ2dLQ0FRRUF1elBYZTVlKzUyTHprRVFaeXJOcApiNDZuV0VTMElEZzMrWndVUGxoUlkzZVVmS0NFOEFXUWJpY1pML2pHaHdXc05oZGh1SFNNbjZkeG1lY2htdDFICmlVQitrMTkyWnpxdC92VnZyTzBWTDhkM0d6aHN4SU1Lb08xWnI0YnhDSkV2bjBwVDllSHo4TlpvUU91Y3hRT04KSThFM0dzMkpxRER2eEhNSUFFTERyd29DaS94emlEZGFlNE1qR1RrRjFLWHMyd2xVSys2OTZDWE5zWGxxaGRvYwp6Tm5qUlh2L3FRbC93ZU1kYWIxa2xhK3pXa1c4YTd1TVNCUlFyWWlQTlhraUowK0NldCsvSDlDNWRvU3ZuNytTCmxJbXNpT3VnWGlGaUQzNlM0SEFkSTIvVXg5dXFxMzlSdDNnSFNNNlpiUWhyVkdOdE5KdVpoTitUZ3NKZXRYeWsKRXdJREFRQUIKLS0tLS1FTkQgUFVCTElDIEtFWS0tLS0t`
var randKey1 = crypto.randomBytes(32);
var rsaEncrypted1 = crypto.publicEncrypt({
key: Buffer.from(pubKeyPem, 'base64').toString(),
// @ts-ignore
padding: crypto.constants.RSA_PKCS1_PADDING,
}, randKey1);
console.log("rand key", randKey1.toString('base64'));
console.log(rsaEncrypted1.toString('base64'));
// -----------------------------------------------------
// Go
func GenerateAndStoreKeys() {
// generate key
privatekey, err := rsa.GenerateKey(rand2.Reader, 2048)
if err != nil {
fmt.Printf("Cannot generate RSA key\n")
os.Exit(1)
}
publickey := &privatekey.PublicKey
// dump private key to file
var privateKeyBytes = x509.MarshalPKCS1PrivateKey(privatekey)
privateKeyBlock := &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: privateKeyBytes,
}
privatePem, err := os.Create("private.pem")
if err != nil {
fmt.Printf("error when create private.pem: %s \n", err)
os.Exit(1)
}
err = pem.Encode(privatePem, privateKeyBlock)
if err != nil {
fmt.Printf("error when encode private pem: %s \n", err)
os.Exit(1)
}
// dump public key to file
publicKeyBytes, _ := x509.MarshalPKIXPublicKey(publickey)
publicKeyBlock := &pem.Block{
Type: "PUBLIC KEY",
Bytes: publicKeyBytes,
}
publicPem, err := os.Create("public.pem")
if err != nil {
fmt.Printf("error when create public.pem: %s \n", err)
os.Exit(1)
}
err = pem.Encode(publicPem, publicKeyBlock)
if err != nil {
fmt.Printf("error when encode public pem: %s \n", err)
os.Exit(1)
}
}
func DecodePrivateKeyFromPem() (*rsa.PrivateKey, error) {
path := "private.pem"
bs, _ := ioutil.ReadFile(path)
block, _ := pem.Decode(bs)
pKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
return pKey, nil
}
func main() {
// GenerateAndStoreKeys()
pKey, err := DecodePrivateKeyFromPem()
if err != nil {
fmt.Println(err)
return
}
b64Cipher := `gqsVlFIW/Txkq4NzdrQqW7ahKsdoBwsJUjNhW/Sh3rfCVDhs1Sqk3YcYHdxNh7v+uxW3iDU/xfgQGG7QZCutpRE+IRTK5ja0oQp9P8tf2GCeGLX0UmklV/uUunD/OxzYAzBxpU8XoYTnKPEFLOOZtpiFnCiHOIlm7V9Y+KLsZWS2aOkFie4lEmpz/di0Tp13qyJoTW9NJWQrvLks2wmVGYDcxITtYn0B2LbcNsOrjGoB7B9ImbtULTxiQpsVslkTKi4krZTqBVqtXXG
UDE5n4aZUWvSh/eXqyeiVJTLpZHOS5n6oNN+dd5v2mbgX7hU1gTNlggoQD6i7MlnyyX9b6A==`
cipherTextBytes, _ := base64.StdEncoding.DecodeString(b64Cipher)
decipher, _ := rsa.DecryptPKCS1v15(rand2.Reader, pKey, cipherTextBytes)
fmt.Println(base64.StdEncoding.EncodeToString(decipher))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment