WeixiangYan/CodeTransOcean huggingface dataset:
Task: RSA code



C#: using System;
using System.Numerics;
using System.Text;

class Program
{
    static void Main(string[] args)
    {
        BigInteger n = BigInteger.Parse("9516311845790656153499716760847001433441357");
        BigInteger e = 65537;
        BigInteger d = BigInteger.Parse("5617843187844953170308463622230283376298685");

        const string plaintextstring = "Hello, Rosetta!";
        byte[] plaintext = ASCIIEncoding.ASCII.GetBytes(plaintextstring);
        BigInteger pt = new BigInteger(plaintext);
        if (pt > n)
            throw new Exception();

        BigInteger ct = BigInteger.ModPow(pt, e, n);
        Console.WriteLine("Encoded:  " + ct);

        BigInteger dc = BigInteger.ModPow(ct, d, n);
        Console.WriteLine("Decoded:  " + dc);

        string decoded = ASCIIEncoding.ASCII.GetString(dc.ToByteArray());
        Console.WriteLine("As ASCII: " + decoded);
    }
}






VB: Imports System
Imports System.Numerics
Imports System.Text

Module Module1
    Sub Main()
        Dim n As BigInteger = BigInteger.Parse("9516311845790656153499716760847001433441357")
        Dim e As BigInteger = 65537
        Dim d As BigInteger = BigInteger.Parse("5617843187844953170308463622230283376298685")
        Dim plainTextStr As String = "Hello, Rosetta!"
        Dim plainTextBA As Byte() = ASCIIEncoding.ASCII.GetBytes(plainTextStr)
        Dim pt As BigInteger = New BigInteger(plainTextBA)
        If pt > n Then Throw New Exception() 
        Dim ct As BigInteger = BigInteger.ModPow(pt, e, n)
        Console.WriteLine(" Encoded: " & ct.ToString("X"))
        Dim dc As BigInteger = BigInteger.ModPow(ct, d, n)
        Console.WriteLine(" Decoded: " & dc.ToString("X"))
        Dim decoded As String = ASCIIEncoding.ASCII.GetString(dc.ToByteArray())
        Console.WriteLine("As ASCII: " & decoded)
    End Sub
End Module
Task: RSA code




Go: package main

import (
    "fmt"
    "math/big"
)

func main() {
    var n, e, d, bb, ptn, etn, dtn big.Int
    pt := "Rosetta Code"
    fmt.Println("Plain text:            ", pt)

    
    
    
    n.SetString("9516311845790656153499716760847001433441357", 10)
    e.SetString("65537", 10)
    d.SetString("5617843187844953170308463622230283376298685", 10)

    
    for _, b := range []byte(pt) {
        ptn.Or(ptn.Lsh(&ptn, 8), bb.SetInt64(int64(b)))
    }
    if ptn.Cmp(&n) >= 0 {
        fmt.Println("Plain text message too long")
        return
    }
    fmt.Println("Plain text as a number:", &ptn)

    
    etn.Exp(&ptn, &e, &n)
    fmt.Println("Encoded:               ", &etn)

    
    dtn.Exp(&etn, &d, &n)
    fmt.Println("Decoded:               ", &dtn)

    
    var db [16]byte
    dx := 16
    bff := big.NewInt(0xff)
    for dtn.BitLen() > 0 {
        dx--
        db[dx] = byte(bb.And(&dtn, bff).Int64())
        dtn.Rsh(&dtn, 8)
    }
    fmt.Println("Decoded number as text:", string(db[dx:]))
}


Java: public static void main(String[] args) {
    
    BigInteger n = new BigInteger("9516311845790656153499716760847001433441357");
    BigInteger e = new BigInteger("65537");
    BigInteger d = new BigInteger("5617843187844953170308463622230283376298685");
    Charset c = Charsets.UTF_8;
    String plainText = "Rosetta Code";
    System.out.println("PlainText : " + plainText);
    byte[] bytes = plainText.getBytes();
    BigInteger plainNum = new BigInteger(bytes);
    System.out.println("As number : " + plainNum);
    BigInteger Bytes = new BigInteger(bytes);
    if (Bytes.compareTo(n) == 1) {
        System.out.println("Plaintext is too long");
        return;
    }
    BigInteger enc = plainNum.modPow(e, n);
    System.out.println("Encoded: " + enc);
    BigInteger dec = enc.modPow(d, n);
    System.out.println("Decoded: " + dec);
    String decText = new String(dec.toByteArray(), c);
    System.out.println("As text: " + decText);
}
Task: RSA code





