PBEWithMD5AndTripleDES

package module
v0.0.0-...-b85826f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 4, 2024 License: MIT Imports: 7 Imported by: 0

README

PBEWithMD5AndTripleDES java com.sun.crypto.provider implementation (JSE) using golang

Usage example:


import (
    tripleDES "github.com/ebirukov/PBEWithMD5AndTripleDES"
    "fmt"
)

func main() {
    password := []byte("mypassword")
    originalText := []byte("mysecret_content")
    iterations := 2000
    
    params := tripleDES.GeneratePBEParams(iterations)
    enc := tripleDES.NewEncryptCipher(password, params)
    dec, err := tripleDES.NewDecryptCipher(password, params.Encode())
    if err != nil {
        panic(err.Error())
    }
    
    encrypted := enc.Encrypt(originalText)
    fmt.Printf("encrypted data: % #x\n", encrypted)
    
    decrypted := dec.Decrypt(encrypted)
    //print decrypted secret string: mysecret_content
    fmt.Printf("decrypted secret string: %+v\n", string(decrypted))
}

Java equivalence:

keySpec = new PBEKeySpec(password.toCharArray(), salt, iterations);
key = SecretKeyFactory.getInstance("PBEWithMD5AndTripleDES").generateSecret(keySpec);
ecipher = Cipher.getInstance(key.getAlgorithm());
dcipher = Cipher.getInstance(key.getAlgorithm());
ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);

byte[] enc = ecipher.doFinal(originalText);
String res = Base64.getEncoder().encodeToString(enc);
System.out.println("encrypted " + res);

byte[] dec = Base64.getDecoder().decode(res);
dec = dcipher.doFinal(dec);
System.out.println("decrypted " + dec);

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PKCS5Padding

func PKCS5Padding(ciphertext []byte, blockSize int) []byte

func PKCS5Trimming

func PKCS5Trimming(encrypt []byte) []byte

Types

type Cipher

type Cipher struct {
	// contains filtered or unexported fields
}

func NewDecryptCipher

func NewDecryptCipher(password []byte, encodedParams []byte) (*Cipher, error)

NewDecryptCipher construct decrypter cipher by PBE params and password

func NewEncryptCipher

func NewEncryptCipher(password []byte, params PBEParams) *Cipher

NewEncryptCipher construct encryptor cipher by PBE params and password

func (*Cipher) Decrypt

func (c *Cipher) Decrypt(src []byte) []byte

func (*Cipher) Encrypt

func (c *Cipher) Encrypt(src []byte) []byte

type PBEParams

type PBEParams struct {
	Salt       []byte
	Iterations int
}

func DecodePBEParams

func DecodePBEParams(encodedParams []byte) (*PBEParams, error)

func GeneratePBEParams

func GeneratePBEParams(iterations int) PBEParams

func (PBEParams) Encode

func (pbe PBEParams) Encode() (enc []byte)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL