//
// Encrypt the byte array with Blowfish
//
func EncryptBlowfish(str string, blowfish_key []byte) (string, error) {
	as_bytes := []byte(str)

	// Max of 8 bytes
	if len(as_bytes) > 8 {
		return "", errors.New(fmt.Sprintf("Input string is too long for blowfish! Max of 8 characters, str = %s", str))
	}

	// 8-Byte Buffers:
	input := []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
	output := []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}

	// Write the bytes to the "input" buffer
	copy(input, as_bytes)

	// Create the blowfish cypher
	cypher, err := blowfish.NewCipher(blowfish_key)
	if nil != err {
		return "", err
	}

	// Encrypt the buffer
	cypher.Encrypt(output, input)

	// Convert the byte array back to a string
	return strings.ToUpper(hex.EncodeToString(output)), nil
}
Exemple #2
0
func saveServices(passwd string, services *Services) {
	sort.Sort(services)
	decryptedBuffer := bytes.NewBuffer(nil)
	err := json.NewEncoder(decryptedBuffer).Encode(services)
	gobro.CheckErr(err)

	blockCipher, err := blowfish.NewCipher([]byte(passwd))
	gobro.CheckErr(err)
	iv := make([]byte, 8)
	_, err = rand.Read(iv)
	gobro.CheckErr(err)

	enc := decryptedBuffer.Bytes()
	cipher.NewCFBEncrypter(blockCipher, iv).XORKeyStream(enc, enc)
	buff := bytes.NewBuffer(iv)
	_, err = buff.Write(enc)
	gobro.CheckErr(err)

	enc = append(iv, enc...)
	base64EncodedLen := base64.StdEncoding.EncodedLen(len(enc))
	base64Enc := make([]byte, base64EncodedLen)
	base64.StdEncoding.Encode(base64Enc, enc)

	err = ioutil.WriteFile(passwdFileName(), base64Enc, os.ModePerm)
	gobro.CheckErr(err)
}
func DecryptBlowfish(str string, blowfish_key []byte) (string, error) {
	as_bytes, err := hex.DecodeString(str)
	if nil != err {
		return "", err
	}

	// Exactly 8 bytes required
	if len(as_bytes) != 8 {
		return "", errors.New(fmt.Sprintf("Input string is incorrect for blowfish! Requires exactly of 8 characters, str = %s", str))
	}

	// 8-Byte Buffers:
	input := as_bytes
	output := []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}

	// Create the blowfish cypher
	cypher, err := blowfish.NewCipher(blowfish_key)
	if nil != err {
		return "", err
	}

	// Encrypt the buffer
	cypher.Decrypt(output, input)

	// Remove the trailing 0x00 bytes we added to the string
	for i := 7; i >= 0 && output[i] == 0x00; i-- {
		output = output[0:i]
	}

	// Convert the byte array back to a string
	return strings.TrimSpace(string(output)), nil
}
Exemple #4
0
func main() {
	in := []byte("Hello, World!!")
	key := []byte("This is key!!")
	if block, err := blowfish.NewCipher(key); err != nil {
		// Encrypt
		enc := cipher.NewCBCEncrypter(block, make([]byte, blowfish.BlockSize))
		var encrypted []byte
		enc.CryptBlocks(encrypted, in)
		fmt.Println(encrypted)
	}
}
Exemple #5
0
func main() {
	key := []byte("my key")
	cipher, err := blowfish.NewCipher(key)
	if err != nil {
		fmt.Println(err.Error())
	}
	src := []byte("hello\n\n\n")
	var enc [512]byte

	cipher.Encrypt(enc[0:], src)

	var decrypt [8]byte
	cipher.Decrypt(decrypt[0:], enc[0:])
	result := bytes.NewBuffer(nil)
	result.Write(decrypt[0:8])
	fmt.Println(string(result.Bytes()))
}
Exemple #6
0
func loadServices(passwd string) (*Services, error) {
	base64Enc, err := ioutil.ReadFile(passwdFileName())
	if err != nil {
		return nil, err
	}

	enc := make([]byte, base64.StdEncoding.DecodedLen(len(base64Enc)))
	base64.StdEncoding.Decode(enc, base64Enc)

	blockCipher, err := blowfish.NewCipher([]byte(passwd))
	gobro.CheckErr(err)
	decrypted := make([]byte, len(enc)-8)
	cipher.NewCFBDecrypter(blockCipher, enc[:8]).XORKeyStream(decrypted, enc[8:])

	services := new(Services)
	err = json.NewDecoder(bytes.NewBuffer(decrypted)).Decode(services)
	if err != nil {
		return nil, errors.New("Invalid password")
	}
	return services, nil
}
Exemple #7
0
func newBlowFishStream(key, iv []byte, doe DecOrEnc) (cipher.Stream, error) {
	block, err := blowfish.NewCipher(key)
	return newStream(block, err, key, iv, doe)
}
Exemple #8
0
func newBlowFishCipher(key []byte) (cipher.Block, error) {
	return blowfish.NewCipher(key)
}
func make_chugger(key []byte) *chugger {
	state, _ := blowfish.NewCipher(key)
	streamer := cipher.NewCTR(state, make([]byte, state.BlockSize()))
	return &chugger{streamer, key, 0, 0}
}