Java: public static void main(String[] args) {
    
    BigInteger n = new BigInteger("9516311845790656153499716760847001433441357");
    BigInteger e = new BigInteger("65537");
    BigInteger d = new BigInteger("5617843187844953170308463622230283376298685");
    Charset c = Charsets.UTF_8;
    String plainText = "Rosetta Code";
    System.out.println("PlainText : " + plainText);
    byte[] bytes = plainText.getBytes();
    BigInteger plainNum = new BigInteger(bytes);
    System.out.println("As number : " + plainNum);
    BigInteger Bytes = new BigInteger(bytes);
    if (Bytes.compareTo(n) == 1) {
        System.out.println("Plaintext is too long");
        return;
    }
    BigInteger enc = plainNum.modPow(e, n);
    System.out.println("Encoded: " + enc);
    BigInteger dec = enc.modPow(d, n);
    System.out.println("Decoded: " + dec);
    String decText = new String(dec.toByteArray(), c);
    System.out.println("As text: " + decText);
}



Python: import binascii

n = 9516311845790656153499716760847001433441357    
e = 65537
d = 5617843187844953170308463622230283376298685

message='Rosetta Code!'
print('message                 ', message)

hex_data   = binascii.hexlify(message.encode())
print('hex data                ', hex_data)

plain_text = int(hex_data, 16)
print('plain text integer      ', plain_text)

if plain_text > n:
  raise Exception('plain text too large for key')

encrypted_text = pow(plain_text,     e, n)
print('encrypted text integer  ', encrypted_text)

decrypted_text = pow(encrypted_text, d, n)
print('decrypted text integer  ', decrypted_text)

print('message                 ', binascii.unhexlify(hex(decrypted_text)[2:]).decode())
Task: RSA code





Java: public static void main(String[] args) {
    
    BigInteger n = new BigInteger("9516311845790656153499716760847001433441357");
    BigInteger e = new BigInteger("65537");
    BigInteger d = new BigInteger("5617843187844953170308463622230283376298685");
    Charset c = Charsets.UTF_8;
    String plainText = "Rosetta Code";
    System.out.println("PlainText : " + plainText);
    byte[] bytes = plainText.getBytes();
    BigInteger plainNum = new BigInteger(bytes);
    System.out.println("As number : " + plainNum);
    BigInteger Bytes = new BigInteger(bytes);
    if (Bytes.compareTo(n) == 1) {
        System.out.println("Plaintext is too long");
        return;
    }
    BigInteger enc = plainNum.modPow(e, n);
    System.out.println("Encoded: " + enc);
    BigInteger dec = enc.modPow(d, n);
    System.out.println("Decoded: " + dec);
    String decText = new String(dec.toByteArray(), c);
    System.out.println("As text: " + decText);
}




VB: Imports System
Imports System.Numerics
Imports System.Text

Module Module1
    Sub Main()
        Dim n As BigInteger = BigInteger.Parse("9516311845790656153499716760847001433441357")
        Dim e As BigInteger = 65537
        Dim d As BigInteger = BigInteger.Parse("5617843187844953170308463622230283376298685")
        Dim plainTextStr As String = "Hello, Rosetta!"
        Dim plainTextBA As Byte() = ASCIIEncoding.ASCII.GetBytes(plainTextStr)
        Dim pt As BigInteger = New BigInteger(plainTextBA)
        If pt > n Then Throw New Exception() 
        Dim ct As BigInteger = BigInteger.ModPow(pt, e, n)
        Console.WriteLine(" Encoded: " & ct.ToString("X"))
        Dim dc As BigInteger = BigInteger.ModPow(ct, d, n)
        Console.WriteLine(" Decoded: " & dc.ToString("X"))
        Dim decoded As String = ASCIIEncoding.ASCII.GetString(dc.ToByteArray())
        Console.WriteLine("As ASCII: " & decoded)
    End Sub
End Module
Task: RSA code




Go: package main

import (
    "fmt"
    "math/big"
)

func main() {
    var n, e, d, bb, ptn, etn, dtn big.Int
    pt := "Rosetta Code"
    fmt.Println("Plain text:            ", pt)

    
    
    
    n.SetString("9516311845790656153499716760847001433441357", 10)
    e.SetString("65537", 10)
    d.SetString("5617843187844953170308463622230283376298685", 10)

    
    for _, b := range []byte(pt) {
        ptn.Or(ptn.Lsh(&ptn, 8), bb.SetInt64(int64(b)))
    }
    if ptn.Cmp(&n) >= 0 {
        fmt.Println("Plain text message too long")
        return
    }
    fmt.Println("Plain text as a number:", &ptn)

    
    etn.Exp(&ptn, &e, &n)
    fmt.Println("Encoded:               ", &etn)

    
    dtn.Exp(&etn, &d, &n)
    fmt.Println("Decoded:               ", &dtn)

    
    var db [16]byte
    dx := 16
    bff := big.NewInt(0xff)
    for dtn.BitLen() > 0 {
        dx--
        db[dx] = byte(bb.And(&dtn, bff).Int64())
        dtn.Rsh(&dtn, 8)
    }
    fmt.Println("Decoded number as text:", string(db[dx:]))
}




Python: import binascii

n = 9516311845790656153499716760847001433441357    
e = 65537
d = 5617843187844953170308463622230283376298685

message='Rosetta Code!'
print('message                 ', message)

hex_data   = binascii.hexlify(message.encode())
print('hex data                ', hex_data)

plain_text = int(hex_data, 16)
print('plain text integer      ', plain_text)

if plain_text > n:
  raise Exception('plain text too large for key')

encrypted_text = pow(plain_text,     e, n)
print('encrypted text integer  ', encrypted_text)

decrypted_text = pow(encrypted_text, d, n)
print('decrypted text integer  ', decrypted_text)

print('message                 ', binascii.unhexlify(hex(decrypted_text)[2:]).decode())
Task: RSA code




Go: package main

import (
    "fmt"
    "math/big"
)

func main() {
    var n, e, d, bb, ptn, etn, dtn big.Int
    pt := "Rosetta Code"
    fmt.Println("Plain text:            ", pt)

    
    
    
    n.SetString("9516311845790656153499716760847001433441357", 10)
    e.SetString("65537", 10)
    d.SetString("5617843187844953170308463622230283376298685", 10)

    
    for _, b := range []byte(pt) {
        ptn.Or(ptn.Lsh(&ptn, 8), bb.SetInt64(int64(b)))
    }
    if ptn.Cmp(&n) >= 0 {
        fmt.Println("Plain text message too long")
        return
    }
    fmt.Println("Plain text as a number:", &ptn)

    
    etn.Exp(&ptn, &e, &n)
    fmt.Println("Encoded:               ", &etn)

    
    dtn.Exp(&etn, &d, &n)
    fmt.Println("Decoded:               ", &dtn)

    
    var db [16]byte
    dx := 16
    bff := big.NewInt(0xff)
    for dtn.BitLen() > 0 {
        dx--
        db[dx] = byte(bb.And(&dtn, bff).Int64())
        dtn.Rsh(&dtn, 8)
    }
    fmt.Println("Decoded number as text:", string(db[dx:]))
}





VB: Imports System
Imports System.Numerics
Imports System.Text

Module Module1
    Sub Main()
        Dim n As BigInteger = BigInteger.Parse("9516311845790656153499716760847001433441357")
        Dim e As BigInteger = 65537
        Dim d As BigInteger = BigInteger.Parse("5617843187844953170308463622230283376298685")
        Dim plainTextStr As String = "Hello, Rosetta!"
        Dim plainTextBA As Byte() = ASCIIEncoding.ASCII.GetBytes(plainTextStr)
        Dim pt As BigInteger = New BigInteger(plainTextBA)
        If pt > n Then Throw New Exception() 
        Dim ct As BigInteger = BigInteger.ModPow(pt, e, n)
        Console.WriteLine(" Encoded: " & ct.ToString("X"))
        Dim dc As BigInteger = BigInteger.ModPow(ct, d, n)
        Console.WriteLine(" Decoded: " & dc.ToString("X"))
        Dim decoded As String = ASCIIEncoding.ASCII.GetString(dc.ToByteArray())
        Console.WriteLine("As ASCII: " & decoded)
    End Sub
End Module
Task: RSA code







Python: import binascii

n = 9516311845790656153499716760847001433441357    
e = 65537
d = 5617843187844953170308463622230283376298685

message='Rosetta Code!'
print('message                 ', message)

hex_data   = binascii.hexlify(message.encode())
print('hex data                ', hex_data)

plain_text = int(hex_data, 16)
print('plain text integer      ', plain_text)

if plain_text > n:
  raise Exception('plain text too large for key')

encrypted_text = pow(plain_text,     e, n)
print('encrypted text integer  ', encrypted_text)

decrypted_text = pow(encrypted_text, d, n)
print('decrypted text integer  ', decrypted_text)

print('message                 ', binascii.unhexlify(hex(decrypted_text)[2:]).decode())     


VB: Imports System
Imports System.Numerics
Imports System.Text

Module Module1
    Sub Main()
        Dim n As BigInteger = BigInteger.Parse("9516311845790656153499716760847001433441357")
        Dim e As BigInteger = 65537
        Dim d As BigInteger = BigInteger.Parse("5617843187844953170308463622230283376298685")
        Dim plainTextStr As String = "Hello, Rosetta!"
        Dim plainTextBA As Byte() = ASCIIEncoding.ASCII.GetBytes(plainTextStr)
        Dim pt As BigInteger = New BigInteger(plainTextBA)
        If pt > n Then Throw New Exception() 
        Dim ct As BigInteger = BigInteger.ModPow(pt, e, n)
        Console.WriteLine(" Encoded: " & ct.ToString("X"))
        Dim dc As BigInteger = BigInteger.ModPow(ct, d, n)
        Console.WriteLine(" Decoded: " & dc.ToString("X"))
        Dim decoded As String = ASCIIEncoding.ASCII.GetString(dc.ToByteArray())
        Console.WriteLine("As ASCII: " & decoded)
    End Sub
End Module
Task: RSA code

C: #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gmp.h>

int main(void)
{
    mpz_t n, d, e, pt, ct;

    mpz_init(pt);
    mpz_init(ct);
    mpz_init_set_str(n, "9516311845790656153499716760847001433441357", 10);
    mpz_init_set_str(e, "65537", 10);
    mpz_init_set_str(d, "5617843187844953170308463622230283376298685", 10);

    const char *plaintext = "Rossetta Code";
    mpz_import(pt, strlen(plaintext), 1, 1, 0, 0, plaintext);

    if (mpz_cmp(pt, n) > 0)
        abort();

    mpz_powm(ct, pt, e, n);
    gmp_printf("Encoded:   %Zd\n", ct);

    mpz_powm(pt, ct, d, n);
    gmp_printf("Decoded:   %Zd\n", pt);

    char buffer[64];
    mpz_export(buffer, NULL, 1, 1, 0, 0, pt);
    printf("As String: %s\n", buffer);

    mpz_clears(pt, ct, n, e, d, NULL);
    return 0;
}





Java: public static void main(String[] args) {
    
    BigInteger n = new BigInteger("9516311845790656153499716760847001433441357");
    BigInteger e = new BigInteger("65537");
    BigInteger d = new BigInteger("5617843187844953170308463622230283376298685");
    Charset c = Charsets.UTF_8;
    String plainText = "Rosetta Code";
    System.out.println("PlainText : " + plainText);
    byte[] bytes = plainText.getBytes();
    BigInteger plainNum = new BigInteger(bytes);
    System.out.println("As number : " + plainNum);
    BigInteger Bytes = new BigInteger(bytes);
    if (Bytes.compareTo(n) == 1) {
        System.out.println("Plaintext is too long");
        return;
    }
    BigInteger enc = plainNum.modPow(e, n);
    System.out.println("Encoded: " + enc);
    BigInteger dec = enc.modPow(d, n);
    System.out.println("Decoded: " + dec);
    String decText = new String(dec.toByteArray(), c);
    System.out.println("As text: " + decText);
}
Task: RSA code

C: #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gmp.h>

int main(void)
{
    mpz_t n, d, e, pt, ct;

    mpz_init(pt);
    mpz_init(ct);
    mpz_init_set_str(n, "9516311845790656153499716760847001433441357", 10);
    mpz_init_set_str(e, "65537", 10);
    mpz_init_set_str(d, "5617843187844953170308463622230283376298685", 10);

    const char *plaintext = "Rossetta Code";
    mpz_import(pt, strlen(plaintext), 1, 1, 0, 0, plaintext);

    if (mpz_cmp(pt, n) > 0)
        abort();

    mpz_powm(ct, pt, e, n);
    gmp_printf("Encoded:   %Zd\n", ct);

    mpz_powm(pt, ct, d, n);
    gmp_printf("Decoded:   %Zd\n", pt);

    char buffer[64];
    mpz_export(buffer, NULL, 1, 1, 0, 0, pt);
    printf("As String: %s\n", buffer);

    mpz_clears(pt, ct, n, e, d, NULL);
    return 0;
}







Python: import binascii

n = 9516311845790656153499716760847001433441357    
e = 65537
d = 5617843187844953170308463622230283376298685

message='Rosetta Code!'
print('message                 ', message)

hex_data   = binascii.hexlify(message.encode())
print('hex data                ', hex_data)

plain_text = int(hex_data, 16)
print('plain text integer      ', plain_text)

if plain_text > n:
  raise Exception('plain text too large for key')

encrypted_text = pow(plain_text,     e, n)
print('encrypted text integer  ', encrypted_text)

decrypted_text = pow(encrypted_text, d, n)
print('decrypted text integer  ', decrypted_text)

print('message                 ', binascii.unhexlify(hex(decrypted_text)[2:]).decode())
Task: The Twelve Days of Christmas

C: #include<stdio.h>
 
int main()
{
    int i,j;
 
    char days[12][10] = 
    {
        "First",
        "Second",
        "Third",
        "Fourth",
        "Fifth",
        "Sixth",
        "Seventh",
        "Eighth",
        "Ninth",
        "Tenth",
        "Eleventh",
        "Twelfth"
    };

    char gifts[12][33] =
    {
        "Twelve drummers drumming",
        "Eleven pipers piping",
        "Ten lords a-leaping",
        "Nine ladies dancing",
        "Eight maids a-milking",
        "Seven swans a-swimming",
        "Six geese a-laying",
        "Five golden rings",
        "Four calling birds",
        "Three french hens", 
        "Two turtle doves", 
        "And a partridge in a pear tree."
    };
 
    for(i=0;i<12;i++)
    {
        printf("\n\nOn the %s day of Christmas\nMy true love gave to me:",days[i]);
 
        for(j=i;j>=0;j--)
        {
            (i==0)?printf("\nA partridge in a pear tree."):printf("\n%s%c",gifts[11-j],(j!=0)?',':' ');
        }
    }
 
    return 0;
}



C#: using System;

public class TwelveDaysOfChristmas {

    public static void Main() {

        string[] days = new string[12] {
            "first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth",
            "tenth", "eleventh", "twelfth",
        };

        string[] gifts = new string[12] {
            "A partridge in a pear tree",
            "Two turtle doves",
            "Three french hens",
            "Four calling birds",
            "Five golden rings",
            "Six geese a-laying",
            "Seven swans a-swimming",
            "Eight maids a-milking",
            "Nine ladies dancing",
            "Ten lords a-leaping",
            "Eleven pipers piping",
            "Twelve drummers drumming"
        };

        for ( int i = 0; i < 12; i++ ) {

            Console.WriteLine("On the " + days[i] + " day of Christmas, my true love gave to me");

            int j = i + 1;
            while ( j-- > 0 )
                Console.WriteLine(gifts[j]);

            Console.WriteLine();

            if ( i == 0 )
                gifts[0] = "And a partridge in a pear tree";
        }

    }

}
Task: The Twelve Days of Christmas

C: #include<stdio.h>
 
int main()
{
    int i,j;
 
    char days[12][10] = 
    {
        "First",
        "Second",
        "Third",
        "Fourth",
        "Fifth",
        "Sixth",
        "Seventh",
        "Eighth",
        "Ninth",
        "Tenth",
        "Eleventh",
        "Twelfth"
    };

    char gifts[12][33] =
    {
        "Twelve drummers drumming",
        "Eleven pipers piping",
        "Ten lords a-leaping",
        "Nine ladies dancing",
        "Eight maids a-milking",
        "Seven swans a-swimming",
        "Six geese a-laying",
        "Five golden rings",
        "Four calling birds",
        "Three french hens", 
        "Two turtle doves", 
        "And a partridge in a pear tree."
    };
 
    for(i=0;i<12;i++)
    {
        printf("\n\nOn the %s day of Christmas\nMy true love gave to me:",days[i]);
 
        for(j=i;j>=0;j--)
        {
            (i==0)?printf("\nA partridge in a pear tree."):printf("\n%s%c",gifts[11-j],(j!=0)?',':' ');
        }
    }
 
    return 0;
}


C++: #include <iostream>
#include <array>
#include <string>
using namespace std;

int main()
{
    const array<string, 12> days
    {
        "first",
        "second",
        "third",
        "fourth",
        "fifth",
        "sixth",
        "seventh",
        "eighth",
        "ninth",
        "tenth",
        "eleventh",
        "twelfth"
    };
  
    const array<string, 12> gifts
    {
        "And a partridge in a pear tree",
        "Two turtle doves",
        "Three french hens",
        "Four calling birds",
        "FIVE GOLDEN RINGS",
        "Six geese a-laying",
        "Seven swans a-swimming",
        "Eight maids a-milking",
        "Nine ladies dancing",
        "Ten lords a-leaping",
        "Eleven pipers piping",
        "Twelve drummers drumming"
    };

    for(int i = 0; i < days.size(); ++i)
    {
        cout << "On the " << days[i] << " day of christmas, my true love gave to me\n";

        if(i == 0)
        {
            cout << "A partridge in a pear tree\n";
        }
        else
        {
            int j = i + 1;
            while(j-- > 0) cout << gifts[j] << '\n';
        }

        cout << '\n';
    }

    return 0;
}
Task: The Twelve Days of Christmas

C: #include<stdio.h>
 
int main()
{
    int i,j;
 
    char days[12][10] = 
    {
        "First",
        "Second",
        "Third",
        "Fourth",
        "Fifth",
        "Sixth",
        "Seventh",
        "Eighth",
        "Ninth",
        "Tenth",
        "Eleventh",
        "Twelfth"
    };

    char gifts[12][33] =
    {
        "Twelve drummers drumming",
        "Eleven pipers piping",
        "Ten lords a-leaping",
        "Nine ladies dancing",
        "Eight maids a-milking",
        "Seven swans a-swimming",
        "Six geese a-laying",
        "Five golden rings",
        "Four calling birds",
        "Three french hens", 
        "Two turtle doves", 
        "And a partridge in a pear tree."
    };
 
    for(i=0;i<12;i++)
    {
        printf("\n\nOn the %s day of Christmas\nMy true love gave to me:",days[i]);
 
        for(j=i;j>=0;j--)
        {
            (i==0)?printf("\nA partridge in a pear tree."):printf("\n%s%c",gifts[11-j],(j!=0)?',':' ');
        }
    }
 
    return 0;
}




Go: package main

import (
	"fmt"
)

func main() {
	days := []string{"first", "second", "third", "fourth", "fifth", "sixth",
		"seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth"}

	gifts := []string{"A Partridge in a Pear Tree", "Two Turtle Doves and", "Three French Hens",
		"Four Calling Birds", "Five Gold Rings", "Six Geese a-Laying",
		"Seven Swans a-Swimming", "Eight Maids a-Milking", "Nine Ladies Dancing",
		"Ten Lords a-Leaping", "Eleven Pipers Piping", "Twelve Drummers Drumming"}

	for i := 0; i < 12; i++ {
		fmt.Printf("On the %s day of Christmas,\n", days[i])
		fmt.Println("My true love sent to me:")

		for j := i; j >= 0; j-- {
			fmt.Println(gifts[j])
		}
		fmt.Println()
	}
}